numpy数组存取操作方法

自己在b站照着学习的,方便使用和复习
#创建一个数组
 import numpy as np
 
 a = np.arange(100).reshape((5,20))

a
Out[4]: 
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只能存取一维数组和二维数组		
#对数组a进行保存,其中%d表示format格式为整数,也可以是浮点数,%.1f		
np.savetxt('E:/pyfile_test/a.csv',a,fmt='%d',delimiter=',')

#将刚导出的csv文件导入到数组b中,文件是浮点数类型,指定导入的数据是int
b=np.loadtxt('E:/pyfile_test/a.csv',dtype=np.int,delimiter=',')

b
Out[18]: 
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]])
		
		
		
#多维数组存储
#生成一个bat文件--批处理文件
c=np.arange(100).reshape((5,10,2))

c.tofile('E:/pyfile_test/c.bat',sep=',',format='%d')

#不指定分隔符默认生成了二进制文件,如果数据备份的话选择二进制,省空间
c.tofile('E:/pyfile_test/b.bat',format='%d')

#将多维数组导入,维度信息丢失,需要自己reshape
d = np.fromfile('E:/pyfile_test/c.bat',sep=',',dtype=np.int)

d
Out[30]: 
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])
	   
#reshape	   
d = np.fromfile('E:/pyfile_test/c.bat',sep=',',dtype=np.int).reshape((5,10,2))

d
Out[32]: 
array([[[ 0,  1],
        [ 2,  3],
        [ 4,  5],
        [ 6,  7],
        [ 8,  9],
        [10, 11],
        [12, 13],
        [14, 15],
        [16, 17],
        [18, 19]],
........
        [[80, 81],
        [82, 83],
        [84, 85],
        [86, 87],
        [88, 89],
        [90, 91],
        [92, 93],
        [94, 95],
        [96, 97],
        [98, 99]]])
		
#快捷存储多维数据并读取
#np.savez可以存储压缩形式的npz格式文件
#npy文件格式中第一行存取了数组的元数据

 np.save('E:/pyfile_test/c_file.npy',c)#将数组c进行存储
 
 d=np.load('E:/pyfile_test/c_file.npy')

所以最便捷的方法是用save和load存取数组,不论什么维度的数据都可以


#生成一个随机数组,维度为3,4,5,数组的元素为0-1之间的随机数
#有rand,randn--正态分布,randint随机分布整数
a=np.random.rand(3,4,5)

a
Out[62]: 
array([[[0.48187563, 0.46153222, 0.73810662, 0.45273689, 0.85285088],
        [0.88424899, 0.57973895, 0.95086275, 0.78160342, 0.43218113],
        [0.82035264, 0.88500205, 0.50907467, 0.08812894, 0.36274144],
        [0.9908143 , 0.84024623, 0.70170702, 0.56857565, 0.86324834]],

       [[0.18402309, 0.8691892 , 0.38078245, 0.64993282, 0.7414359 ],
        [0.7524337 , 0.79894529, 0.11541167, 0.79763221, 0.16383416],
        [0.96062384, 0.95972972, 0.88401711, 0.40087081, 0.43996838],
        [0.51550076, 0.20407239, 0.3017933 , 0.23495618, 0.94939913]],

       [[0.82162149, 0.57189753, 0.81323194, 0.48882077, 0.9163273 ],
        [0.38902703, 0.2026601 , 0.77406103, 0.30443894, 0.16733822],
        [0.8302778 , 0.43731814, 0.16955258, 0.4793714 , 0.45536927],
        [0.97938068, 0.69132676, 0.41364171, 0.32076053, 0.08049487]]])
      
#生成100-200的随机整数,维度是2,2,5	  
an=np.random.randint(100,200,(2,2,5))

an
Out[68]: 
array([[[155, 114, 120, 129, 145],
        [100, 195, 163, 188, 132]],

       [[117, 105, 113, 168, 128],
        [100, 178, 104, 102, 105]]])

#随机数种子,方便测试得出相同的随机数
np.random.randint(100,200,(2,2,5))
Out[73]: 
array([[[109, 115, 164, 128, 189],
        [193, 129, 108, 173, 100]],

       [[140, 136, 116, 111, 154],
        [188, 162, 133, 172, 178]]])

np.random.seed(10)

np.random.randint(100,200,(2,2,5))
Out[75]: 
array([[[109, 115, 164, 128, 189],
        [193, 129, 108, 173, 100]],

       [[140, 136, 116, 111, 154],
        [188, 162, 133, 172, 178]]])
		
#随机更换第一个维度的顺序
#shuffle是彻底改变数组
#permutation不改变原来数组的顺序
a = np.random.randint(100,200,(2,2,5))
a
Out[84]: 
array([[[149, 151, 154, 177, 169],
        [113, 125, 113, 192, 186]],

       [[130, 130, 189, 112, 165],
        [131, 157, 136, 127, 118]]])

np.random.shuffle(a)

a
Out[86]: 
array([[[130, 130, 189, 112, 165],
        [131, 157, 136, 127, 118]],

       [[149, 151, 154, 177, 169],
	   
	   
	   
#生成一个一维数组
d=np.random.randint(100,200,(8,))

d
Out[88]: array([123, 194, 111, 128, 174, 188, 109, 115])

#用choice重新选取一个新的数组,此时数据有重复,123
 np.random.choice(d,(3,2))
Out[89]: 
array([[111, 123],
       [109, 115],
       [123, 128]])
	   
#加上replace函数结果为不重复
np.random.choice(d,(3,2),replace=False)
Out[90]: 
array([[188, 111],
       [123, 115],
       [174, 128]])
#设置一个概率,数据越大,几率越大,概率为p
np.random.choice(d,(3,2),replace=False,p=d/np.sum(d))
Out[93]: 
array([[194, 188],
       [109, 111],
       [174, 115]])
	   
	#均匀分布   
np.random.uniform(100,200,(3,4))
Out[94]: 
array([[146.7403279 , 187.57444947, 129.60686989, 113.12910529],
       [184.28179335, 165.90363039, 159.5439605 , 143.63536983],
       [135.62503268, 158.71309253, 114.9471337 , 117.12385982]])

#正态分布

np.random.normal(100,200,(3,4))
Out[96]: 
array([[ 431.22943329, -146.91374093, -111.53694493,  127.68279692],
       [  82.81999126,   72.75389809, -127.42304247,  227.62716139],
       [   1.67921022, -149.25700191,   24.01358452,   45.74837317]])
 #泊松分布

np.random.poisson(100,(3,4))
Out[98]: 
array([[102, 102,  96, 110],
       [103, 100,  95, 105],
       [108,  97, 124,  85]])

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值