numpy读取txt_Numpy库学习笔记2-Numpy数据存储与函数
CSV文件
Comma-Separated Value,逗号分隔值
CSV是一种文件格式,用来存储批量数据
文件格式如下:(逗号为英文输入法下,)
向Excel中导入CSV文件
将数据写入csv文件的方法
np.savetxt(frame, array, fmt="%.18e", delimiter=',')
- frame:文件、字符串或产生器,可以是.gz或.bz2的压缩文件
- array:存入文件的数组
- fmt:写入文件的格式
- delimiter:分割字符串,默认是任何空格,如CSV文件,需改为','
In [1]: import numpy as np
In [2]: a=np.arange(100).reshape(5,20)
In [3]: np.savetxt('C:Users阿迪Desktopa.csv.txt',a,fmt='%d',delimiter=',')
将CSV文件中的数据读入到一个numpy中的数组类型中
np.loadtxt(frame,dtype=np.float,delimiter=None,unpack=False)
- frame:文件、字符串或产生器,可以是.gz或.bz2的压缩文件
- dtype:数据类型
- delimiter:分割字符串,默认是任何空格
- unpack:如果True,读入属性将分别写入不同变量
In [5]: b=np.loadtxt('C:Users阿迪Desktopa.csv.txt',delimiter
...: =',')
In [6]: b
Out[6]:
array([[ 0., 1., 2., 3., 4., 5., 6., 7., 8., 9., 10., 11., 12.,
13., 14., 15., 16., 17., 18., 19.],
[20., 21., 22., 23., 24., 25., 26., 27., 28., 29., 30., 31., 32.,
33., 34., 35., 36., 37., 38., 39.],
[40., 41., 42., 43., 44., 45., 46., 47., 48., 49., 50., 51., 52.,
53., 54., 55., 56., 57., 58., 59.],
[60., 61., 62., 63., 64., 65., 66., 67., 68., 69., 70., 71., 72.,
73., 74., 75., 76., 77., 78., 79.],
[80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90., 91., 92.,
93., 94., 95., 96., 97., 98., 99.]])
CSV文件的局限性
CSV只能有效存储一维或二维数组
np.savetxt()/np.loadtxt()只能有效存取一维和二维数组
多维数据的存取
a.tofile(frame,sep='',format='%s')
- frame:文件、字符串
- sep:数据分割字符串,如果是空串,写入文件为二进制
- format:写入数据的格式
In [3]: a=np.arange(100).reshape(5,10,2)
In [4]: a.tofile("b.dat",sep=',',format='%d')
In [5]: a.tofile("b.dat",format='%d')
二进制文件
np.fromfile(frame,dtype=float,count=-1,sep='')
- frame:文件、字符串
- dtype:读取的数据类型
- count:读入元素个数,-1表示读入整个文件
- sep:数据分割字符串,如果是空串,写入文件为二进制
In [6]: a=np.arange(100).reshape(5,10,2)
In [7]: a.tofile("b.dat",sep=',',format='%d')
In [8]: c=np.fromfile("b.dat",dtype=np.int,sep=',')
In [9]: c
Out[9]:
array([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50,
51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67,
68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84,
85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99])
In [10]: c=c.reshape(5,10,2)
In [11]: c
Out[11]:
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15],
[16, 17],
[18, 19]],
[[20, 21],
[22, 23],
[24, 25],
[26, 27],
[28, 29],
[30, 31],
[32, 33],
[34, 35],
[36, 37],
[38, 39]],
[[40, 41],
[42, 43],
[44, 45],
[46, 47],
[48, 49],
[50, 51],
[52, 53],
[54, 55],
[56, 57],
[58, 59]],
[[60, 61],
[62, 63],
[64, 65],
[66, 67],
[68, 69],
[70, 71],
[72, 73],
[74, 75],
[76, 77],
[78, 79]],
[[80, 81],
[82, 83],
[84, 85],
[86, 87],
[88, 89],
[90, 91],
[92, 93],
[94, 95],
[96, 97],
[98, 99]]])
#二进制文件只需将sep去掉
a.tofile()和np.fromfile()的使用需要读取时知道存入文件时数组的维度和元素类型
可以通过元数据来存储额外信息
Numpy的便捷文件存取
NumPy 为 ndarray 对象引入了一个简单的文件格式:npy npy 文件用于存储重建 ndarray 所需的数据、图形、dtype 和其他信息
numpy.save(file, arr, allow_pickle=True)
- file:要保存的文件,扩展名为 .npy,如果文件路径末尾没有扩展名 .npy,该扩展名会被自动加上。
- arr: 要保存的数组
- allow_pickle: 可选,布尔值,允许使用 Python pickles 保存对象数组,Python 中的 pickle 用于在保存到磁盘文件或从磁盘文件读取之前,对对象进行序列化和反序列化。
读取时用np.load(file)
In [12]: a=np.arange(100).reshape(5,10,2)
In [13]: np.save("a.npy",a)
In [14]: b=np.load("a.npy")
In [15]: b
Out[15]:
array([[[ 0, 1],
[ 2, 3],
[ 4, 5],
[ 6, 7],
[ 8, 9],
[10, 11],
[12, 13],
[14, 15],
[16, 17],
[18, 19]],
[[20, 21],
[22, 23],
[24, 25],
[26, 27],
[28, 29],
[30, 31],
[32, 33],
[34, 35],
[36, 37],
[38, 39]],
[[40, 41],
[42, 43],
[44, 45],
[46, 47],
[48, 49],
[50, 51],
[52, 53],
[54, 55],
[56, 57],
[58, 59]],
[[60, 61],
[62, 63],
[64, 65],
[66, 67],
[68, 69],
[70, 71],
[72, 73],
[74, 75],
[76, 77],
[78, 79]],
[[80, 81],
[82, 83],
[84, 85],
[86, 87],
[88, 89],
[90, 91],
[92, 93],
[94, 95],
[96, 97],
[98, 99]]])
如果程序中间需要通过文件进行数据缓存,使用load/save方法
如果希望存储一个文件能够与其他的程序做数据交互和对接,用csv文件或tofile方法
Numpy库的随机数函数
- rand(d0,d1,..,dn) 根据d0-dn创建随机数数组,浮点数,[0,1),均匀分布
- randn(d0,d1,..,dn) 根据d0-dn创建随机数数组,标准正态分布
- randint(low[,high,shape]) 根据shape创建随机整数数组,范围时[low,high)
- seed(s) 随机数种子,s是给定的种子值
In [16]: a=np.random.rand(3,4,5)
In [17]: a
Out[17]:
array([[[0.17028468, 0.2994093 , 0.74881314, 0.65300107, 0.14082405],
[0.99980132, 0.49630372, 0.40444675, 0.63690841, 0.00940976],
[0.80312353, 0.16929515, 0.3984148 , 0.96987706, 0.49546325],
[0.34021906, 0.68546956, 0.06218532, 0.78027718, 0.71768477]],
[[0.14723833, 0.5967866 , 0.5102794 , 0.53493146, 0.43556592],
[0.39085088, 0.17951064, 0.69493391, 0.67344956, 0.94968574],
[0.64665837, 0.41006991, 0.68572048, 0.75245903, 0.05180048],
[0.69048286, 0.87879237, 0.94204279, 0.05494194, 0.81539204]],
[[0.07090875, 0.3998068 , 0.36881179, 0.95857677, 0.40232101],
[0.59877393, 0.48127404, 0.10879809, 0.92653146, 0.89552368],
[0.41769806, 0.0982495 , 0.91121966, 0.94234462, 0.59797372],
[0.93669388, 0.68792697, 0.29034668, 0.64822109, 0.63835499]]])
In [18]: a=np.random.randn(3,4,5)
In [19]: a
Out[19]:
array([[[-5.46473695e-01, -4.47442606e-01, 5.15961864e-02,
-1.75218421e+00, 7.76024895e-01],
[-7.63783966e-01, 2.66254186e+00, 1.27493230e+00,
-6.26763551e-01, -4.92136174e-01],
[ 2.19391987e-01, 3.85443682e-01, -3.72435280e-01,
1.10159614e+00, 1.19574111e+00],
[-1.51187021e-01, -1.71648343e+00, 4.42042164e-01,
-4.00961284e-01, 1.57676198e+00]],
[[ 2.41502338e-01, -1.69414028e-01, -1.22958980e-01,
3.90116673e-01, -3.75452784e-01],
[ 4.78169401e-01, 1.57573796e+00, -7.35533568e-01,
-1.95860944e-03, -2.16466178e+00],
[ 1.26319595e+00, -4.22475613e-01, 1.33181994e+00,
-1.39627223e+00, -4.75634307e-01],
[-1.46552573e-01, 9.18805832e-01, 6.63363852e-01,
5.34156672e-01, 1.06254450e+00]],
[[-6.36216256e-01, -2.27595619e+00, -1.25183474e-02,
-2.03484638e-01, -6.61740196e-01],
[ 1.58462291e-01, 1.85615913e+00, 1.61858119e+00,
1.37331770e+00, 1.88285426e+00],
[-7.28002055e-01, 9.00981073e-02, -5.44975256e-01,
-6.01417320e-01, 2.49419495e-01],
[ 6.61255507e-01, 9.68804291e-01, 4.82407993e-02,
1.67300056e-01, 6.72175866e-01]]])
In [20]: b=np.random.randint(100,200,(3,4))
In [21]: b
Out[21]:
array([[191, 101, 192, 112],
[194, 188, 122, 165],
[184, 115, 143, 129]])
In [22]: np.random.seed(10)
In [23]: np.random.randint(100,200,(3,4))
Out[23]:
array([[109, 115, 164, 128],
[189, 193, 129, 108],
[173, 100, 140, 136]])
In [24]: np.random.seed(10)
In [25]: np.random.randint(100,200,(3,4))
Out[25]:
array([[109, 115, 164, 128],
[189, 193, 129, 108],
[173, 100, 140, 136]])
- shuffle(a) 根据数组a的第1轴(最外层维度)进行随机排列,改变数组
- permutation(a) 根据数组a的第1轴产生一个新的乱序数组,不改变数组
- choice(a[,size,replace,p]) 从一维数组a中以概率p抽取元素,形成size形状新数组replace 表示是否可以重用元素,默认为False
In [26]: a=np.random.randint(100,200,(3,4))
In [27]: a
Out[27]:
array([[116, 111, 154, 188],
[162, 133, 172, 178],
[149, 151, 154, 177]])
In [28]: np.random.shuffle(a)
In [29]: a
Out[29]:
array([[116, 111, 154, 188],
[149, 151, 154, 177],
[162, 133, 172, 178]])
In [30]: np.random.shuffle(a)
In [31]: a
Out[31]:
array([[162, 133, 172, 178],
[116, 111, 154, 188],
[149, 151, 154, 177]])
In [32]: a=np.random.randint(100,200,(3,4))
In [33]: a
Out[33]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
In [34]: np.random.permutation(a)
Out[34]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
In [35]: np.random.permutation(a)
Out[35]:
array([[131, 157, 136, 127],
[113, 192, 186, 130],
[130, 189, 112, 165]])
In [36]: a
Out[36]:
array([[113, 192, 186, 130],
[130, 189, 112, 165],
[131, 157, 136, 127]])
In [37]: b=np.random.randint(100,200,(8,))
In [38]: b
Out[38]: array([123, 194, 111, 128, 174, 188, 109, 115])
In [39]: np.random.choice(b,(3,2))
Out[39]:
array([[111, 123],
[109, 115],
[123, 128]])
In [40]: np.random.choice(b,(3,2),replace=False)
Out[40]:
array([[188, 111],
[123, 115],
[174, 128]])
In [41]: np.random.choice(b,(3,2),p=b/np.sum(b))
Out[41]:
array([[194, 188],
[109, 111],
[174, 109]])
- uniform(low,high,size) 产生具有均匀分布的数组,low起始值,high结束值,size形状
- normal(loc,scale,size) 产生具有正态分布的数组,loc均值,scale标准差,size形状
- poisson(lam,size) 产生具有泊松分布的数组,lam随机时间发生率,size形状
In [42]: u=np.random.uniform(0,10,(3,4))
In [43]: u
Out[43]:
array([[9.83020867, 4.67403279, 8.75744495, 2.96068699],
[1.31291053, 8.42817933, 6.59036304, 5.95439605],
[4.36353698, 3.56250327, 5.87130925, 1.49471337]])
In [44]: n=np.random.normal(10,5,(3,4))
In [45]: n
Out[45]:
array([[ 8.17771928, 4.17423265, 3.28465058, 17.2669643 ],
[10.00584724, 9.94039808, 13.57941572, 4.07115727],
[ 6.81836048, 6.94593078, 3.40304302, 7.19135792]])
Numpy的统计函数
- sum(a,axis=None) 根据给定轴axis计算数组a相关元素之和
- mean(a,axis=None) 根据给定轴axis计算数组a相关元素的期望
- average(a,axis=None,weights=None) 根据给定轴axis计算数组a相关元素的加权平均值
- std(a,axis=None) 根据给定轴axis计算数组a相关元素的标准差
- var(a,axis=None) 根据给定轴axis计算数组a相关元素的方差
In [46]: a=np.arange(15).reshape(3,5)
In [47]: a
Out[47]:
array([[ 0, 1, 2, 3, 4],
[ 5, 6, 7, 8, 9],
[10, 11, 12, 13, 14]])
In [48]: np.sum(a)
Out[48]: 105
In [49]: np.mean(a,axis=1)
Out[49]: array([ 2., 7., 12.])
In [50]: np.mean(a,axis=0)
Out[50]: array([5., 6., 7., 8., 9.])
In [51]: np.average(a,axis=0,weights=[10,5,1])
Out[51]: array([2.1875, 3.1875, 4.1875, 5.1875, 6.1875])
In [52]: np.std(a)
Out[52]: 4.320493798938574
In [53]: np.var(a)
Out[53]: 18.666666666666668
- min(a)/max(a) 计算数组a中元素的最小值、最大值
- argmin(a)/argmax(a) 计算数组a中元素最小值、最大值的降一维后下标
- unravel_index(index,shape) 根据shape将一维下标index转换成多维小标
- ptp(a) 计算数组a中元素最大值与最小值的差
- median(a) 计算数组a中元素的中位数(中值)
In [54]: b=np.arange(15,0,-1).reshape(3,5)
In [55]: b
Out[55]:
array([[15, 14, 13, 12, 11],
[10, 9, 8, 7, 6],
[ 5, 4, 3, 2, 1]])
In [56]: np.max(b)
Out[56]: 15
In [57]: np.argmax(b)
Out[57]: 0
In [58]: np.unravel_index(np.argmax(b),b.shape)
Out[58]: (0, 0)
In [59]: np.ptp(b)
Out[59]: 14
In [60]: np.median(b)
Out[60]: 8.0
Numpy的梯度函数
梯度:连续值之间的变化率,即斜率
np.gradient(f) 计算数组f中元素的梯度,当f为多维时,返回每个维度梯度
通常用于图像处理,声音处理等多媒体运算时
In [61]: a=np.random.randint(0,20,(5))
In [62]: a
Out[62]: array([16, 10, 17, 0, 17])
In [63]: np.gradient(a)
Out[63]: array([-6. , 0.5, -5. , 0. , 17. ])
In [64]: b=np.random.randint(0,20,(5))
In [65]: b
Out[65]: array([ 9, 16, 9, 12, 18])
In [66]: np.gradient(b)
Out[66]: array([ 7. , 0. , -2. , 4.5, 6. ])
In [67]: c=np.random.randint(0,50,(3,5))
In [68]: c
Out[68]:
array([[30, 17, 17, 16, 0],
[31, 37, 9, 0, 38],
[22, 32, 2, 3, 31]])
In [69]: np.gradient(c)
Out[69]:
[array([[ 1. , 20. , -8. , -16. , 38. ],
[ -4. , 7.5, -7.5, -6.5, 15.5],
[ -9. , -5. , -7. , 3. , -7. ]]), #最外层维度的梯度
array([[-13. , -6.5, -0.5, -8.5, -16. ],
[ 6. , -11. , -18.5, 14.5, 38. ],
[ 10. , -10. , -14.5, 14.5, 28. ]])] #第二层维度的梯度