python科学计算库numpy的使用

来源:互联网 发布:spss数据输入 编辑:程序博客网 时间:2024/05/22 03:25
numpy,主要用来做矩阵运算,在使用前要先保证numpy库已经安装好了。


1、基础使用

从文件加载数据,使用 numpy.genfromtxt加载,第一个参数文件名,delimiter指定分隔符,dtype指定读入的数据类型。

返回结果ndarray格式,即一个矩阵结构,这结构非常的常用。

要查看帮助可以使用命令查看,如:print(help(numpy.genfromtxt))

import numpyworld_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)
ndarray中存放的数据类型都是一样的,如果改变一个其它的也会跟着改变,这与python的list有所不同。
numbers = numpy.array([1, 2, 3, 4])numbers.dtypenumbers = numpy.array([1, 2, 3, '4'])numbers.dtype
dtype('int32')
dtype('<U11')
切片,取0-2下标范围的数据
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())
5205012.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 #别名 npa=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 pinp.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 aprint 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 = ab.shape = 3,4print(a.shape)print(id(a))print(id(b))
(3, 4)
19707827635681970782763568
ID一样,说明都是指向的同一地址
c = a.view()c is ac.shape = 2,6#print a.shapec[0,4] = 1234a
array([[   0,    1,    2,    3],       [1234,    5,    6,    7],       [   8,    9,   10,   11]])
虽然ID不一样了,但矩阵中的值是指向的同一地址
d = a.copy() d is ad[0,0] = 9999print 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 npdata = 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]