本次使用的 Python 版本是 2.7.14
,Numpy 版本是 1.13.3
:
安装 Numpy:
1 | pip install numpy |
---|
首先创建一个普通的 list,然后转换成 numpy 的 array,并获取常用属性:
12345678910 | #coding : utf-8import numpy as nparr = np.array([[1,2,3], [4,5,6], [6,7,8]])print(arr)print "矩阵维数:",arr.ndim print "行列数:",arr.shapeprint "元素数:",arr.size |
---|
[[1 2 3]
[4 5 6]
[6 7 8]]
数组维数: 2
行列数: (3L, 3L)
元素数: 9
使用普通 List 转换成 Numpy Array:
1234 | #coding=utf-8import numpy as nparr1 = np.array([1, 2, 3]) #创建一维矩阵arr2 = np.array([1, 2, 3], [4, 5, 6]) #创建二维矩阵 |
---|
使用 Numpy 提供的创建指定形状的函数:
1234567891011 | #coding=utf-8import numpy as nparrZero = np.zeros((3, 4), dtype=np.float32) #生成一个三行四列的用 0 填充的矩阵,数据类型是 float32arrOne = np.ones((2, 3), dtype=np.float64) #生成一个两行三列的用 1 填充的矩阵,数据类型是 float64arrEmpty = np.empty((2,3)) #生成一个两行三列不初始化元素的矩阵arrRange = np.arange(10,20,2) # 生成一个有序的 一维矩阵 ( 从 10 - 20 步长为 2)arrRange2 = np.arange(0,20).reshape((5,4)) # 生成一个有序的多维数组 |
---|
[[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]
[ 0. 0. 0. 0.]]
[[ 1. 1. 1.]
[ 1. 1. 1.]]
[[ 2.44011712e-154 6.01347002e-154 4.57672080e-072]
[ 1.15298046e-259 6.01347002e-154 1.05147932e-153]]
[10 12 14 16 18]
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]
[12 13 14 15]
[16 17 18 19]]
基础运算
12345678910111213141516 | #coding=utf-8import numpy as npa = np.arange(10,60,10) # [10 20 30 40 50]b = np.arange(1,6) # [1 2 3 4 5]print "a = ",aprint "b = ",bprint "a + b = ",a + bprint "a - b = ",a - bprint "a * b = ",a * bprint "a / b = ",a / bprint "a的平方",a**2print "b < 3:", b < 3print "b == 3", b == 3 |
---|
a = [10 20 30 40 50]
b = [1 2 3 4 5]
a + b = [11 22 33 44 55]
a - b = [ 9 18 27 36 45]
a * b = [ 10 40 90 160 250]
a / b = [10 10 10 10 10]
a的平方 [ 100 400 900 1600 2500]
b < 3: [ True True False False False]
b == 3 [False False True False False]
高级运算
1234567891011121314151617181920212223 | #coding=utf-8import numpy as np# 随机生成一个两行三列的矩阵r = np.random.random((2,3))print 'np.random.random:\n', rprint "r.sum()", r.sum()print "r.min()", r.min()print "r.max()", r.max()printprint "r.sum(axis=1)",r.sum(axis=1) #计算每行的sum值print "r.min(axis=0)",r.min(axis=0) #计算每列的min值A = np.random.random((3,4))print 'A:',Aprint 'np.argmax():',A.argmax() #输出最大值的索引print 'np.argmin():',A.argmin() #输出最小值的索引print 'np.mean()',A.mean() #输出平均值print 'np.median()',np.median(A) #输出中位数 |
---|
np.random.random:
[[ 0.10887587 0.81277327 0.37954375]
[ 0.55984453 0.43466927 0.86671991]]
r.sum() 3.16242659254
r.min() 0.108875867717
r.max() 0.866719910302
r.sum(axis=1) [ 1.30119289 1.8612337 ]
r.min(axis=0) [ 0.10887587 0.43466927 0.37954375]
A: [[ 0.40985503 0.88362101 0.20321617 0.72286158]
[ 0.99780298 0.90575526 0.47214338 0.15433585]
[ 0.27101526 0.90980161 0.24162338 0.95733382]]
np.argmax(): 4
np.argmin(): 7
np.mean() 0.594113776444
np.median() 0.597502479295
[[ 2 3 4 5]
[ 6 7 8 9]
[10 11 12 13]]
[ 6. 7. 8. 9.]
[ 6. 7. 8. 9.]
将 n 维矩阵转换成 1 维矩阵
1234 | #coding=utf-8A = np.arange(3, 15).reshape(3, 4)print Aprint A.flatten() |
---|
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[ 3 4 5 6 7 8 9 10 11 12 13 14]
翻转矩阵
1234 | #coding=utf-8A = np.arange(3, 15).reshape(3, 4)print Aprint A.T |
---|
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
[[ 3 7 11]
[ 4 8 12]
[ 5 9 13]
[ 6 10 14]]
12345678910 | #coding=utf-8import numpy as npA = np.array([1,1,1])B = np.array([2,2,2])C = np.vstack((A,B)) #纵向合并D = np.hstack((A,B)) #横向合并print 'np.vstack((A,B)):\n',Cprintprint 'np.vstack((A,B)):\n',D |
---|
np.vstack((A,B)):
[[1 1 1]
[2 2 2]]
np.vstack((A,B)):
[1 1 1 2 2 2]
1234567891011 | #coding=utf-8import numpy as npA = np.arange(12).reshape((3,4))print 'A:\n',Aprintprint 'np.split(A,2,axis=1):\n',np.split(A,2,axis=1) #纵向分成两列printprint 'np.split(A,2,axis=0):\n',np.split(A,3,axis=0) #横向分成三列printprint 'np.array_split(A,2,axis=0) ',np.array_split(A,2,axis=0) #进行不等量分割` |
---|
A:
[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
np.split(A,2,axis=1):
[array([[0, 1],
[4, 5],
[8, 9]]), array([[ 2, 3],
[ 6, 7],
[10, 11]])]
np.split(A,2,axis=0):
[array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
np.array_split(A,2,axis=0):
[array([[0, 1, 2, 3],
[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]])]
12345678 | #coding=utf-8import numpy as npA = np.arange(3, 15).reshape(3, 4)# A.flat 是迭代器,迭代所有的元素for item in A.flat: print item |
---|
[[ 3 4 5 6]
[ 7 8 9 10]
[11 12 13 14]]
3
4
5
6
7
8
9
10
11
12
13
14
numpy 除了上述的基础操作之外,还有 ufunc、sort 等函数,功能强大,大家可以自行查阅文档学习,这里我不再花费时间讲解。
最后一共一份 Numpy 的中文翻译文档,版本是 1.11.3,提供给英文不太好的同学:NumPy v1.11手册