文章目录
- Numpy的函数子库
- NumPy的random子库
- Numpy的统计函数
- Numpy的梯度函数
Numpy的函数子库
NumPy的random子库
np.random.*
函数 |
说明 |
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 [56]: a = np.random.rand(3,4)
In [57]: a
Out[57]:
array([[0.1418607 , 0.32400784, 0.68540098, 0.56252664],
[0.06896844, 0.23757673, 0.38972662, 0.12713579],
[0.95238154, 0.81339513, 0.4815053 , 0.68610578]])
In [58]: b = np.random.randn(3,4)
In [59]: b
Out[59]:
array([[ 0.9243267 , 0.98594375, 1.47001418, 2.5367111 ],
[-1.93785009, -0.85003969, -0.68096995, 0.04112802],
[ 0.07211376, -0.10020339, -0.66382975, 0.71514039]])
In [60]: c = np.random.randint(10,20,(2,3))
In [61]: c
Out[61]:
array([[18, 12, 10],
[17, 14, 13]])
In [62]: np.random.seed(12)
In [63]: d = np.random.randint(10,20,(2,3))
In [64]: d
Out[64]:
array([[16, 11, 12],
[13, 13, 10]])
In [65]: e = np.random.randint(10,20,(2,3))
In [66]: e
Out[66]:
array([[16, 11, 14],
[15, 19, 12]])
指定随机数种子,每次randint()生成的随机数都相同
函数 |
说明 |
shuffle(a) |
根据数组a的第1轴进行随排列,改变数组x |
permutation(a) |
根据数组a的第1轴产生一个新的乱序数组,不改变数组x |
choice(a[,size,replace,p]) |
从一维数组a中以概率p抽取元素,形成size形状新数组,replace表示是否可以重用元素,默认为False |
In [68]: a = np.random.randint(10,20,(4,4))
In [69]: a
Out[69]:
array([[16, 10, 15, 18],
[12, 19, 13, 14],
[13, 11, 17, 10],
[12, 16, 12, 10]])
In [70]: np.random.shuffle(a)
In [71]: a
Out[71]:
array([[12, 16, 12, 10],
[12, 19, 13, 14],
[13, 11, 17, 10],
[16, 10, 15, 18]])
In [72]: np.random.shuffle(a)
In [73]: a
Out[73]:
array([[12, 16, 12, 10],
[13, 11, 17, 10],
[16, 10, 15, 18],
[12, 19, 13, 14]])
In [74]: b = np.random.randint(10,20,(3,3))
In [75]: b
Out[75]:
array([[10, 10, 19],
[18, 19, 16],
[11, 18, 14]])
In [76]: np.random.permutation(b)
Out[76]:
array([[18, 19, 16],
[11, 18, 14],
[10, 10, 19]])
In [77]: b
Out[77]:
array([[10, 10, 19],
[18, 19, 16],
[11, 18, 14]])
In [81]: c = np.random.randint(100,200,(10,))
In [82]: c
Out[82]: array([197, 120, 129, 169, 151, 143, 132, 143, 118, 170])
In [83]: np.random.choice(c,(2,4))
Out[83]:
array([[129, 169, 129, 132],
[151, 143, 197, 197]])
In [84]: np.random.choice(c,(2,4),replace=False)
Out[84]:
array([[118, 170, 132, 129],
[120, 169, 197, 143]])
In [85]: np.random.choice(c,(2,4),p=c/np.sum(c))
Out[85]:
array([[151, 170, 143, 143],
[151, 151, 170, 120]])
函数 |
说明 |
uniform(low,high,size) |
产生具有均匀分布的数组,low起始值,high结束值,size形状 |
normal(loc,scale,size) |
产生具有正态分布的数组,loc均值,scale标准差,size形状 |
poisson(lam,size) |
产生具有泊松分布的数组,lam随机事件发生率,size形状 |
In [87]: u = np.random.uniform(0,100,(4,3))
In [88]: u
Out[88]:
array([[94.45814622, 7.34700439, 59.51524566],
[ 3.11510709, 66.52574325, 63.73854951],
[86.24651623, 94.16377123, 44.56270534],
[66.99576324, 92.43306996, 61.94234668]])
In [91]: n = np.random.normal(50,10,(4,3))
In [92]: n
Out[92]:
array([[44.68523102, 37.4505274 , 59.30168895],
[47.653849 , 46.35543274, 36.59608302],
[51.72911851, 71.97371662, 61.3013652 ],
[43.25637731, 47.22515845, 46.71863417]])
Numpy的统计函数
np.*
函数 |
说明 |
sum(a, axis=None) |
根据给定轴axis计算数组a相关元素之和,axis整数或元组 |
mean(a, axis=None) |
根据给定轴axis计算数组a相关元素的期望,axis整数或元组 |
average(a,axis=None,weights=None) |
根据给定轴axis计算数组a相关元素的加权平均值 |
std(a, axis=None) |
根据给定轴axis计算数组a相关元素的标准差 |
var(a, axis=None) |
根据给定轴axis计算数组a相关元素的方差 |
In [93]: a = np.arange(12).reshape(4,3)
In [94]: a
Out[94]:
array([[ 0, 1, 2],
[ 3, 4, 5],
[ 6, 7, 8],
[ 9, 10, 11]])
In [95]: np.sum(a)
Out[95]: 66
In [96]: np.mean(a,axis=0)
Out[96]: array([4.5, 5.5, 6.5])
In [97]: np.mean(a,axis=1)
Out[97]: array([ 1., 4., 7., 10.])
In [98]: np.average(a,axis=0,weights=[10,1,1,1])
Out[98]: array([1.38461538, 2.38461538, 3.38461538])
In [99]: np.std(a)
Out[99]: 3.452052529534663
In [100]: np.var(a)
Out[100]: 11.916666666666666
函数 |
说明 |
min(a) max(a) |
计算数组a中元素的最小值、最大值 |
argmin(a) argmax(a) |
计算数组a中元素最小值、最大值的降一维后下标 |
unravel_index(index, shape) |
根据shape将一维下标index转换成多维下标 |
ptp(a) |
计算数组a中元素最大值与最小值的差 |
median(a) |
计算数组a中元素的中位数(中值) |
In [101]: np.random.seed(5)
In [102]: b = np.random.randint(0,10,(3,4))
In [103]: b
Out[103]:
array([[3, 6, 6, 0],
[9, 8, 4, 7],
[0, 0, 7, 1]])
In [104]: np.max(b)
Out[104]: 9
In [105]: np.argmax(b)
Out[105]: 4
In [106]: np.unravel_index(np.argmax(b),b.shape)
Out[106]: (1, 0)
In [107]: np.ptp(b)
Out[107]: 9
In [108]: np.median(b)
Out[108]: 5.0
Numpy的梯度函数
函数 |
说明 |
np.gradient(f) |
计算数组f中元素的梯度,当f为多维时,返回每个维度梯度 |
梯度:连续值之间的变化率,即斜率。
XY坐标轴连续三个X坐标对应的Y轴值:a, b, c,其中,b的梯度是: (c‐a)/2
In [109]: a = np.random.randint(0,10,(8,))
In [110]: a
Out[110]: array([5, 7, 0, 1, 4, 6, 2, 9])
In [111]: np.gradient(a)
Out[111]: array([ 2. , -2.5, -3. , 2. , 2.5, -1. , 1.5, 7. ])
In [112]: b = np.random.randint(0,10,(4,3))
In [113]: b
Out[113]:
array([[9, 9, 9],
[1, 2, 7],
[0, 5, 0],
[0, 4, 4]])
In [114]: np.gradient(b)
Out[114]:
[array([[-8. , -7. , -2. ],
[-4.5, -2. , -4.5],
[-0.5, 1. , -1.5],
[ 0. , -1. , 4. ]]),
array([[ 0., 0., 0.],
[ 1., 3., 5.],
[ 5., 0., -5.],
[ 4., 2., 0.]])]