[NumPy 学习笔记] - No.3 NumPy数值运算及矩阵操作

numpy学习笔记

numpy是python中非常有用的一个库,我们可以使用numpy创建大型的高维数组并进行运算。这里记录一下numpy一些常用的方法。如果想仔细研究numpy的强大功能还需要翻阅NumPy API文档

数组运算

基本运算

在numpy中,最基本的运算是加减,即对应位置元素做加减法;注意我们提到的运算是shape相同的两个变量之间的运算。

# 基本运算
x = np.array([[1,2], [3,4]], dtype=np.float64)
y = np.array([[1,2], [3,4]], dtype=np.float64)
print ("x + y:\n", np.add(x, y)) # or x + y
print ("x - y:\n", np.subtract(x, y)) # or x - y

x + y:
[[ 2. 4.]
[ 6. 8.]]
x - y:
[[ 0. 0.]
[ 0. 0.]

除此之外,还可以对矩阵的行元素求和,列元素求和,全部元素求和:

x = np.array([[1,2],[3,4]])
print (x)
print ("sum all: ", np.sum(x)) # adds all elements
print ("sum by col: ", np.sum(x, axis=0)) # add numbers in each column
print ("sum by row: ", np.sum(x, axis=1)) # add numbers in each row

[[1 2]
[3 4]]
sum all: 10
sum by col: [4 6]
sum by row: [3 7]

乘法和点乘

乘法很简单,就是矩阵对应位置求乘积; 点乘则是数学中的一种矩阵运算

x = np.array([[1,2], [3,4]], dtype=np.float64)
y = np.array([[1,2], [3,4]], dtype=np.float64)
print ("x * y:\n", np.multiply(x, y)) # or x * y  

# Dot product
a = np.array([[1,2,3], [4,5,6]], dtype=np.float64) # we can specify dtype
b = np.array([[7,8], [9,10], [11, 12]], dtype=np.float64)
print ("dot product:\n",a.dot(b))

x * y:
[[ 1. 4.]
[ 9. 16.]]
dot product:
[[ 58. 64.]
[ 139. 154.]]

矩阵转置
# Transposing
print ("x:\n", x)
#x:
# [[1 2]
# [3 4]]

print ("x.T:\n", x.T)
#x.T:
# [[1 3]
# [2 4]]

高级操作

Tile

np.tile(A,B)代表将A重复B次;B如果是int型表示在列方向上重复B次;B如果是二元组表示分别在行和列上重复。

x = np.array([[1,2], [3,4]])
print("列方向重复2:\n",np.tile(x,2))
#列方向重复2:
# [[1 2 1 2]
# [3 4 3 4]]

print("行列均重复2:\n",np.tile(x,(2,2)))
#行列均重复2:
# [[1 2 1 2]
# [3 4 3 4]
# [1 2 1 2]
# [3 4 3 4]]
Broadcasting

之前,我们说过在numpy的运算中,参与运算的两个变量需要具有相同的shape。如果两个变量尺寸不相同可以运算么?

进行x + y操作, 如果x,y的尺寸不相同,在满足一定的条件下,触发广播机制,是可以进行运算的

我们对 x,y的shape,从后向前逐位比对,如果x,y的shape每一位数字满足下列条件中的一个:

  • 相同
  • 有一个数字是1

比对完成后,那么x,y是可以进行广播的。

例如:

x.shape是(3,3,3) y.shape是(3,3,3) x,y的shape 每一位数字满足上述条件 可以进行计算

x.shape是(3,5) y.shape 是(3,1) x,y的shape 每一位数字满足上述条件 可以进行广播

x.shape是(3,5) y.shape 是(5,3) x,y的shape 每一位数字不满足上述条件 不可以进行广播

我们来看一个例子

a = np.array([[ 0, 0, 0],
          [10,10,10],
          [20,20,20],
          [30,30,30]])
b = np.array([1,2,3])
print(a + b)

结果如下:

[[ 1 2 3]
[11 12 13]
[21 22 23]
[31 32 33]]

运行a+b,由于a,b的尺寸不一样,且a.shape == (4,3), b.shape == (1,3) , 触发了广播机制

在这里插入图片描述

如上图所示,由于a,b的列相同,行数不相同,因此b在行方向重复4次,然后和a相加。如果是4X3 的矩阵和4x1 的矩阵相加,那么就是将3x1 矩阵在列方向重复三次,进行相加。

Reshape

在numpy中,使用reshape对数组进行尺寸转换,但要求reshape前后两个矩阵的元素相同,及3x4的矩阵无法被reshape 成 1x8的矩阵

以3x4矩阵为例:

x = np.array([[1,2,3,4],[5,6,7,8],[9,10,11,12]])
y1 = x.reshape(4,3)
print("y1:\n",y1)

z1 = x.reshape(2,2,3)
print("z1:\n",z1)

# reshape的参数中,允许一个参数为未知,即-1,另外一个维度会被自动计算出来
z2 = x.reshape(2,-1)
print("z2:\n",z2)

z3 = x.reshape(-1)   #直接将数组拉成一维数组
print("z3:\n",z3)

y1:
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[10 11 12]]
z1:
[[[ 1 2 3]
[ 4 5 6]]

[[ 7 8 9]
[10 11 12]]]
z2:
[[ 1 2 3 4 5 6]
[ 7 8 9 10 11 12]]
z3:
[ 1 2 3 4 5 6 7 8 9 10 11 12]

Removing dimensions

使用np.squeeze (x) 来删除x中单维度,例如:

x = np.array([[[1,2,1]],[[2,2,3]]])
print ("x.shape: ", x.shape)
y = np.squeeze(x) # squeeze dim 1
print ("y.shape: ", y.shape) 
print ("y: \n", y)

x.shape: (2, 1, 3)
y.shape: (2, 3)
y:
[[1 2 1]
[2 2 3]]

如果变量x中有多个单维度,可以通过制定axis来删除某个维度:

x = np.arange(12).reshape(1,3,1,4)
print("x:\n",x)
print("x.shape:\n",x.shape)

y1 = np.squeeze(x,0)
y2 = np.squeeze(x,2)
print("y1.shape:\n",y1.shape)
print("y2.shape:\n",y2.shape)

x:
[[[[ 0 1 2 3]]

[[ 4 5 6 7]]

[[ 8 9 10 11]]]]
x.shape:
(1, 3, 1, 4)
y1.shape:
(3, 1, 4)
y2.shape:
(1, 3, 4)

Adding dimensions

除了删除维度之外,还可以添加一个单维度。

# Adding dimensions
x = np.array([[1,2,1],[2,2,3]])
print ("x.shape: ", x.shape)
y = np.expand_dims(x, 1) # expand dim 1
print ("y.shape: ", y.shape) 
print ("y: \n", y)

x.shape: (2, 3)
y.shape: (2, 1, 3)
y:
[[[1 2 1]]

[[2 2 3]]]

资料参考:practicalAI

发布了118 篇原创文章 · 获赞 140 · 访问量 25万+

猜你喜欢

转载自blog.csdn.net/tjuyanming/article/details/88562146