numpy,主要用来做矩阵运算,在使用前要先保证numpy库已经安装好了。
1、基础使用
从文件加载数据,使用 numpy.genfromtxt加载,第一个参数文件名,delimiter指定分隔符,dtype指定读入的数据类型。
返回结果ndarray格式,即一个矩阵结构,这个结构非常的常用。
要查看帮助可以使用命令查看,如:print(help(numpy.genfromtxt))
import numpy
world_alcohol = numpy.genfromtxt("world_alcohol.txt", delimiter=",", dtype=str)
print(type(world_alcohol))
<class 'numpy.ndarray'>
2、数组结构和基础操作
定义一维矩阵和二维矩阵,并打印出矩阵的结构。
vector = numpy.array([1, 2, 3, 4]) print(vector.shape) matrix = numpy.array([[5, 10, 15], [20, 25, 30]]) print(matrix.shape)
(4,) (2, 3)
切片,取0-2下标范围的数据
ndarray中存放的数据类型都是一样的,如果改变一个其它的也会跟着改变,这与python的list有所不同。
dtype('int32')numbers = numpy.array([1, 2, 3, 4]) numbers.dtype numbers = numpy.array([1, 2, 3, '4']) numbers.dtype
dtype('<U11')
vector = numpy.array([5, 10, 15, 20]) print(vector[0:3])
[ 5 10 15]
取列,获取第二列的数据
matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) print(matrix[:,1])
[10 25 40]
取指定的行列范围值
matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) print(matrix[1:3,0:2])
[[20 25] [35 40]]
值判断操作,查找等于10的值
vector = numpy.array([5, 10, 15, 20]) vector == 10
array([False, True, False, False], dtype=bool)
通过判断结果,获取值,这里的equal_to_ten也可以作为index去获取数值vector = numpy.array([5, 10, 15, 20]) equal_to_ten = (vector == 10) print(equal_to_ten) print(vector[equal_to_ten])
[False True False False] [10]
查找第二列值为25的,并取出这一行
matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) second_column_25 = (matrix[:,1] == 25) print(second_column_25) print(matrix[second_column_25, :])
[False True False] [[20 25 30]]
类型转换,将整个矩阵转换为float类型
vector = numpy.array(["1", "2", "3"]) vector = vector.astype(float) print(vector.dtype) print(vector)
float64 [ 1. 2. 3.]
最大值,最小值,合计值,平均值
vector = numpy.array([5, 10, 15, 20]) print(vector.min()) print(vector.max()) print(vector.sum()) print(vector.mean())
5 20 50 12.5
矩阵求合,通过axis指定纬度,0为按列,1为按行
matrix = numpy.array([ [5, 10, 15], [20, 25, 30], [35, 40, 45] ]) print(matrix.sum(axis=0)) print(matrix.sum(axis=1))
[60 75 90] [ 30 75 120]
3、常用函数构造矩阵
import numpy as np #别名 np a=np.arange(15) #构造数组,内有15个元素 b=a.reshape(3, 5) #向量转换为矩阵 print(a) print(b)
[ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14] [[ 0 1 2 3 4] [ 5 6 7 8 9] [10 11 12 13 14]]
b.shape #矩阵结构 = (3, 5)
b.ndim #矩阵纬度 = 2
b.dtype.name #矩阵元素类型 = 'int32'
b.size #元素数量 = 15
矩阵形状转换
a = np.floor(10*np.random.random((3,4))) #10以内的随机数向下取整,3行4列的随机矩阵 print a.ravel() #二维矩阵拉平为向量 a.shape = (6, 2) #重新变为6行2列的矩阵 print a.T #行列转置,变为2行6列 a.reshape(3,-1) #行列值使用-1时为自动计算值,这里就会自动取4
初始化0值矩阵,浮点型,传入参数为元祖数据格式
np.zeros ((3,4))
array([[ 0., 0., 0., 0.], [ 0., 0., 0., 0.], [ 0., 0., 0., 0.]])
初始化1值矩阵,三维,整形
np.ones( (2,3,4), dtype=np.int32 )
array([[[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]]])
构造指定范围值的数组np.arange( 10, 30, 5 ) #起始值,结束值,间隔
array([10, 15, 20, 25])
随机模块,生成一个2行3列的随机值矩阵
np.random.random((2,3))
array([[ 0.40130659, 0.45452825, 0.79776512], [ 0.63220592, 0.74591134, 0.64130737]])
平均间隔矩阵值,根据指定区间,生成指定数量的数值
from numpy import pi np.linspace( 0, 2*pi, 100 ) #起始值,结束值,取多少个 np.sin(np.linspace( 0, 2*pi, 100 )) #对取值结果进行sin操作,其它类似的还有np.exp(次幂),np.sqrt(平方根)
数学运算a = np.array( [20,30,40,50] ) #[20 30 40 50] b = np.arange( 4 ) #[0 1 2 3] c = a-b #[20 29 38 47] b**2 #[0 1 4 9] a<35 #[ True True False False]
矩阵乘法,注意*乘与dot乘的区别,*乘对应位置的相乘,dot乘行乘列,A.dot(B) = np.dot(A, B)A = np.array( [[1,1], [0,1]] ) B = np.array( [[2,0], [3,4]] ) print(A) print("--------") print(B) print("--------") print(A*B) print("--------") print(A.dot(B)) print("--------") print(np.dot(A, B))
[[1 1] [0 1]] -------- [[2 0] [3 4]] -------- [[2 0] [0 4]] -------- [[5 4] [3 4]] -------- [[5 4] [3 4]]
矩阵拼接a=np.floor(10*np.random.random((2,2))) b=np.floor(10*np.random.random((2,2))) print(a) print('---') print(b) print('---') print(np.hstack((a,b))) #横向拼接 print('---') print(np.vstack((a,b))) #竖向拼接
[[ 3. 9.] [ 3. 9.]] --- [[ 7. 7.] [ 7. 3.]] --- [[ 3. 9. 7. 7.] [ 3. 9. 7. 3.]] --- [[ 3. 9.] [ 3. 9.] [ 7. 7.] [ 7. 3.]]
矩阵切分
a = np.floor(10*np.random.random((2,12))) print a print np.hsplit(a,3) #竖向切分,将a平均切为3份 print np.hsplit(a,(3,4)) #竖向切分,从指定的第3列,第4列处切分为3份 a = np.floor(10*np.random.random((12,2))) print(a) np.vsplit(a,3) #横向切分
4、复制操作a = np.arange(12) b = a b.shape = 3,4 print(a.shape) print(id(a)) print(id(b))
(3, 4)
1970782763568 1970782763568
ID一样,说明都是指向的同一地址
c = a.view() c is a c.shape = 2,6 #print a.shape c[0,4] = 1234 a
array([[ 0, 1, 2, 3], [1234, 5, 6, 7], [ 8, 9, 10, 11]])
虽然ID不一样了,但矩阵中的值是指向的同一地址
d = a.copy() d is a d[0,0] = 9999 print d print a
[[9999 1 2 3] [1234 5 6 7] [ 8 9 10 11]] [[ 0 1 2 3] [1234 5 6 7] [ 8 9 10 11]]
矩阵和当中的值都使用了独立的地址
5、其它获取最大值
import numpy as np data = np.sin(np.arange(20)).reshape(5,4)#生成随机矩阵 print(data) ind = data.argmax(axis=0) #axis=0按列获取索引值 print(ind) data_max = data[ind, range(data.shape[1])] #获取值 print(data_max)
[[ 0. 0.84147098 0.90929743 0.14112001] [-0.7568025 -0.95892427 -0.2794155 0.6569866 ] [ 0.98935825 0.41211849 -0.54402111 -0.99999021] [-0.53657292 0.42016704 0.99060736 0.65028784] [-0.28790332 -0.96139749 -0.75098725 0.14987721]] [2 0 3 1] [ 0.98935825 0.84147098 0.99060736 0.6569866 ]
矩阵扩展
a = np.arange(0, 40, 10) b = np.tile(a, (3, 5)) #扩展 行3倍,列5倍 print b
[ 0 10 20 30] [[ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30] [ 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30 0 10 20 30]]
排序
a = np.array([[4, 3, 5], [1, 2, 1]]) b = np.sort(a, axis=1) #按行排序 结果赋值给b #print(b) a.sort(axis=1) #直接对a排序 print(a) a = np.array([4, 3, 1, 2]) j = np.argsort(a) #排序索引 print(j) #索引 print(a[j]) #排序后的值
[[3 4 5] [1 1 2]] [2 3 1 0] [1 2 3 4]