次序统计
计算最小值
numpy.amin(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])
import numpy as np
x = np.array([[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]])
y = np.amin(x)
print(y) # 11
y = np.amin(x, axis=0)
print(y) # [11 12 13 14 15]
y = np.amin(x, axis=1)
print(y) # [11 16 21 26 31]
解释:axis的上限随着数组的维度增加而增加,n维数组ndarray,axis的范围为[0, n-1],例如,对于一维数组[1, 2],axis的范围为0,二维数组[[1, 2], [3, 4]]的axis的范围为0~1,以此类推。其实axis每加一级,就相当于把数组降一维度,递归的对其中的每个元素进行操作:
# 伪代码
operation(ndarray, axis=n):
if(n == 0):
res = caculate something cover all items in ndarray
return res
res = []
for item in ndarray:
res.append(operation(item, axis=n-1))
return res
对于上面的例子,数组是二维的,当axis=0时,拆掉最外面的中括号,剩下
[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]
以每个块为整体,对所有的块做min运算,这是向量化的运算,对不同一维数组中的同一位置上的元素做运算,在这个例子中,得到的结果是[11, 12, 13, 14, 15]。因为axis=0,所以最终的答案就为它。
当axis=1时,对每个一维数组降维后分别进行min运算,得到的结果都分别是一个标量,为11,16,21,26,31,因为axis=1,所以回溯后这些结果会被追加到列表中,所以最终结果为[11, 16, 21, 26, 31]。
计算最大值
numpy.amax(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])
原理同上,不做解释。
计算极差
极差:一个数组中的最大值-最小值
numpy.ptp(a, axis=None, out=None, keepdims=np._NoValue) Range of values (maximum - minimum)
import numpy as np
np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10 2 1 1 16]
# [18 11 10 14 10]
# [11 1 9 18 8]
# [16 2 0 15 16]]
print(np.ptp(x)) # 18
print(np.ptp(x, axis=0)) # [ 8 10 10 17 8]
print(np.ptp(x, axis=1)) # [15 8 17 16]
计算分位数
numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)
- a:目标数组
- q:[0, 100]的float数组,分位数
- axis:维度方向
import numpy as np
np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10 2 1 1 16]
# [18 11 10 14 10]
# [11 1 9 18 8]
# [16 2 0 15 16]]
print(np.percentile(x, [25, 50]))
# [ 2. 10.]
print(np.percentile(x, [25, 50], axis=0))
# [[10.75 1.75 0.75 10.75 9.5 ]
# [13.5 2. 5. 14.5 13. ]]
print(np.percentile(x, [25, 50], axis=1))
# [[ 1. 10. 8. 2.]
# [ 2. 11. 9. 15.]]
均值与方差
计算中位数
numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)
import numpy as np
np.random.seed(20200623)
x = np.random.randint(0, 20, size=[4, 5])
print(x)
# [[10 2 1 1 16]
# [18 11 10 14 10]
# [11 1 9 18 8]
# [16 2 0 15 16]]
print(np.percentile(x, 50))
print(np.median(x))
# 10.0
print(np.percentile(x, 50, axis=0))
print(np.median(x, axis=0))
# [13.5 2. 5. 14.5 13. ]
print(np.percentile(x, 50, axis=1))
print(np.median(x, axis=1))
# [ 2. 11. 9. 15.]
计算平均值
numpy.mean(a[, axis=None, dtype=None, out=None, keepdims=np._NoValue)])
import numpy as np
x = np.array([[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]])
y = np.mean(x)
print(y) # 23.0
y = np.mean(x, axis=0)
print(y) # [21. 22. 23. 24. 25.]
y = np.mean(x, axis=1)
print(y) # [13. 18. 23. 28. 33.]
计算加权平均值
numpy.average(a[, axis=None, weights=None, returned=False])