-
NumPy常用统计函数
目录
1.求和函数 numpy.sum(a, axis=None)
------a.sum(axis=None)
2.求均值 numpy.mean(a, axis=None)
-----a.mean(axis=None)
3.加权平均值numpy.average(a,axis=None,weights=None)
4.标准差numpy.std(a,axis=None)
-------a.std(axis=None)
5.方差numpy.var(a,axis=None)
--------a.var(axis=None)
6.最大值/最小值 numpy.amin(a,axis=None)
----------numpy.min(a,axis=None)
-------a.min(axis=None)
7.最小值索引一维下标numpy.argmin(a,axis=None)
---------a.argmin(axis=None)
8.最大值索引numpy.argmax(a,axis=None)
---------a.argmax(axis=None)
9.原形状索引下标numpy.unravel_index(index, shape)
10.中位数numpy.median(a,axis=None)
11.最值之差numpy.ptp(a,axis=None)
------------a.ptp(a,axis=None)
12.百分位数numpy.percentile(a, q, axis=None)
引入模块import numpy as np
1.求和
1.numpy.sum(a, axis=None)
/a.sum(axis=None)
根据给定轴
axis
计算数组a
相关元素之和,axis
整数或元组,不指定轴则默认求全部元素之和。若
a
的shape
为(d0,d1,..,dn)
,当axis=(m1,m2,...mi)
时,返回结果应是一个shape
为(d0,d1,...,dn)-(dm1,dm2,...dmi)
,每个元素是轴m1,m2,...mi
上元素之和
例:
a = np.arange(24).reshape((2, 3, 4))
print("数组a:\n", a)
print("np.sum(a):", np.sum(a)) # 全部元素和
print("np.sum(a, axis=0):\n", np.sum(a, axis=0)) # 第0轴(最外围)的元素和
print("np.sum(a, axis=1):\n", np.sum(a, axis=1)) # 第1轴元素和
print("np.sum(a, axis=(0, 1)):\n", np.sum(a, axis=(0, 1))) # 第0轴和第1轴元素之和
print("np.sum(a, axis=(0, 2)):\n", np.sum(a, axis=(0, 2))) # 第0轴和第2轴元素之和
输出:
数组a:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
np.sum(a): 276
np.sum(a, axis=0):
[[12 14 16 18] # 0+12=12 1+13=14 ...
[20 22 24 26] # 4+16=20 5+17=22
[28 30 32 34]]
np.sum(a, axis=1):
[[12 15 18 21] # 0+4+8=12 1+5+9=15 ...
[48 51 54 57]] # 12+16+20=48 13+17+21=51
np.sum(a, axis=(0, 1)):
[60 66 72 78] # 0+4+8+12+16+20=60 1+5+9+13+17+21=66...
np.sum(a, axis=(0, 2)):
[ 60 92 124] # 0+1+2+3+12+13+14+15=60 4+5+6+7+16+17+18+19=92....
2.求均值
2.numpy.mean(a, axis=None)
/a.mean(axis=None)
`
根据给定轴
axis
计算数组a
相关元素的平均值,axis
整数或元组。不指定
axis
,默认求所有元素平均值。指定axis
,求指定轴上元素平均值。若
a
的shape
为(d0,d1,..,dn)
,当axis=(m1,m2,...mi)
时,返回结果应是一个shape
为(d0,d1,...,dn)-(dm1,dm2,...dmi)
,每个元素是轴m1,m2,...mi
上所有元素的平均值
例:
print("数组a:\n", a)
print("np.mean(a):", np.mean(a)) # 全部元素的平均值
print("np.mean(a, axis=0):\n", np.mean(a, axis=0)) # 0轴上的平均值
print("np.mean(a, axis=(0, 2)):\n", np.mean(a, axis=(0, 2))) # 0轴和2轴平均值
输出:
数组a:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
np.mean(a): 11.5
np.mean(a, axis=0):
[[ 6. 7. 8. 9.] # (0+12)/2=6 (1+13)/2=7...
[10. 11. 12. 13.] # (4+16)/2=10 (5+17)/2=11...
[14. 15. 16. 17.]] # (8+20)/2=14 (9+21)/2=15..
np.mean(a, axis=(0, 2)):
[ 7.5 11.5 15.5] # (0+1+2+3+12+13+14+15)/2=7.5..
3.numpy.average(a,axis=None,weights=None)
根据给定轴
axis
计算数组a
相关元素的加权平均值,
weights
是一个权重数组,形状应与给定数组a
的shape
相同,即:weights.shape=a.shape
或者在指定一个轴axis
时,weight
则应是一个一维数组,数组元素个数与指定轴维度数相同。当不指定
weigts
时,此时即为求平均值,效果同.mean
相同
例:
print("数组a:\n", a)
print("np.average(a, axis=0):\n", np.average(a, axis=0))
print("np.average(a, axis=0, weights=[10, 1]):\n", np.average(a, axis=0, weights=[10, 1]))
wei = np.random.randint(1, 60, (2, 3, 4 ))
print("权重数组是:", wei)
print("np.average(a, axis=(0, 2), weights=wei):\n", np.average(a, axis=(0, 2), weights=wei))
输出:
数组a:
[[[ 0 1 2 3]
[ 4 5 6 7]
[ 8 9 10 11]]
[[12 13 14 15]
[16 17 18 19]
[20 21 22 23]]]
np.average(a, axis=0):
[[ 6. 7. 8. 9.]
[10. 11. 12. 13.]
[14. 15. 16. 17.]]
np.average(a, axis=0, weights=[10, 1]):
[[ 1.09090909 2.09090909 3.09090909 4.09090909] # (0*10+12*1)/(10+1)=1.0909
[ 5.09090909 6.09090909 7.09090909 8.09090909] # (4*10+16*1)/(10+1)=5.0909
[ 9.09090909 10.09090909 11.09090909 12.09090909]]
权重数组是: [[[37 5 50 9]
[ 9 40 17 42]
[45 4 41 29]]
[[17 24 29 37]
[20 8 14 37]
[ 3 1 48 14]]]
np.average(a, axis=(0, 2), weights=wei):
[ 7.73557692 10.92513369 13.96756757] # (0*37+1*5+2*50+3*9+12*17+13*24+14*29+15*37)/(37+5+50+9+17+24+29+37)=7.7355
3.标准差/方差
4.numpy.std(a,axis=None)
/a.std(axis=None)
numpy.var(a,axis=None)
/a.var(axis=None)
.std(a,axis=None)
根据给定轴axis
计算数组a
相关元素的总体标准差(要与样本标准差区分)即:
(Standard Deviation)
——std
标准差,又称均方差
.var(a,axis=None)
根据给定轴axis
计算数组a
相关元素的总体方差即: