import numpy as np
a = np.arange(100).reshape(5, 20)
np.savetxt('a.csv', a, fmt='%d', delimiter=',')
参数fmt表示的是存入的数据类型,参数delimiter表示的是分隔符
b = np.loadtxt('a.csv', delimiter=',')
b
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.]])
numpy库的子库random中包含了许多生成随机数的函数
rand 随机生成的0-1的小数
np.random.rand(3, 4, 5)
array([[[0.31758507, 0.90577837, 0.57722286, 0.93548532, 0.67162346], [0.60669063, 0.37058699, 0.09268176, 0.07844332, 0.31498516], [0.89279518, 0.48433622, 0.33788483, 0.63917024, 0.21380341], [0.93540191, 0.13810566, 0.84765187, 0.81036746, 0.73655162]], [[0.29426517, 0.64758244, 0.79142543, 0.99561557, 0.54726003], [0.13982297, 0.65018781, 0.24860317, 0.82961137, 0.08030367], [0.06089794, 0.57703706, 0.76067903, 0.3365547 , 0.5817126 ], [0.90333741, 0.84033221, 0.44258611, 0.79047538, 0.95275519]], [[0.92782177, 0.46044411, 0.13944521, 0.79174124, 0.22450652], [0.64648888, 0.98737518, 0.71785434, 0.7538382 , 0.36011979], [0.91529664, 0.43811212, 0.17316345, 0.83911132, 0.2484261 ], [0.94319115, 0.43730428, 0.2671841 , 0.57026498, 0.19784627]]])
randn 生成呈现正态分布的小数
np.random.randn(3, 4, 5)
array([[[-1.12557871, -1.36187298, 2.12778214, 1.14716151, -0.20528341], [-0.26978878, 1.20338214, -0.34394721, 0.65791381, 0.4740779 ], [-1.52342418, -0.95010058, 1.85885593, 0.83989784, 2.37659475], [-0.31869578, -0.20959431, -0.04414596, 0.7047024 , -0.69207778]], [[-2.08112153, 1.41569022, -0.73259308, 1.38272686, 0.51822214], [ 1.35657693, -1.44589925, -1.32261999, -0.59110943, -0.51575457], [ 0.28218551, -0.80553322, -1.57620057, -0.47332692, -0.01079564], [-0.51143865, -0.32561054, -0.05456582, -0.56157563, 0.69832108]], [[ 0.82902173, -0.27178094, -1.03625369, 2.12448222, -1.78156889], [ 0.42228067, -0.05892293, -0.08577194, -0.19544103, 0.13547228], [-1.02194311, -0.45929995, -0.56612156, -0.6817057 , 0.36600193], [-0.08898777, -0.61071157, 1.06798344, -0.84894722, -1.19189576]]])
randint 生成随机整数
np.random.randint(100, 200, (3, 4, 5))
array([[[167, 118, 133, 165, 170], [181, 135, 113, 181, 136], [156, 145, 105, 129, 144], [169, 108, 185, 177, 117]], [[175, 172, 130, 160, 122], [145, 191, 167, 124, 105], [117, 133, 146, 182, 136], [198, 190, 165, 123, 169]], [[131, 196, 128, 115, 199], [121, 131, 141, 116, 109], [101, 193, 147, 156, 144], [131, 145, 119, 155, 153]]])
seed 设定随机种子
np.random.seed(10)
当随机种子相同时生成的随机数是相同的
a = np.arange(15).reshape((3, 5))
a
array([[ 0, 1, 2, 3, 4], [ 5, 6, 7, 8, 9], [10, 11, 12, 13, 14]])
sum 对array的元素求和
np.sum(a)
105
np.mean(a)
7.0
mean也可以在行和列求平均数
np.mean(a, axis=1)
array([ 2., 7., 12.])
np.mean(a, axis=0)
array([5., 6., 7., 8., 9.])
average 加权平均数
np.average(a)
7.0
默认权值为1
np.average(a, axis=0, weights=[10, 5, 1])
array([2.1875, 3.1875, 4.1875, 5.1875, 6.1875])
np.std(a) # 标准差
4.320493798938574
np.var(a) # 方差
18.666666666666668
max 求最大值
np.max(a)
14
同理min求最小值
argmin 降成一维后的最小值下标
np.argmin(a)
0
同理argmax 降成一维后的最大值下标
b = np.arange(15, 0, -1).reshape(3, 5)
b
array([[15, 14, 13, 12, 11], [10, 9, 8, 7, 6], [ 5, 4, 3, 2, 1]])
np.ptp(b) # 最大值和最小值的差
14
np.median(b) # 中位数
8.0
gradient 求梯度
a = np.random.randint(0, 20, (5))
np.gradient(a)
array([18., 9., -5., -8., -6.])
c = np.random.randint(0, 50, (3, 5))
np.gradient(c) # 生成两个array 第一个为外层梯度值 第二个为内层梯度值
[array([[ 28. , -10. , -25. , -26. , -7. ], [ -5. , 8.5, -10.5, 5.5, 15. ], [-38. , 27. , 4. , 37. , 37. ]]), array([[ 2. , 6. , 7.5, -6. , -17. ], [-36. , -20.5, -0.5, 3. , 2. ], [ 29. , 0.5, 4.5, 19.5, 2. ]])]