numpy学习笔记
import numpy as np
N维数组对象ndarray
np.array()生成一个ndarray数组
轴(axis)保存数据维度,秩(rank)轴的数量
ndarray对象的属性:
避免使用非同质的ndarray对象。
np.array(list/tuple,dtype=np.float32)
In [24]: np.arange(10) Out[24]: array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) In [25]: np.ones((3,4)) Out[25]: array([[ 1., 1., 1., 1.], [ 1., 1., 1., 1.], [ 1., 1., 1., 1.]]) In [26]: np.ones((3,4),dtype=np.int32) Out[26]: array([[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 1]], dtype=int32) In [28]: np.zeros((2,3)) Out[28]: array([[ 0., 0., 0.], [ 0., 0., 0.]]) In [29]: np.eye(5) Out[29]: array([[ 1., 0., 0., 0., 0.], [ 0., 1., 0., 0., 0.], [ 0., 0., 1., 0., 0.], [ 0., 0., 0., 1., 0.], [ 0., 0., 0., 0., 1.]])
In [40]: a=np.linspace(1,25,5) In [41]: a Out[41]: array([ 1., 7., 13., 19., 25.]) In [42]: b=np.linspace(1,25,5,endpoint=False) In [43]: b Out[43]: array([ 1. , 5.8, 10.6, 15.4, 20.2]) In [45]: np.concatenate((a,b)) Out[45]: array([ 1. , 7. , 13. , 19. , 25. , 1. , 5.8, 10.6, 15.4, 20.2])
ndarray数组维度的变换
In [47]: a=np.ones((2,3,4),dtype=np.int32) In [48]: a Out[48]: 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]]], dtype=int32) In [49]: a.reshape(4,6) Out[49]: 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]], dtype=int32) In [50]: a Out[50]: 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]]], dtype=int32) In [51]: a.resize((4,6)) In [52]: a Out[52]: 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]], dtype=int32) In [53]: a.flatten() Out[53]: array([1, 1, 1, ..., 1, 1, 1], dtype=int32) In [54]: a Out[54]: 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]], dtype=int32)
reshape()是另返回一个新的array,原array不变;resize()是改变原数组
数组类型的改变:astype()
ndarray数组向列表的转换:tolist()
In [2]: a=np.ones((2,3,4),dtype=np.int32) In [3]: a Out[3]: 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]]], dtype=int32) In [4]: b=a.astype(np.float) In [5]: b Out[5]: 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.]]])
数据的csv文件存取
np.savetxt(frame,array,fmt='%0.18e',delimiter=None)
In [22]: a=np.arange(15).reshape((3,5)) In [24]: np.savetxt('a.csv',a,fmt='%d',delimiter=',')
np.loadtxt(frame,dtype=np.float,delimiter=None,unpack=False)
In [25]: b=np.loadtxt('a.csv',delimiter=',') In [26]: b Out[26]: array([[ 0., 1., 2., 3., 4.], [ 5., 6., 7., 8., 9.], [ 10., 11., 12., 13., 14.]]) In [27]: b=np.loadtxt('a.csv',dtype=np.int,delimiter=',') In [28]: b Out[28]: array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
以上两个函数只能有效存取一维和二维数组,
对于多维数据的存取,可使用tofile()和fromfile(),这两个方法需要知道存入文件时数组的维度和元素类型。
In [29]: a=np.arange(100).reshape(5,2,10) In [30]: a Out[30]: array([[[ 0, 1, 2, ..., 7, 8, 9], [10, 11, 12, ..., 17, 18, 19]], [[20, 21, 22, ..., 27, 28, 29], [30, 31, 32, ..., 37, 38, 39]], [[40, 41, 42, ..., 47, 48, 49], [50, 51, 52, ..., 57, 58, 59]], [[60, 61, 62, ..., 67, 68, 69], [70, 71, 72, ..., 77, 78, 79]], [[80, 81, 82, ..., 87, 88, 89], [90, 91, 92, ..., 97, 98, 99]]]) In [32]: a.tofile('b.dat',sep=',',format="%d") In [37]: c=np.fromfile('b.dat',dtype=np.int,sep=',') In [38]: c Out[38]: array([ 0, 1, 2, ..., 97, 98, 99]) In [39]: c=np.fromfile('b.dat',dtype=np.int,sep=',').reshape(5,2,10) In [40]: c Out[40]: array([[[ 0, 1, 2, ..., 7, 8, 9], [10, 11, 12, ..., 17, 18, 19]], [[20, 21, 22, ..., 27, 28, 29], [30, 31, 32, ..., 37, 38, 39]], [[40, 41, 42, ..., 47, 48, 49], [50, 51, 52, ..., 57, 58, 59]], [[60, 61, 62, ..., 67, 68, 69], [70, 71, 72, ..., 77, 78, 79]], [[80, 81, 82, ..., 87, 88, 89], [90, 91, 92, ..., 97, 98, 99]]])
此外,还可以使用np.save(frame,array)、np.savez(frame,array)和np.load(frame)。这种方法很便捷,以npy为扩展名(压缩扩展名为npz),自动保存了维度信息。
In [42]: a=np.arange(20).reshape(2,2,5) In [44]: np.save('a.npy',a) In [47]: b=np.load('a.npy') In [48]: b Out[48]: array([[[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9]], [[10, 11, 12, 13, 14], [15, 16, 17, 18, 19]]])
np.random的随机数函数
In [2]: a=np.random.rand(2,3,4) In [3]: a Out[3]: array([[[ 0.3774548 , 0.38378479, 0.23116153, 0.62468074], [ 0.54844696, 0.07423047, 0.6164151 , 0.20719271], [ 0.70499044, 0.49245108, 0.41352731, 0.21213278]], [[ 0.6749494 , 0.10447893, 0.88275619, 0.85359191], [ 0.75249162, 0.18598287, 0.40681266, 0.55572018], [ 0.52925702, 0.05278294, 0.45759326, 0.20160628]]]) In [4]: b=np.random.randint(10,20,(2,3,4)) In [5]: b Out[5]: array([[[12, 16, 16, 13], [14, 10, 15, 16], [18, 18, 13, 16]], [[15, 19, 15, 13], [17, 17, 19, 17], [13, 19, 10, 12]]])
In [8]: u=np.random.uniform(0,10,(3,4)) In [9]: u Out[9]: array([[ 5.43561499, 3.36781091, 5.66832985, 8.51846059], [ 5.85174133, 3.46196561, 9.50727415, 0.95776365], [ 2.63730522, 8.21679612, 4.88207727, 1.80323693]]) In [10]: n=np.random.normal(10,5,(3,4)) In [11]: n Out[11]: array([[ 10.41948308, 3.57014388, 7.1968483 , 1.41896386], [ 6.86452724, 4.73895058, 13.04939017, 10.82616448], [ 7.92904101, 10.10575646, 11.47346598, 12.61651639]])