本文最后更新于 2024-02-08T15:00:54+00:00
numpy教程
阅读该教程,您需要python基础的预备知识
矩阵基础
注意:矩阵必须满足所有行的元素个数相等,像b=np.array([[5,6,3,4],[2,4,7]])
是不合法的
1 2 3 4 5 6 7 8 9
| import numpy as np arr=np.array([[1,2,3], [2,3,4]]) print(arr) print([[1,2,3], [4,5,6]]) print(f"arr是{arr.ndim}维矩阵") print(f"行数和列数为{arr.shape}") print(f"arr中的元素总数为{arr.size}")
|
1 2 3 4 5 6
| [[1 2 3] [2 3 4]] [[1, 2, 3], [4, 5, 6]] arr是2维矩阵 行数和列数为(2, 3) arr中的元素总数为6
|
dtype元素的类型
定义矩阵时可以用dtype声明元素的类型,常见包括
- int64 64位整数
- int32 32位整数
- float32
- float64
- float16
了解更多numpy的数据类型,请阅读
1 2 3 4
| import numpy as np arr=np.array([2,2333,3,4],np.dtype(np.float64)) print(arr.dtype) print(arr)
|
1 2
| float64 [2.000e+00 2.333e+03 3.000e+00 4.000e+00]
|
zeros 全零矩阵
1 2 3 4
| import numpy as np a=np.zeros((3,4)) print(a) print(a.dtype)
|
1 2 3 4
| [[0. 0. 0. 0.] [0. 0. 0. 0.] [0. 0. 0. 0.]] float64
|
ones 全一矩阵
1 2 3 4
| import numpy as np a=np.ones((3,4)) print(a) print(a.dtype)
|
1 2 3 4
| [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] float64
|
empty 元素都几乎接近0的矩阵
1 2 3 4
| import numpy as np a=np.empty((3,4)) print(a) print(a.dtype)
|
1 2 3 4
| [[6.95322371e-310 0.00000000e+000 0.00000000e+000 0.00000000e+000] [0.00000000e+000 0.00000000e+000 0.00000000e+000 0.00000000e+000] [0.00000000e+000 0.00000000e+000 0.00000000e+000 0.00000000e+000]] float64
|
arrange 某一区间的数列
np.arrange(a,b,c)
等价于matlab中的a:c:b
1 2 3 4 5 6 7
| import numpy as np a=np.arange(12) print(a) b=np.arange(2,10) print(b) c=np.arange(4,15,2) print(c)
|
1 2 3
| [ 0 1 2 3 4 5 6 7 8 9 10 11] [2 3 4 5 6 7 8 9] [ 4 6 8 10 12 14]
|
reshape 改变矩阵形状
1 2 3 4
| import numpy as np a=np.arange(12) a=a.reshape((3,4)) print(a)
|
1 2 3
| [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
|
linspace(开始,结尾,分几段)
1 2 3 4
| import numpy as np a=np.linspace(1,5,9) print(a.reshape(3,3)) print(a)
|
随机矩阵
1 2 3
| import numpy as np a=np.random.random((3,2)) print(a)
|
1 2 3
| [[0.81661957 0.66981303] [0.50054947 0.85381138] [0.36714028 0.16603213]]
|
矩阵运算
矩阵和标量的基本运算
1 2 3 4 5 6 7 8 9 10
| import numpy as np a=np.array([1,2,3,4,5]) print(a) print(a+2) print(a-3) print(a*5) print(a/4) print(a//4) print(a**2) print(a*np.sin(a))
|
1 2 3 4 5 6 7 8
| [1 2 3 4 5] [3 4 5 6 7] [-2 -1 0 1 2] [ 5 10 15 20 25] [0.25 0.5 0.75 1. 1.25] [0 0 0 1 1] [ 1 4 9 16 25] [ 0.84147098 1.81859485 0.42336002 -3.02720998 -4.79462137]
|
矩阵元素的判断
1 2 3 4
| import numpy as np a=np.array([1,2,3,4,5]) print(a) print(a<=3)
|
1 2
| [1 2 3 4 5] [ True True True False False]
|
元素乘法和矩阵乘法
1 2 3 4 5 6 7 8 9 10 11
| import numpy as np a=np.array([[1,1], [0,1]]) b=np.array([[0,1], [2,3]]) c=a*b print(c) c_dot=np.dot(a,b) print(c_dot) c_dot_2=a.dot(b) print(c_dot_2)
|
1 2 3 4 5 6
| [[0 1] [0 3]] [[2 4] [2 3]] [[2 4] [2 3]]
|
求和sum,最小值min,最大值max
1 2 3 4 5 6
| import numpy as np a=np.random.random((3,2)) print(a) print(np.sum(a)) print(np.min(a)) print(np.max(a))
|
1 2 3 4 5 6
| [[0.16496185 0.9437828 ] [0.81840345 0.89624578] [0.26440633 0.74531292]] 3.833113133002955 0.16496184822955373 0.9437827991060538
|
使用axis axis的意义是维度数
1 2 3 4 5 6
| import numpy as np a=np.array([[1,2,3,4], [2,3,4,5], [3,4,5,6]]) print(np.sum(a,axis=0)) print(np.sum(a,axis=1))
|
min,max,mean也适用axis
argmin,argmax矩阵中最小最大值的索引
1 2 3 4
| import numpy as np a=np.arange(2,14).reshape((3,4)) print(np.argmin(a)) print(np.argmax(a))
|
1 2 3 4 5 6
| import numpy as np a=np.arange(2,14).reshape((3,4)) print(np.mean(a)) print(np.average(a)) print(a.mean()) print(np.median(a))
|
cumsum累加数组 diff数组差分
1 2 3 4 5
| import numpy as np a=np.arange(2,14).reshape((3,4)) print(a) print(np.cumsum(a)) print(np.diff(a))
|
1 2 3 4 5 6 7
| [[ 2 3 4 5] [ 6 7 8 9] [10 11 12 13]] [ 2 5 9 14 20 27 35 44 54 65 77 90] [[1 1 1] [1 1 1] [1 1 1]]
|
sort排序每行
1 2 3 4
| import numpy as np a=np.arange(14,2,-1).reshape((3,4)) print(a) print(np.sort(a))
|
1 2 3 4 5 6
| [[14 13 12 11] [10 9 8 7] [ 6 5 4 3]] [[11 12 13 14] [ 7 8 9 10] [ 3 4 5 6]]
|
矩阵转置
1 2 3 4 5
| import numpy as np a=np.arange(14,2,-1).reshape((3,4)) print(a) print(np.transpose(a)) print(a.T)
|
1 2 3 4 5 6 7 8 9 10 11
| [[14 13 12 11] [10 9 8 7] [ 6 5 4 3]] [[14 10 6] [13 9 5] [12 8 4] [11 7 3]] [[14 10 6] [13 9 5] [12 8 4] [11 7 3]]
|
clip(矩阵,矩阵中保留数的下限,矩阵中保留数的上限)
1 2 3 4
| import numpy as np a=np.arange(14,2,-1).reshape((3,4)) print(a) print(np.clip(a,5,9))
|
1 2 3 4 5 6
| [[14 13 12 11] [10 9 8 7] [ 6 5 4 3]] [[9 9 9 9] [9 9 8 7] [6 5 5 5]]
|
矩阵索引
下标从0开始算
对于行向量,a[i]会取出下标为i的元素
1 2 3 4
| import numpy as np a=np.arange(3,15) print(a) print(a[3])
|
1 2
| [ 3 4 5 6 7 8 9 10 11 12 13 14] 6
|
对于3行4列的矩阵,a[i]取出第i行
1 2 3 4
| import numpy as np a=np.arange(3,15).reshape(3,4) print(a) print(a[2])
|
1 2 3 4
| [[ 3 4 5 6] [ 7 8 9 10] [11 12 13 14]] [11 12 13 14]
|
1 2 3 4 5
| import numpy as np a=np.arange(3,15).reshape(3,4) print(a) print(a[2][1]) print(a[2,1])
|
1 2 3 4 5
| [[ 3 4 5 6] [ 7 8 9 10] [11 12 13 14]] 12 12
|
利用冒号
注意:取出来的都是行向量
1 2 3 4 5 6 7
| import numpy as np a=np.arange(3,15).reshape(3,4) print(a) print(a[2,:]) print(a[:,1]) print(a[1,1:3]) print(a[0,0:4:2])
|
1 2 3 4 5 6 7
| [[ 3 4 5 6] [ 7 8 9 10] [11 12 13 14]] [11 12 13 14] [ 4 8 12] [8 9] [3 5]
|
flat和flatten
1 2 3 4
| import numpy as np a=np.arange(3,15).reshape(3,4) print(a.flat) print(a.flatten())
|
1 2
| <numpy.flatiter object at 0x0000021DAA7D7CE0> [ 3 4 5 6 7 8 9 10 11 12 13 14]
|
for循环遍历
1 2 3 4 5 6 7
| import numpy as np a=np.arange(3,15).reshape(3,4) print(a) for row in a: print(row) for col in a.T: print(col)
|
1 2 3 4 5 6 7 8 9 10
| [[ 3 4 5 6] [ 7 8 9 10] [11 12 13 14]] [3 4 5 6] [ 7 8 9 10] [11 12 13 14] [ 3 7 11] [ 4 8 12] [ 5 9 13] [ 6 10 14]
|
遍历所有元素
1 2 3 4 5
| import numpy as np a=np.arange(3,15).reshape(3,4) print(a) for item in a.flat: print(item,end=" ")
|
1 2 3 4
| [[ 3 4 5 6] [ 7 8 9 10] [11 12 13 14]] 3 4 5 6 7 8 9 10 11 12 13 14
|
矩阵的合并
1 2 3 4 5 6 7
| import numpy as np a=np.array([1,1,1]) b=np.array([2,2,2]) c=np.vstack((a,b)) d=np.hstack((a,b)) print(c) print(d)
|
1 2 3
| [[1 1 1] [2 2 2]] [1 1 1 2 2 2]
|
转置合并
1 2 3 4 5
| import numpy as np a=np.array([1,1,1])[:,np.newaxis] b=np.array([2,2,2])[:,np.newaxis] d=np.hstack((a,b)) print(d)
|
newaxis的使用
newaxis用于增加维度
1 2 3 4 5 6 7 8 9
| import numpy as np a=np.array([1,1,1]) print(a.shape) c=a[:,np.newaxis] print(c) print(c.shape) d=a[np.newaxis,:] print(d) print(d.shape)
|
1 2 3 4 5 6 7
| (3,) [[1] [1] [1]] (3, 1) [[1 1 1]] (1, 3)
|
concatenate矩阵拼接
1 2 3 4 5
| import numpy as np a=np.array([1,1,1])[:,np.newaxis] b=np.array([2,2,2])[:,np.newaxis] print(np.concatenate((a,b,b,a),axis=0)) print(np.concatenate((a,b,b,a),axis=1))
|
深浅拷贝
1 2 3 4
| import numpy as np a=np.arange(4) b=a print(b is a)
|
等号复制是浅拷贝
1 2 3 4 5 6 7
| import numpy as np a=np.arange(4) b=a print(a) a[0]=11 print(a) print(b)
|
1 2 3
| [0 1 2 3] [11 1 2 3] [11 1 2 3]
|
利用copy深拷贝
1 2 3 4 5 6 7 8
| import numpy as np a=np.arange(4) b=a.copy() print(b is a) print(a) a[1:3]=[22,33] print(a) print(b)
|
1 2 3 4
| False [0 1 2 3] [ 0 22 33 3] [0 1 2 3]
|