1: arange+reshape,arange()按照指定步長遞增生成
>>> mat = np.arange(0,18,2)
>>> mat = mat.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
2: array+reshape
>>> mat = np.array([1,3,4,6])
>>> mat = mat.reshape(2,2)
[[1 3]
[4 6]]
3: empty()生成接近於0的隨機數矩陣
>>> mat = np.empty((3,3))
[[0.00000000e+000 0.00000000e+000 0.00000000e+000]
[0.00000000e+000 0.00000000e+000 3.39917164e-321]
[2.22522596e-306 9.34609789e-307 2.56765117e-312]]
4: ones()生成全1矩陣
>>> mat = np.ones((3,2))
[[1. 1.]
[1. 1.]
[1. 1.]]
5: 生成全0矩陣
>>> mat = np.zeros((3,2))
[[0. 0.]
[0. 0.]
[0. 0.]]
1: 使用+即可完成矩陣(對應元素處)相加,前提是兩個矩陣的行列數相同
>>> mata = np.ones((3,3))
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
>>> matb = np.arange(0,18,2)
>>> matb = matb.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matc = mata+matb
[[ 1. 3. 5.]
[ 7. 9. 11.]
[13. 15. 17.]]
1: 使用-即可完成矩陣(對應元素處)相減,前提是兩個矩陣的行列數相同
>>> mata = np.ones((3,3))
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
>>> matb = np.arange(0,18,2)
>>> matb = matb.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matc = matb-mata
[[-1. 1. 3.]
[ 5. 7. 9.]
[11. 13. 15.]]
1: 使用*或者np.dot()即可完成矩陣(對應元素處)相乘,前提是兩個矩陣的行列數相同
>>> mata = np.array([1,1,1,2,2,2,3,3,3]).reshape(3,3)
[[1 1 1]
[2 2 2]
[3 3 3]]
>>> matb = np.arange(0,18,2)
>>> matb = matb.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matc = np.multiply(mata,matb)
[[ 0 2 4]
[12 16 20]
[36 42 48]]
>>> matc = mata*matb
[[ 0 2 4]
[12 16 20]
[36 42 48]]
2: 使用np.dot()完成線代中的矩陣乘法,即n*m矩陣乘以m*p矩陣得到n*p的矩陣
>>> mata = np.array([1,2,3,1,2,3]).reshape(2,3)
[[1 2 3]
[1 2 3]]
>>> matb = np.arange(0,12,2)
>>> matb = matb.reshape(3,2)
[[ 0 2]
[ 4 6]
[ 8 10]]
>>> matc = np.dot(matb,mata)
[[ 2 4 6]
[10 20 30]
[18 36 54]]
3: 使用矩陣乘以乙個數字,即為各個矩陣元素乘以此數字
>>> mata = np.array([1,2,3,1,2,3]).reshape(2,3)
[[1 2 3]
[1 2 3]]
>>> matc = mata*2
[[2 4 6]
[2 4 6]]
1: 使用/即可完成矩陣(對應元素處)相除,前提是兩個矩陣的行列數相同
>>> mata = np.array([1,1,1,2,2,2,3,3,3]).reshape(3,3)
[[1 1 1]
[2 2 2]
[3 3 3]]
>>> matb = np.arange(0,18,2)
>>> matb = matb.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matc = matb/mata
[[0. 2. 4. ]
[3. 4. 5. ]
[4. 4.66666667 5.33333333]]
2: 可以先對被除的矩陣求倒數然後再乘以另外個矩陣,具體如下
>>> mata = np.array([1,1,1,2,2,2,3,3,3],dtype=np.float64).reshape(3,3)
[[1. 1. 1.]
[2. 2. 2.]
[3. 3. 3.]]
>>> matb = np.arange(0,18,2)
>>> matb = matb.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matc = matb*(mata**-1)
[[0. 2. 4. ]
[3. 4. 5. ]
[4. 4.66666667 5.33333333]]
1: 矩陣求和,sum(),行方向求和sum(axis=0),對列求和sum(axis=1)
>>> mata = np.array([1,1,1,2,2,2,3,3,3]).reshape(3,3)
[[1 1 1]
[2 2 2]
[3 3 3]]
>>> num = mata.sum()
18>>> rownum = mata.sum(axis=0)
[6 6 6]
>>> colnum = mata.sum(axis=1)
[3 6 9]
2: 求均值,mean(),行方向求均值mean(axis=0),列方向求均值mean(axis=1)
>>> mata = np.array([1,1,1,2,2,2,3,3,3]).reshape(3,3)
[[1 1 1]
[2 2 2]
[3 3 3]]
>>> mean = mata.mean()
2.0>>> rowmean = mata.mean(axis=0)
[2. 2. 2.]
>>> colmean = mata.mean(axis=1)
[1. 2. 3.]
3: 求最大值max(),最小值min()
>>> matb = np.arange(0,18,2)
>>> matb = matb.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> minnum = matb.min()
0>>> maxnum = matb.max()
164: 求最大值/最小值的索引,argmin()/argmax(),其中可使用axis來指定行/列方向
>>> matb = np.arange(0,18,2)
>>> matb = matb.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> minindex = matb.argmin(axis=0)
[0 0 0]
>>> maxindex = matb.argmax(axis=0)
[2 2 2]
5: np.where(),將符合條件的矩陣元素執行指定操作
例: 將matb中大於10的矩陣元素置為-1,其他則不變
>>> matb = np.arange(0,18,2)
>>> matb = matb.reshape(3,3)
[[ 0 2 4]
[ 6 8 10]
[12 14 16]]
>>> matc = np.where(matb>10,-1,matb)
[[ 0 2 4]
[ 6 8 10]
[-1 -1 -1]]
numpy矩陣的基礎操作
import numpy delimiter分隔符,dtype資料格式 word alcho numpy.genfromtxt d qiujiahao4.txt delimiter dtype str print type word alcho print word alcho 0 1 2 3 4 ...
Numpy矩陣基礎
1 與運算 vector numpy.array 5,10,15,20 equal to ten and five vector 5 vector 10 print equal to ten and five false false false false 與運算比較嚴格 乙個數不可能即等於5又等於...
numpy基礎操作
猴急先導入包 import numpy as npvct row np.array 1 2,3 行向量 vct col np.array 4 5 6 列向量matrix np.array 1,2,3 4,5,6 7,8,9 mtx np.mat 1,2,3 4,5,6 7,8,9 from scip...