numpy.array学习笔记

来源:互联网 发布:散打教学软件 编辑:程序博客网 时间:2024/05/17 06:45

Numpy.array笔记

numpy.array[]里面的内容将会被默认设置为一致可有变量名.dtype来查看保存的具体类型.

import numpy
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]  #传入布尔值,索引返回真实值

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]]

vector = numpy.array([5,10,15,20])equal_to_ten_and_five = (vector == 10) | (vector == 5)print(equal_to_ten_and_five)print(vector[equal_to_ten_and_five])
[ True  True False False][ 5 10]
vector = numpy.array(["1","2","3"])print (vector.dtype)print (vector)vector = vector.astype(float)   #值类型的转换print(vector.dtype)print(vector)
<U1['1' '2' '3']float64[ 1.  2.  3.]

vector.min() #取极值
matrix = numpy.array([                [5,10,15],                [20,25,30],                [35,40,45]            ])matrix.sum(axis=1)  #axis=0则按列求和,等于1按行求和
array([ 30,  75, 120])

print (numpy.array(15))a = numpy.arange(15).reshape(3,5) #变成三行五列  也可以使用 a.shape = (3,5
a = np.floor(10*np.random.random((3,4)))print (a)print (a.ravel())a.shape = (6,2)print (a)print (a.T)

)print(a)
15[[ 0  1  2  3  4] [ 5  6  7  8  9] [10 11 12 13 14]]
a.ndim 2;  a.shape (3,5);  a.dtype.name 'int32';  a,size 15
numpy.zeros([3,4])  #初始化3行4列的全是0的元祖,类型默认为float
numpy.ones([3,4],dtype=numpy.int32)  #初始化3行4列的全是1的元祖,并且类型为int32
numpy.arange(10,30,5)  #从10到30 步长为5 左开右闭
array([10, 15, 20, 25])
numpy.random.random((2,3))   #范围默认-1~1  random的相关用法
array([[ 0.05902752,  0.92385352,  0.49967019],       [ 0.78749701,  0.23785766,  0.19547465]])

import numpy as npfrom numpy import pinp.linspace(0,2*pi,100)
#生成100个从0开始步长为2*pi的一维元祖

a = np.array([10,20,30,40])      #元祖的数学运算,但必须保持类型一致b = np.arange(4)                 print (a)print (b)c = a - bprint (c)c -= 1print (c)print (b**2)print (a < 35)
[10 20 30 40][0 1 2 3][10 19 28 37][ 9 18 27 36][0 1 4 9][ True  True  True False]

矩阵的乘法
a = np.array([[1,1],[0,1]])b = np.array([[2,0],[3,4]])print('a=',a)print('\n')print('b=',b)print('\n')print(a*b)print('\n')print(a.dot(b))print(np.dot(a,b))
a= [[1 1] [0 1]]b= [[2 0] [3 4]][[2 0] [0 4]][[5 4] [3 4]][[5 4] [3 4]]

a = np.floor(10*np.random.random((3,4)))print (a)print (a.ravel()) #将矩阵变成向量a.shape = (6,2)  #当矩阵元素已经确定时,reshape最后一个参数可以是-1让计算机自己操作print (a)print (a.T)
[[ 8.  7.  9.  5.] [ 6.  7.  8.  4.] [ 0.  4.  1.  1.]][ 8.  7.  9.  5.  6.  7.  8.  4.  0.  4.  1.  1.][[ 8.  7.] [ 9.  5.] [ 6.  7.] [ 8.  4.] [ 0.  4.] [ 1.  1.]][[ 8.  9.  6.  8.  0.  1.] [ 7.  5.  7.  4.  4.  1.]]

a = np.floor(10*np.random.random((2,2)))b = np.floor(10*np.random.random((2,2)))print (a)print (b)print (np.hstack((a,b)))  #hspilt 按行切分print (np.vstack((a,b)))  #vspilt 
[[ 6.  2.] [ 3.  1.]][[ 0.  9.] [ 2.  8.]][[ 6.  2.  0.  9.] [ 3.  1.  2.  8.]][[ 6.  2.] [ 3.  1.] [ 0.  9.] [ 2.  8.]]
a = np.floor(10*np.random.random((2,12)))print(a)print (np.hsplit(a,3))print (np.hsplit(a,(3,4)))   #按标志的列数进行切片a = np.floor(10*np.random.random((2,12)))np.vsplit(a.T,3)
[[ 6.  1.  8.  9.  9.  3.  0.  4.  6.  4.  3.  9.] [ 4.  5.  1.  9.  1.  1.  1.  1.  8.  7.  5.  0.]][array([[ 6.,  1.,  8.,  9.],       [ 4.,  5.,  1.,  9.]]), array([[ 9.,  3.,  0.,  4.],       [ 1.,  1.,  1.,  1.]]), array([[ 6.,  4.,  3.,  9.],       [ 8.,  7.,  5.,  0.]])][array([[ 6.,  1.,  8.],       [ 4.,  5.,  1.]]), array([[ 9.],       [ 9.]]), array([[ 9.,  3.,  0.,  4.,  6.,  4.,  3.,  9.],       [ 1.,  1.,  1.,  1.,  8.,  7.,  5.,  0.]])]
Out[85]:
[array([[ 1.,  9.],        [ 4.,  6.],        [ 2.,  3.],        [ 3.,  7.]]), array([[ 4.,  0.],        [ 6.,  4.],        [ 0.,  9.],        [ 5.,  6.]]), array([[ 8.,  7.],        [ 6.,  1.],        [ 2.,  4.],        [ 8.,  9.]])]

a = np.arange(12)b = aprint (b is a)b.shape = (3,4)print(a.shape)     #对b操作也会对a产生影响   参考C语言里的指针print(id(a))     print(id(b))
True(3, 4)20702279805762070227980576
#存储位置的地址一样
c = a.view()print (c is a)c.shape = (2,6)print (a.shape)c[0,4] = 1234print (a)print (id(a))print (id(c))
False(3, 4)[[   0    1    2    3] [1234    5    6    7]    #a的内容也变了,但是形状没变 [   8    9   10   11]]20702279805762070227982016
# 虽然地址不一样,但共用了同一个元素值

a = np.arange(12)c = a.copy()print (c is a)c.shape = (2,6)print (a.shape)c[0,3] = 1234print (a)print (c)print (id(a))print (id(c))
False(12,)[ 0  1  2  3  4  5  6  7  8  9 10 11][[   0    1    2 1234    4    5] [   6    7    8    9   10   11]]20702279828962070227982256 
#与上例比较
data = np.sin(np.arange((20)).reshape(5,4))print (data)ind = data.argmax(axis=0)print (ind)data_max = data[ind,range(data.shape[1])]print (data_max)
data = np.sin(np.arange((20)).reshape(5,4))
print (data)
ind = data.argmax(axis=0) #按列找最大值,输出索引
print (ind)
data_max = data[ind,range(data.shape[1])]
print (data_max)

a = np.arange(0,10,3)a.shape = (2,2)print (a)b = np.tile(a,(2,2))  #被操作数被视为一个元素而已print (b)
[[0 3] [6 9]][[0 3 0 3] [6 9 6 9] [0 3 0 3] [6 9 6 9]]
a = np.floor((10*np.random.random((2,4))))print (a)b = np.sort(a,axis=1)print (b)print (a)a.sort(axis=1)print (a)a = np.array([4,3,2,1])j = np.argsort(a)  #输出顺序的索引print (j)print (a[j])
[[ 8.  6.  9.  4.] [ 8.  9.  8.  1.]][[ 4.  6.  8.  9.] [ 1.  8.  8.  9.]][[ 8.  6.  9.  4.] [ 8.  9.  8.  1.]][[ 4.  6.  8.  9.] [ 1.  8.  8.  9.]][3 2 1 0][1 2 3 4]