Numpy模块(一)
Numpy是什么?
它是一个开源科学计算库! 拥有丰富的数学函数,强大的多维数组以及优异的运算性能.
import numpy as np import datetime as dt n=100000 start=dt.datetime.now() A,B = [] , [] for i in range(n): A.append( i**2) B.append (i**3) C=[] for a,b in zip(A,B): C.append(a+b) print((dt.datetime.now()-start).microseconds) start=dt.datetime.now() # 170010 E=np.arange(n)**2+np.arange(n)**3 # E是一个数组 print((dt.datetime.now()-start).microseconds) # 1000
numpy计算的时间能比python缩短100倍左右
多维数组:
通过dtype参数和astype()方法都可以指定和修改元素的数据类型:
import numpy as np import datetime as dt a = np.array((10,23,32,23)) print(a) # [10 23 32 23] b = np.arange(1,10,2) print(b) # [1 3 5 7 9] c = np.array([ [1,2,3], [4,5,6] ]) print(c,type(c),type(c[0][0])) print(c.dtype) ''' [[1 2 3] [4 5 6]] <class 'numpy.ndarray'> <class 'numpy.int32'> int32 ''' d = np.array(['1','2','3']) print(type(d[0]),d.dtype) # <class 'numpy.str_'> <U1 (小端字节序,unicode编码,字符数为1) e = np.array(['1','2','3'],dtype=np.int32) print(e.dtype) # int32 f = e.astype(np.str_) print(f.dtype) # <U11 整型转字符串填零变长了
import numpy as np import datetime as dt i = np.array([ [np.arange(1,5),np.arange(5,9),np.arange(35,39)], [np.arange(13,17),np.arange(15,19),np.arange(45,49)] ]) print(i.shape) # (2, 3, 4) 两页三行四页
循环打印多维数组中的每一个元素:
其中i,j,k分别表示页行列,a[i][j][k]和a[i,j,k]等价
示例:
方式一:
import numpy as np a = np.array([('ABC',[1,2,3])],dtype = 'U3 , 3i4') print(a, a[0]['f0'], a[0]['f1']) # [('ABC', [1, 2, 3])] ABC [1 2 3]
方式二:
import numpy as np b = np.array([('ABC',[1,2,3])],dtype = [ ('name',np.str_,3),('score',np.int32,3)]) print(b[0]['name'] ,b[0]['score']) # ABC [1 2 3]
方式三:
import numpy as np c = np.array([('ABC',[1, 2, 3])],dtype = { 'names':['name','score'], 'formats':['U3' , '3i4']}) print(c) print(c[0]['name'] ,c[0]['score']) # [('ABC', [1, 2, 3])] ABC [1 2 3]
方式四:
import numpy as np d = np.array([('ABC',[1, 2, 3])],dtype = { 'name':('U3',0), 'score':('3i4',12) }) print(d) print(d[0]['name'] ,d[0]['score']) # [('ABC', [1, 2, 3])] ABC [1 2 3]
方式五:
import numpy as np e = np.array([0x1234],dtype=( '<u2',{'lo':('u1',0),'hi':('u1',1)})) print('{:x} {:x}'.format(e['lo'][0], e['hi'][0])) # 小端字节序,高字段低地址 34 12 e = np.array([0x1234],dtype=( '>u2',{'lo':('u1',0),'hi':('u1',1)})) print('{:x} {:x}'.format(e['lo'][0], e['hi'][0])) # 大端字节序,低字段高地址 12 34
一维数组的切分:
import numpy as np a = np.arange(1,10) print((a[:3])) # 1 2 3 print(a[3:6]) # 4 5 6 print(a[6:]) # 7 8 9 print(a[::-1]) # 9 8 7 6 5 4 3 2 1 print(a[:-4:-1]) # 9 8 7 print(a[-4:-7:-1]) # 6 5 4 print(a[...]) # 1 2 3 4 5 6 7 8 9 print(a[::]) # 1 2 3 4 5 6 7 8 9 print(a[:]) # 1 2 3 4 5 6 7 8 9
多维数组的切分:
b = np.arange(1,25).reshape(2,3,4) print(b) ''' [[[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] [[13 14 15 16] [17 18 19 20] [21 22 23 24]]] ''' print(b[:,0,0]) # 所有页的第0行第0列的数据 [ 1 13] print(b[0,:,:]) ''' 第0页所有数据 [[ 1 2 3 4] [ 5 6 7 8] [ 9 10 11 12]] ''' print(b[0,1,::2]) #第0页第1行 索引从0步长为2 拿所有数据 [5 7] print(b[:,:,1]) print(b[...,1]) ''' 所有页所有行的第1列 [[ 2 6 10] [14 18 22]] ''' print(b[:,1]) ''' 所有页的第1行的数据 [[ 5 6 7 8] [17 18 19 20]] ''' print(b[-1,1:,2:]) ''' 最后一页第一行及以后第二列及以后的所有数据 [[19 20] [23 24]]
视图与副本示例:
import numpy as np a = np.arange(1,9) b = a.reshape(2,4) c = a.reshape(2,2,2) print(b) print(c) ''' [[1 2 3 4] [5 6 7 8]] [[[1 2] [3 4]] [[5 6] [7 8]]] ''' d = c.ravel() e = c.flatten() print(d) print(e) ''' ravel 一维视图,数据仍是源数据 flatten 一维副本,数据为自己数据 [1 2 3 4 5 6 7 8] [1 2 3 4 5 6 7 8] ''' a += 10 print(d) print(e) ''' [11 12 13 14 15 16 17 18] [1 2 3 4 5 6 7 8] '''
补充:
多维副本:
f = b.reshape(2,2,2).copy()
一维数组转置两种处理方式:
import numpy as np
a = np.arange(1,10).reshape(3,3)
b = np.arange(11,20).reshape(3,3)
c = np.vstack((a,b))
print(c)
'''
垂直堆叠
[[ 1 2 3]
[ 4 5 6]
[ 7 8 9]
[11 12 13]
[14 15 16]
[17 18 19]]
'''
e,f = np.vsplit(c,2)
print(e,f,sep='\n')
'''
垂直拆分为2个相同维度的数组
[[1 2 3]
[4 5 6]
[7 8 9]]
[[11 12 13]
[14 15 16]
[17 18 19]]
'''
import numpy as np a = np.arange(1,10).reshape(3,3) b = np.arange(11,20).reshape(3,3) c = np.hstack((a,b)) print(c) ''' 水平堆叠 [[ 1 2 3 11 12 13] [ 4 5 6 14 15 16] [ 7 8 9 17 18 19]] ''' e,f = np.hsplit(c,2) print(e,f,sep='\n') ''' 水平拆分为2个相同维度的数组 [[1 2 3] [4 5 6] [7 8 9]] [[11 12 13] [14 15 16] [17 18 19]] '''
import numpy as np a = np.arange(1,10).reshape(3,3) b = np.arange(11,20).reshape(3,3) c = np.dstack((a,b)) print(a,b,sep='\n') print(c) ''' [[1 2 3] [4 5 6] [7 8 9]] [[11 12 13] [14 15 16] [17 18 19]] 前后每行元素一一对应 [[[ 1 11] [ 2 12] [ 3 13]] [[ 4 14] [ 5 15] [ 6 16]] [[ 7 17] [ 8 18] [ 9 19]]] ''' e,f = np.dsplit(c,2) print(e,f,sep='\n') ''' 拆分后不能恢复原来数组 [[[1] [2] [3]] [[4] [5] [6]] [[7] [8] [9]]] [[[11] [12] [13]] [[14] [15] [16]] [[17] [18] [19]]] '''
print(e.T[0].T,f.T[0].T,sep='\n')
'''
转置操作后能恢复为原来数组:
[[1 2 3]
[4 5 6]
[7 8 9]]
[[11 12 13]
[14 15 16]
[17 18 19]]
'''
import numpy as np a = np.arange(1,10) b = np.arange(11,20) c = np.row_stack((a,b)) print(c) ''' 与vstack处理的效果相同 [[ 1 2 3 4 5 6 7 8 9] [11 12 13 14 15 16 17 18 19]] ''' d = np.column_stack((a,b)) print(d) ''' 与hstack效果不同 [[ 1 11] [ 2 12] [ 3 13] [ 4 14] [ 5 15] [ 6 16] [ 7 17] [ 8 18] [ 9 19]] '''
import numpy as np a = np.array([ [1+1j, 2+2j, 3+3j], [4+4j, 5+5j, 6+6j], [7+7j, 8+8j, 9+9j] ]) print(a.dtype,a.dtype.str,a.dtype.char) # complex128 <c16 D 每个元素16的字节,128位二进制数 print(a.shape) #(3, 3) 三行三列 print(a.ndim) #每个元素是二维的 print(a.size,len(a)) #size指元素个数为9,len指数组行数3 print(a.itemsize,a.nbytes) #16 144 每个元素16的字节,总字节数为9*16=144 print(a.T) ''' 转置 [[1.+1.j 4.+4.j 7.+7.j] [2.+2.j 5.+5.j 8.+8.j] [3.+3.j 6.+6.j 9.+9.j]] ''' print(a.real,a.imag,sep='\n') ''' 实部,虚部各自打印 [[1. 2. 3.] [4. 5. 6.] [7. 8. 9.]] [[1. 2. 3.] [4. 5. 6.] [7. 8. 9.]] ''' for elem in a.flat: print(elem) ''' 迭代取值 (1+1j) (2+2j) (3+3j) (4+4j) (5+5j) (6+6j) (7+7j) (8+8j) (9+9j) ''' print(a.flat[[1,3,5]]) a.flat[[2,4,6]] = 0 print(a) ''' flat迭代器可按索引取值 [2.+2.j 4.+4.j 6.+6.j] 也可以修改值 [[1.+1.j 2.+2.j 0.+0.j] [4.+4.j 0.+0.j 6.+6.j] [0.+0.j 8.+8.j 9.+9.j]] '''
数组与列表的区别与转换:
import numpy as np def fun(a,b): a.append(b) return a x = np.array([10,20,30]) y = 40 x = np.array(fun(x.tolist(),y)) print(x) # [10 20 30 40] 数组.tolist()转换成列表就可以append了 x=np.append(x,50) print(x) # [10 20 30 40 50] 数组可用append函数添加数据,但是必须用x接受方可打印