Numpy
import numpy as np ar = np.array([1,2,3,4,5,6]) #一维数组就是1行 print(ar, type(ar), ar.dtype) 打印: [1 2 3 4 5 6] <class ‘numpy.ndarray‘> int32 ar = np.array([[1,2,3,4,5,6], [2,3,4,5,6,7]]) #二维数组就是1行1列 print(ar) 打印: [[1 2 3 4 5 6] [2 3 4 5 6 7]] ar = np.array([[1,2,3,4,5,6], [2,3,4,5,6,7], [3,4,5,6,7,8]]) #3行6列的二维数组 print(ar) 打印: [[1 2 3 4 5 6] [2 3 4 5 6 7] [3 4 5 6 7 8]] ar = np.array([[[1,2,3,4,5,6], [2,3,4,5,6,7], [3,4,5,6,7,8]], [[1,2,3,4,5,6], [2,3,4,5,6,7], [3,4,5,6,7,8]]]) #2个二维数组或多个二维数组即三维数组 print(ar) 打印: [[[1 2 3 4 5 6] [2 3 4 5 6 7] [3 4 5 6 7 8]] [[1 2 3 4 5 6] [2 3 4 5 6 7] [3 4 5 6 7 8]]] ar = np.array([[1,2,3,4,5,6], [2,3,4,5,6,7], [3,4,5,6,7,8]]) #3行6列的二维数组 #ar = np.array([[[1,2,3,4,5,6], [2,3,4,5,6,7], [3,4,5,6,7,8]], [[1,2,3,4,5,6], [2,3,4,5,6,7], [3,4,5,6,7,8]]]) #2个二维数组或多个二维数组即三维数组 print(ar, type(ar), ar.dtype) print(ar.ndim) #输出数组维度的个数(轴数),或者说“秩”,维度的数量也称rank print(ar.shape) ## 数组的维度,对于n行m列的数组,shape为(n,m) print(ar.size) # 数组的元素总数,对于n行m列的数组,元素总数为n*m print(ar.itemsize) # 数组中每个元素的字节大小,int32l类型字节为4,float64的字节为8 print(ar.data) # 包含实际数组元素的缓冲区,由于一般通过数组的索引获取元素,所以通常不需要使用这个属性。 ar # 交互方式下输出,会有array(数组) 打印: [[1 2 3 4 5 6] [2 3 4 5 6 7] [3 4 5 6 7 8]] <class ‘numpy.ndarray‘> int32 2 (3, 6) 18 4
array([[1, 2, 3, 4, 5, 6],
[2, 3, 4, 5, 6, 7],
[3, 4, 5, 6, 7, 8]])
数组创建
>>> ar1 = np.array(range(10)) #整型 >>> ar1 array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]) >>> print(ar1) [0 1 2 3 4 5 6 7 8 9] >>> ar2 = np.arange(10) >>> print(ar2) [0 1 2 3 4 5 6 7 8 9] >>> ar3 = np.array([1,2,3,4,5]) >>> print(ar3,type(ar3),ar3.dtype) [1 2 3 4 5] <class ‘numpy.ndarray‘> int32 >>> ar3 = np.array([1,2,3.14,4,5.20]) #浮点型 >>> print(ar3,type(ar3),ar3.dtype) [1. 2. 3.14 4. 5.2 ] <class ‘numpy.ndarray‘> float64 >>> ar4 = np.array([[1,2,3,4,5],[5,6,7,8,9]]) >>> print(ar4) [[1 2 3 4 5] [5 6 7 8 9]] >>> ar4 = np.array([[1,2,3,4,5],[5,6,7,8,9,10]]) #嵌套序列不一样就会变成一维数组 >>> print(ar4,type(ar4),ar4.dtype,ar4.ndim) [list([1, 2, 3, 4, 5]) list([5, 6, 7, 8, 9, 10])] <class ‘numpy.ndarray‘> object 1 >>> ar4 = np.array([[1,2,3,4,5],[‘a‘,‘b‘,‘c‘,‘d‘,‘e‘]]) >>> print(ar4) [[‘1‘ ‘2‘ ‘3‘ ‘4‘ ‘5‘] [‘a‘ ‘b‘ ‘c‘ ‘d‘ ‘e‘]] >>> print(ar4,ar4.ndim) [[‘1‘ ‘2‘ ‘3‘ ‘4‘ ‘5‘] [‘a‘ ‘b‘ ‘c‘ ‘d‘ ‘e‘]] 2 >>> ar4 = np.array([[1,2,3],(‘a‘,‘b‘,‘c‘)]) ######二维数组,嵌套序列,可以是列表可以是元组。 >>> print(ar4, ar4.shape, ar4.ndim, ar4.size) [[‘1‘ ‘2‘ ‘3‘] [‘a‘ ‘b‘ ‘c‘]] (2, 3) 2 6 >>> >>> print(np.random.rand(10).reshape(2,5)) ###随机数组,10个0-1的数字,2乘以5 [[0.927168 0.77335508 0.0120362 0.1504996 0.93548895] [0.34811207 0.41284246 0.75599419 0.53838818 0.74908313]] >>>
>>> print(np.linspace(10,20,num=20)) #10-19 [10. 10.52631579 11.05263158 11.57894737 12.10526316 12.63157895 13.15789474 13.68421053 14.21052632 14.73684211 15.26315789 15.78947368 16.31578947 16.84210526 17.36842105 17.89473684 18.42105263 18.94736842 19.47368421 20. ] >>> print(np.linspace(10,20,num=21)) [10. 10.5 11. 11.5 12. 12.5 13. 13.5 14. 14.5 15. 15.5 16. 16.5 17. 17.5 18. 18.5 19. 19.5 20. ] >>> print(np.linspace(10,20,num=21,endpoint = False)) #默认是True, False是最后一个值不包含; [10. 10.47619048 10.95238095 11.42857143 11.9047619 12.38095238 12.85714286 13.33333333 13.80952381 14.28571429 14.76190476 15.23809524 15.71428571 16.19047619 16.66666667 17.14285714 17.61904762 18.0952381 18.57142857 19.04761905 19.52380952] >>> print(np.linspace(10,20,num=21,endpoint = True)) #跟上边一样了,可以省略不写 [10. 10.5 11. 11.5 12. 12.5 13. 13.5 14. 14.5 15. 15.5 16. 16.5 17. 17.5 18. 18.5 19. 19.5 20. ] >>> s = np.linspace(10,20,num=21,retstep = True) >>> print(s,type(s)) (array([10. , 10.5, 11. , 11.5, 12. , 12.5, 13. , 13.5, 14. , 14.5, 15. , 15.5, 16. , 16.5, 17. , 17.5, 18. , 18.5, 19. , 19.5, 20. ]), 0.5) <class ‘tuple‘> >>> print(s[0]) [10. 10.5 11. 11.5 12. 12.5 13. 13.5 14. 14.5 15. 15.5 16. 16.5 17. 17.5 18. 18.5 19. 19.5 20. ]>>> print(np.linspace(10,20,num=21,retstep = False)) #默认为False [10. 10.5 11. 11.5 12. 12.5 13. 13.5 14. 14.5 15. 15.5 16. 16.5 17. 17.5 18. 18.5 19. 19.5 20. ]
创建数组:zeros()/zeros_like()/ones()/ones_like()
# numpy.zeros(shape, dtype=float, order=‘C‘):返回给定形状和类型的新数组,用零填充。 # shape:数组纬度,二维以上需要用(),且输入参数为整数 # dtype:数据类型,默认numpy.float64 # order:是否在存储器中以C或Fortran连续(按行或列方式)存储多维数据。
>>> print(np.zeros(5)) [0. 0. 0. 0. 0.] >>> print(np.zeros(10)) [0. 0. 0. 0. 0. 0. 0. 0. 0. 0.] >>> print(np.zeros((3,5))) [[0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.] [0. 0. 0. 0. 0.]] >>> print(np.zeros((3,5), dtype=np.int)) [[0 0 0 0 0] [0 0 0 0 0] [0 0 0 0 0]] >>> ar = np.array([list(range(10)),list(range(10,20))]) >>> print(ar) [[ 0 1 2 3 4 5 6 7 8 9] [10 11 12 13 14 15 16 17 18 19]] >>> print(np.zeros_like(ar)) [[0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0]] >>> ar2 = np.ones(9) >>> ar3 = np.ones((2,3,4)) >>> ar4 = np.ones_like(ar3) >>> print(ar2) [1. 1. 1. 1. 1. 1. 1. 1. 1.] >>> print(ar3) [[[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]]] >>> print(ar4) [[[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]] [[1. 1. 1. 1.] [1. 1. 1. 1.] [1. 1. 1. 1.]]] >>> >>>
# 创建一个正方的N*N的单位矩阵,对角线值为1,其余为0
>>> print(np.eye(5)) [[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.]]
Numpy通用函数
数组形状:.T/.reshape()/.resize()
>>> import numpy as np >>> ar1 = np.arange(10)>>> print(ar1,‘\n‘,ar1.T) [0 1 2 3 4 5 6 7 8 9] [0 1 2 3 4 5 6 7 8 9] >>> ar2 = np.ones((5,2)) >>> print(ar2,‘\n‘,ar2.T) [[1. 1.] [1. 1.] [1. 1.] [1. 1.] [1. 1.]] [[1. 1. 1. 1. 1.] [1. 1. 1. 1. 1.]] >>># .T方法:转置,例如原shape为(3,4)/(2,3,4),转置结果为(4,3)/(4,3,2) → 所以一维数组转置后结果不变 >>> ar3 = ar1.reshape(2,5) #用法一,直接将已有数组改变形状。 >>> print(ar1,‘\n‘,ar3) [0 1 2 3 4 5 6 7 8 9] [[0 1 2 3 4] [5 6 7 8 9]] >>> ar4 = np.zeros((4,6)).reshape(3,8) #用法二,生成数组后直接改变形状。 >>> print(ar4) [[0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0.] [0. 0. 0. 0. 0. 0. 0. 0.]] >>> ar5 = np.reshape(np.arange(12),(3,4)) #用法三,参数内添加数组,目标形状。 >>> print(ar5) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] >>># numpy.reshape(a, newshape, order=‘C‘):为数组提供新形状,而不更改其数据,所以元素数量需要一致!! >>> ar6 = np.resize(np.arange(5),(3,4)) >>> print(ar6) [[0 1 2 3] [4 0 1 2] [3 4 0 1]] >>>
# numpy.resize(a, new_shape):返回具有指定形状的新数组,如有必要可重复填充所需数量的元素。
# 注意了:.T/.reshape()/.resize()都是生成新的数组!!!
数组的复制
>>> ar1 = np.arange(10) >>> ar2 = ar1 >>> print(ar2 is ar1) True >>> ar1[2] = 9 >>> print(ar1, ar2) [0 1 9 3 4 5 6 7 8 9] [0 1 9 3 4 5 6 7 8 9]# 回忆python的赋值逻辑:指向内存中生成的一个值 → 这里ar1和ar2指向同一个值,所以ar1改变,ar2一起改变 >>> ar3 = ar1.copy() >>> print(ar3 is ar1) False >>> ar1[0] = 9 >>> print(ar1, ar3) [9 1 9 3 4 5 6 7 8 9] [0 1 9 3 4 5 6 7 8 9] >>># copy方法生成数组及其数据的完整拷贝
数组类型转换:.astype()
>>> ar1 = np.arange(10, dtype=float) >>> print(ar1,ar1.dtype) [0. 1. 2. 3. 4. 5. 6. 7. 8. 9.] float64# 可以在参数位置设置数组类型 >>> ar2 = ar1.astype(np.int32) >>> print(ar2,ar2.dtype) [0 1 2 3 4 5 6 7 8 9] int32
# a.astype():转换数组类型
# 注意:养成好习惯,数组类型用np.int32,而不是直接int32
数组堆叠
>>> a = np.arange(5) #a为一维数组,5个元素; >>> b = np.arange(5,9) #b为一维数组,4个元素; >>> ar1 = np.hstack((a,b)) #注意:((a,b))这里形状可以不一样。 >>> print(a,a.shape) [0 1 2 3 4] (5,) >>> print(b,b.shape) [5 6 7 8] (4,) >>> print(ar1,ar1.shape) [0 1 2 3 4 5 6 7 8] (9,) >>> >>> a = np.array([[1],[2],[3]]) #a为二维数组,3行1列; >>> b = np.array([[‘a‘],[‘b‘],[‘c‘]]) #b为二维数组,3行1列; >>> ar2 = np.hstack((a,b)) #((a,b)),这里a,b形状必须一致。>>> print(a,a.shape,‘\n‘, b,b.shape) [[1] [2] [3]] (3, 1) [[‘a‘] [‘b‘] [‘c‘]] (3, 1) >>> print(ar2,ar2.shape) [[‘1‘ ‘a‘] [‘2‘ ‘b‘] [‘3‘ ‘c‘]] (3, 2)# numpy.hstack(tup):水平(按列顺序)堆叠数组 >>> >>> a = np.arange(5) >>> b = np.arange(5,10) >>> ar1 = np.vstack((a,b)) >>> print(a,a.shape,‘\n‘, b,b.shape) [0 1 2 3 4] (5,) [5 6 7 8 9] (5,) >>> print(ar1,ar1.shape) [[0 1 2 3 4] [5 6 7 8 9]] (2, 5) >>> a = np.array([[1],[2],[3]]) >>> b = np.array([[‘a‘],[‘b‘],[‘c‘],[‘d‘]]) >>> ar2 = np.vstack((a,b)) #这里形状可以不一样。 >>> print(a,a.shape,‘\n‘,b,b.shape) [[1] [2] [3]] (3, 1) [[‘a‘] [‘b‘] [‘c‘] [‘d‘]] (4, 1) >>> print(ar2,ar2.shape) [[‘1‘] [‘2‘] [‘3‘] [‘a‘] [‘b‘] [‘c‘] [‘d‘]] (7, 1)# numpy.vstack(tup):垂直(按列顺序)堆叠数组 >>> >>> a = np.arange(5) >>> b = np.arange(5,10) >>> ar1 = np.stack((a,b)) >>> ar2 = np.stack((a,b),axis = 1) >>> print(a,a.shape,‘\n‘,b,b.shape) [0 1 2 3 4] (5,) [5 6 7 8 9] (5,) >>> print(ar1,ar1.shape) [[0 1 2 3 4] [5 6 7 8 9]] (2, 5) >>> print(ar2,ar2.shape) [[0 5] [1 6] [2 7] [3 8] [4 9]] (5, 2) >>>
# numpy.stack(arrays, axis=0):沿着新轴连接数组的序列,形状必须一样!
# 重点解释axis参数的意思,假设两个数组[1 2 3]和[4 5 6],shape均为(3,0)
# axis=0:[[1 2 3] [4 5 6]],shape为(2,3)
# axis=1:[[1 4] [2 5] [3 6]],shape为(3,2)
数组拆分
>>> ar = np.arange(16).reshape(4,4) >>> ar1 = np.hsplit(ar,2) >>> print(ar) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] >>> print(ar1,type(ar1)) [array([[ 0, 1], [ 4, 5], [ 8, 9], [12, 13]]), array([[ 2, 3], [ 6, 7], [10, 11], [14, 15]])] <class ‘list‘>
# numpy.hsplit(ary, indices_or_sections):将数组水平(逐列)拆分为多个子数组 → 按列拆分
# 输出结果为列表,列表中元素为数组
>>> ar2 = np.vsplit(ar,4) >>> print(ar2,type(ar2)) [array([[0, 1, 2, 3]]), array([[4, 5, 6, 7]]), array([[ 8, 9, 10, 11]]), array([[12, 13, 14, 15]])] <class ‘list‘> >>># numpy.vsplit(ary, indices_or_sections)::将数组垂直(行方向)拆分为多个子数组 → 按行拆
数组简单运算
>>> ar = np.arange(6).reshape(2,3) >>> print(ar) [[0 1 2] [3 4 5]] >>> print(ar + 10) #加法 [[10 11 12] [13 14 15]] >>> print(ar * 2) #乘法 [[ 0 2 4] [ 6 8 10]] >>> print(1 / (ar+1)) #除法 [[1. 0.5 0.33333333] [0.25 0.2 0.16666667]] >>> print(ar ** 0.5) #幂法 [[0. 1. 1.41421356] [1.73205081 2. 2.23606798]] >>> >>> print(ar.mean()) #求平均值 2.5 >>> print(ar.max()) #求最大值 5 >>> print(ar.min()) #求最小值 0 >>> print(ar.std()) #求标准差 1.707825127659933 >>> print(ar.var()) #求方差 2.9166666666666665 >>> print(ar.sum(),np.sum(ar,axis = 0)) #求和 np.sum()------>> axis = 0按列求和、axis = 1按行求和。 15 [3 5 7] >>> print(np.sort(np.array([1,4,3,2,5,6]))) #排序 [1 2 3 4 5 6]#常用函数
Numpy索引及切片
>>> ar = np.arange(20) >>> print(ar) [ 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19] >>> print(ar[4]) 4 >>> print(ar[3:6]) [3 4 5]# 基本索引及切片 >>> >>> ar = np.arange(16).reshape(4,4) >>> print(ar,‘数组轴数为%i‘%ar.ndim) #4*4的数组 [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11] [12 13 14 15]] 数组轴数为2 >>> print(ar[2], ‘数组轴数为%i‘%ar[2].ndim) #切片为下一个维度的一个元素,所以为一维数组。 [ 8 9 10 11] 数组轴数为1 >>> print(ar[2][1]) #二次索引,得到一维数组中的一个值; 9 >>> print(ar[1:3], ‘数组轴数为%i‘%ar[1:3].ndim) #切片为2个一维数组组成的二维数组; [[ 4 5 6 7] [ 8 9 10 11]] 数组轴数为2 >>> print(ar[2,2]) #切片为数组中的第3行第3列; 10 >>> print(ar[:2,1:]) #切片为数组中的第1、2行,第2、3、4列;二维数组 [[1 2 3] [5 6 7]] >>># 二维数组索引及切片 >>> ar = np.arange(8).reshape(2,2,2) >>> print(ar, ‘数组轴数为%i‘%ar.ndim) #2*2*2的数组; [[[0 1] [2 3]] [[4 5] [6 7]]] 数组轴数为3 >>> print(ar[0], ‘数组轴数为%i‘%ar[0].ndim) #三维数组的下一个维度的第一个元素 ----->> 一个二维数组; [[0 1] [2 3]] 数组轴数为2 >>> print(ar[0][0], ‘数组轴数为%i‘%ar[0][0].ndim) #三维数组的下一个维度的第一个元素下的第一个元素 ------>> 一个一维数组 [0 1] 数组轴数为1 >>> print(ar[0][0][1], ‘数组轴数为%i‘%ar[0][0][1].ndim) 1 数组轴数为0 >>># **三维数组索引及切片
布尔型索引及切片
>>> ar = np.arange(12).reshape(3,4) >>> i = np.array([True,False,True]) >>> j = np.array([True,True,False,False]) >>> print(ar,‘\n‘,i,‘\n‘,j) [[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]] [ True False True] [ True True False False] >>> print(ar[i,:]) #在第一维度做判断,只保留True,这里第一维度是指行,ar[i,:] = ar[i](简单书写格式) [[ 0 1 2 3] [ 8 9 10 11]] >>> print(ar[:,j]) #在第二维度做判断, [[0 1] [4 5] [8 9]] >>>#布尔型索引:以布尔型的矩阵去做筛选 >>> m = ar > 5 >>> print(m) #这里m是一个判断矩阵; [[False False False False] [False False True True] [ True True True True]] >>> print(ar[m]) #用m判断矩阵去筛选ar数组中>5的元素 ------------->>>后边pandas判断方式的原理就在此。 [ 6 7 8 9 10 11]
数组索引及切片的值更改、复制
>>> ar = np.arange(10) >>> print(ar) [0 1 2 3 4 5 6 7 8 9] >>> ar[5] = 100 >>> ar[7:9] = 200 >>> print(ar) [ 0 1 2 3 4 100 6 200 200 9] >>>##一个标量赋值给一个索引/切片时,会自动改变/传播原始数组 >>> ar = np.arange(10) >>> b = ar.copy() >>> b[7:9] = 200 >>> print(ar) [0 1 2 3 4 5 6 7 8 9] >>> print(b) [ 0 1 2 3 4 5 6 200 200 9] >>>#复制
Numpy随机数
随机数生成
####生成一个标准正太分布的4*4样本值>>> samples = np.random.normal(size=(4,4)) #random.normal就表示正态分布 >>> print(samples) [[ 0.07060943 -1.25339552 0.29914172 -0.5340139 ] [-0.48759624 -0.59666746 -0.11825987 0.04588257] [-0.43502379 -0.29065528 0.17958867 -1.61939862] [ 0.06733506 0.11634428 0.05324929 -0.46936231]]
numpy.random.rand(d0, d1, ..., dn):生成一个[0,1)之间的随机浮点数或N维浮点数组 —— 均匀分布
>>> a = np.random.rand() >>> print(a,type(a)) #生成一个随机浮点数 0.34655619552666683 <class ‘float‘> >>> >>> b = np.random.rand(4) >>> print(b,type(b)) #生成形状为4的一维数组 [0.97735994 0.20438528 0.5741046 0.6604635 ] <class ‘numpy.ndarray‘> >>> c = np.random.rand(2,3) >>> print(c,type(c)) #生成形状为2*3的二维数组,注意这里不是((2,3)) [[0.75476081 0.30673306 0.94664526] [0.4011794 0.91558286 0.09614256]] <class ‘numpy.ndarray‘> >>>
#####在Jupyter里边运行samples1 = np.random.rand(500) samples2 = np.random.rand(500) import matplotlib.pyplot as plt #导入matplotlib模块,用于图标辅助分析。 % matplotlib inline #魔法函数,每次运行自动生成图表 plt.scatter(samples1,samples2)# 生成500个均匀分布的样本值
numpy.random.randn(d0, d1, ..., dn):生成一个浮点数或N维浮点数组 —— 正态分布
samples1 = np.random.randn(500) samples2 = np.random.randn(500) import matplotlib.pyplot as plt % matplotlib inline plt.scatter(samples1,samples2) # randn和rand的参数用法一样 # 生成1000个正太的样本值
numpy.random.randint(low, high=None, size=None, dtype=‘l‘):生成一个整数或N维整数数组
# 若high不为None时,取[low,high)之间随机整数,否则取值[0,low)之间随机整数,且high必须大于low # dtype参数:只能是int类型 >>> print(np.random.randint(2)) ## low=2:生成1个[0,2)之间随机整数 0 >>> print(np.random.randint(2,size = 5)) #low=2,size=5 :生成5个[0,2)之间随机整数 [1 1 0 0 0] >>> print(np.random.randint(2,6,size=5)) #low=2,high=6,size=5:生成5个[2,6)之间随机整数 [2 4 2 5 4] >>> print(np.random.randint(2,size=(2,3))) #low=2,size=(2,3):生成一个2x3整数数组,取数范围:[0,2)随机整数 [[0 1 1] [1 0 0]] >>> print(np.random.randint(2,6,(2,3))) #low=2,high=6,size=(2,3):生成一个2*3整数数组,取值范围:[2,6)随机整数 [[5 3 3] [3 2 2]]
Numpy数据的输入输出
存储数组数据 .npy文件
import os os.chdir(r‘C:\Users\Administrator\Desktop‘) ar = np.random.rand(5,5) print(ar) # np.save(‘arraydata.npy‘,ar) np.save(r‘C:\Users\Administrator\Desktop\arraydata.npy‘,ar) print(‘finish‘) 打印: [[ 0.26757585 0.29147944 0.64875451 0.93792551 0.94136359] [ 0.26270971 0.11359578 0.40340343 0.43775798 0.00448808] [ 0.77723808 0.67647676 0.01720309 0.1811023 0.5937187 ] [ 0.64925335 0.76782983 0.07480746 0.54560242 0.34152663] [ 0.77761772 0.67317061 0.61600948 0.58411754 0.61670874]] finish
读取数组数据 .npy文件
ar_load = np.load(‘arraydata.npy‘) print(ar_load) #np.load(r‘C:\Users\Administrator\Desktop\arraydata.npy‘) ##也可以直接打开
[[ 0.26757585 0.29147944 0.64875451 0.93792551 0.94136359] [ 0.26270971 0.11359578 0.40340343 0.43775798 0.00448808] [ 0.77723808 0.67647676 0.01720309 0.1811023 0.5937187 ] [ 0.64925335 0.76782983 0.07480746 0.54560242 0.34152663] [ 0.77761772 0.67317061 0.61600948 0.58411754 0.61670874]]
# 存储/读取文本文件
ar = np.random.rand(5,5) np.savetxt(‘array.txt‘,ar,delimiter=‘,‘) # np.savetxt(fname, X, fmt=‘%.18e‘, delimiter=‘ ‘, newline=‘\n‘, header=‘‘, footer=‘‘, comments=‘# ‘):存储为文本txt文件 ar_loadtxt = np.loadtxt(‘array.txt‘,delimiter=‘,‘) print(ar_loadtxt) # 也可以直接 np.loadtxt(r‘C:\Users\Administrator\Desktop\array.txt‘) [[ 0.85083698 0.67495645 0.95420959 0.29894536 0.85662616] [ 0.2238608 0.31017771 0.58716182 0.48031634 0.65689202] [ 0.79469571 0.32661995 0.99651714 0.1758829 0.01264854] [ 0.75023541 0.10395296 0.69800992 0.23672871 0.00297461] [ 0.828437 0.67540604 0.92137268 0.652755 0.23985235]]
原文地址:https://www.cnblogs.com/shengyang17/p/9446372.html
时间: 2024-11-02 17:42:00