x = np.array([[1,2],[3,4]], dtype=np.float64)
y = np.array([[5,6],[7,8]], dtype=np.float64)
# 逐元素求和
print(x + y) #直接向量相加
print(np.add(x, y)) #调用函数
# 逐元素作差
print(x - y) #直接向量相减
print(np.subtract(x, y)) #调用函数
# 逐元素相乘
print(x * y)
print(np.multiply(x, y))
# 逐元素相除
print(x / y)
print(np.divide(x, y))
# 逐元素求平方根
print(np.sqrt(x))
[[ 6. 8.]
[ 10. 12.]]
[[ 6. 8.]
[ 10. 12.]]
[[-4. -4.]
[-4. -4.]]
[[-4. -4.]
[-4. -4.]]
[[ 5. 12.]
[ 21. 32.]]
[[ 5. 12.]
[ 21. 32.]]
[[ 0.2 0.33333333]
[ 0.42857143 0.5 ]]
[[ 0.2 0.33333333]
[ 0.42857143 0.5 ]]
[[ 1. 1.41421356]
[ 1.73205081 2. ]]
NumPy计算乘积的函数:dot,inner,outer
a = np.arange(12).reshape(2,3,2)
b = np.arange(12,24).reshape(2,2,3)
c = np.dot(a,b)
print(c.shape) # (2, 3, 2, 3)
print(np.alltrue( c[0,:,0,:] == np.dot(a[0],b[0]) )) # True
print(np.alltrue( c[1,:,0,:] == np.dot(a[1],b[0]) )) # True
print(np.alltrue( c[0,:,1,:] == np.dot(a[0],b[1]) )) # True
print(np.alltrue( c[1,:,1,:] == np.dot(a[1],b[1]) )) # True
a = np.arange(12).reshape(2,3,2)
b = np.arange(12,24).reshape(2,3,2)
d = np.inner(a,b)
print(d.shape) # (2, 3, 2, 3)
print(d[0,0,0,0] == np.inner(a[0,0],b[0,0])) # True
print(d[0,1,1,0] == np.inner(a[0,1],b[1,0])) # True
print(d[1,2,1,2] == np.inner(a[1,2],b[1,2])) # True
print(np.outer([1,2,3],[4,5,6,7]))
(2, 3, 2, 3)
True
True
True
True
(2, 3, 2, 3)
True
True
True
[[ 4 5 6 7]
[ 8 10 12 14]
[12 15 18 21]]
v = np.array([9,10])
w = np.array([11, 12])
print(v.dot(w))
print(np.dot(v, w))
219
219
v = np.array([9,10])
x = np.array([[1,2],[3,4]], dtype=np.float64)
print(np.dot(x, v))
print(np.matmul(x,v))
[ 29. 67.]
[ 29. 67.]
[ 29. 67.]
x = np.array([[1,2],[3,4]], dtype=np.float64)
print(x)
print(x.T)
v = np.array([1,2,3])
print(v)
print(v.T) #一维数组转置不变
w = np.array([[1,2,3]])
print(w)
print(w.T)
[[ 1. 2.]
[ 3. 4.]]
[[ 1. 3.]
[ 2. 4.]]
[1 2 3]
[1 2 3]
[[1 2 3]]
[[1]
[2]
[3]]
arr = np.arange(16).reshape((2, 2, 4))
print(arr)
[[[ 0 1 2 3]
[ 4 5 6 7]]
[[ 8 9 10 11]
[12 13 14 15]]]
print(arr.transpose((1,0,2)))
[[[ 0 1 2 3]
[ 8 9 10 11]]
[[ 4 5 6 7]
[12 13 14 15]]]
print(arr.swapaxes(1,2))
[[[ 0 4]
[ 1 5]
[ 2 6]
[ 3 7]]
[[ 8 12]
[ 9 13]
[10 14]
[11 15]]]
x = np.array([[1,2],[3,4]])
print(x)
print(np.sum(x)) # 数组/矩阵中所有元素求和; prints "10"
print(np.sum(x, axis=0)) # 按行去求和; prints "[4 6]"
print(np.sum(x, axis=1)) # 按列去求和; prints "[3 7]"
print(np.mean(x)) # 数组/矩阵中所有元素求均值; prints "2.5"
print(np.mean(x, axis=0)) # 按行去求均值; prints "[ 2. 3.]"
print(np.mean(x, axis=1)) # 按列去求均值; prints "[ 1.5 3.5]"
print(np.max(x)) # 数组/矩阵中所有元素求最大值; prints "4"
print(np.min(x)) # 数组/矩阵中所有元素求最小值; prints "1"
print(np.std(x,axis=0)) #按行去求标准差; prints "[ 1. 1.]"
print(np.var(x,axis=1)) #按列去求方差; prints "[ 0.25 0.25]"
print(x.cumsum(axis=0)) # 按行去累加; prints "[[1 2][4 6]]"
print(x.cumprod(axis=1)) # 按列去累乘;prints "[[ 1 2][ 3 12]]"
[[1 2]
[3 4]]
10
[4 6]
[3 7]
2.5
[ 2. 3.]
[ 1.5 3.5]
4
1
[ 1. 1.]
[ 0.25 0.25]
[[1 2]
[4 6]]
[[ 1 2]
[ 3 12]]
arr = np.random.randn(8)
print(arr)
arr.sort()
print(arr)
[ 0.70150419 -0.88493701 0.37449618 -0.42676191 1.52654468 -1.79515205
0.05635219 0.80712566]
[-1.79515205 -0.88493701 -0.42676191 0.05635219 0.37449618 0.70150419
0.80712566 1.52654468]
arr = np.random.randn(5,3)
print(arr)
arr.sort(1)
print(arr)
[[-0.51135747 -0.0355637 0.38398028]
[-1.44309081 1.31425286 0.16295143]
[-0.54112556 -1.07293118 0.55690543]
[ 0.55382507 0.79843566 -1.29064181]
[ 0.69978121 0.24467205 0.13107927]]
[[-0.51135747 -0.0355637 0.38398028]
[-1.44309081 0.16295143 1.31425286]
[-1.07293118 -0.54112556 0.55690543]
[-1.29064181 0.55382507 0.79843566]
[ 0.13107927 0.24467205 0.69978121]]
找出排序后位置在5%的数字
large_arr = np.random.randn(1000)
large_arr.sort()
print(large_arr[int(0.05*len(large_arr))])
-1.65535730932
应用场景:要用小的矩阵去和大的矩阵做一些操作,但是希望小矩阵能循环和大矩阵的那些块做一样的操作。
x = np.array([[1,2,3], [4,5,6]])
v = np.array([1,2,3])
print(x + v)
[[2 4 6]
[5 7 9]]
x = np.array([[1,2,3], [4,5,6]]) # 2x3的
w = np.array([4,5]) # w 形状是 (2,)
print((x.T + w).T) #通过转置完成广播运算
[[ 5 6 7]
[ 9 10 11]]
x = np.array([[1,2,3], [4,5,6]]
print(x * 2)
[[ 2 4 6]
[ 8 10 12]]
np.where(cond,x,y):满足条件(cond)输出x,不满足输出y
x_arr = np.array([1.1, 1.2, 1.3, 1.4, 1.5])
y_arr = np.array([2.1, 2.2, 2.3, 2.4, 2.5])
cond = np.array([True, False, True, True, False])
print(np.where(cond, x_arr, y_arr))
[ 1.1 2.2 1.3 1.4 2.5]
arr = np.random.randn(4,4)
print(arr)
print(np.where(arr > 0, 2, -2))
print(np.where(arr > 0, 2, arr))
[[ -1.10484247e+00 -3.82422727e-01 -3.24361549e-01 1.21286234e+00]
[ 1.54499855e-01 -4.77728163e-04 1.44621074e+00 -2.64241611e-03]
[ 1.36394862e+00 6.96638259e-02 -2.75237740e-01 -3.32892881e-01]
[ -1.37165175e+00 1.79997993e-01 -1.13509664e-01 1.88373639e+00]]
[[-2 -2 -2 2]
[ 2 -2 2 -2]
[ 2 2 -2 -2]
[-2 2 -2 2]]
[[ -1.10484247e+00 -3.82422727e-01 -3.24361549e-01 2.00000000e+00]
[ 2.00000000e+00 -4.77728163e-04 2.00000000e+00 -2.64241611e-03]
[ 2.00000000e+00 2.00000000e+00 -2.75237740e-01 -3.32892881e-01]
[ -1.37165175e+00 2.00000000e+00 -1.13509664e-01 2.00000000e+00]]
np.where可以嵌套使用
cond_1 = np.array([True, False, True, True, False])
cond_2 = np.array([False, True, False, True, False])
result = np.where(cond_1 & cond_2, 0, \
np.where(cond_1, 1, np.where(cond_2, 2, 3)))
print(result)
[1 2 1 0 3]
arr = np.random.randn(10)
print(arr)
print((arr > 0).sum()) #数组中大于0的数相加
[ 0.27350655 -1.51093462 0.26835915 -0.45991855 1.34450904 -1.86871203
0.04308971 1.69640444 -0.02191351 -0.43875275]
5
bools = np.array([False, False, True, False])
print(bools.any()) # 有一个为True则返回True
print(bools.all()) # 有一个为False则返回False
True
False
[numpy指南]http://docs.scipy.org/doc/numpy/reference/
[numpy ndarray详解]https://danzhuibing.github.io/py_numpy_ndarray.html
[NumPy-快速处理数据]http://old.sebug.net/paper/books/scipydoc/numpy_intro.html