axis:坐标轴的方向,调整计算的维度方向,取值范围0/1,0为沿纵轴计算,1为沿横轴计算。
次序统计
最小值
numpy.amin(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue,where=np._NoValue])
返回数组或数组某一轴的最小值
import numpy as np
# 计算最小值
x = np.array([[8,2,5],
[7,6,9],
[3,5,2]])
y = np.amin(x)
print(y) # 2
y = np.amin(x, axis=0) # axis=0:计算数组中的元素沿纵轴的最小值
print(y) # [3 2 2]
y = np.amin(x, axis=1) # axis=1:计算数组中的元素沿横轴的最小值
轴的最小值
print(y) # [2 6 2]
最大值
numpy.amax(a[,axis=None,out=None,keepdims=np._NoValue,
initial=np._NoValue,where=np._NoValue])
返回数组或数组某一轴的最大值
import numpy as np
# 计算最大值
x = np.array([[8,2,5],
[7,6,9],
[3,5,2]])
y = np.amax(x)
print(y) # 9
y = np.amax(x, axis=0) # axis=0:计算数组中的元素沿纵轴的最大值
print(y) # [8 6 9]
y = np.amax(x, axis=1) # axis=1:计算数组中的元素沿横轴的最大值
print(y) # [8 9 5]
极差
numpy.ptp(a, axis=None, out=None, keepdims=np._NoValue)
max-min
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.amax(x)) # 18
print(np.amin(x)) # 0
print(np.ptp(x)) # 极差:18
print(np.amax(x,axis=0)) # [18 11 10 18 16]
print(np.amin(x,axis=0)) # [10 1 0 1 8]
print(np.ptp(x, axis=0)) # [ 8 10 10 17 8]
print(np.amax(x,axis=1)) # [16 18 18 16]
print(np.amin(x,axis=1)) # [ 1 10 1 0]
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)
q为介于0-100的参数,如25则为计算四分位数。
import numpy as np
# 计算分位数
# p分位数的位置公式如下:position = 1+(n-1)*p
# p分位数位置的值 = 位于p分位数取整后位置的值 +
#(位于p分位数取整下一位位置的值 - 位于p分位数取整后位置的值)*(p分位数位置 - p分位数位置取整)
np.random.seed(20200623)
# x = np.random.randint(0, 20, size=[4, 5])
x = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
print(x)
# print(np.sort(x)) # 对x进行排序
# print(np.argsort(x)) # 对x进行排序,返回排序后索引位置
print(np.percentile(x, [25, 50])) # [4.75 8.5] 计算25%分位数和中位数
print(np.percentile(x, [25, 50], axis=0))
# [[ 4. 5. 6. 7.]
# [ 7. 8. 9. 10.]]
print(np.percentile(x, [25, 50], axis=1))
# [[ 1.75 5.75 9.75 13.75]
# [ 2.5 6.5 10.5 14.5 ]]
均值与方差
中位数
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])
x = np.array([[1,2,3,4],
[5,6,7,8],
[9,10,11,12],
[13,14,15,16]])
print(x)
print(np.percentile(x, 50))
print(np.median(x)) # 8.5
print(np.percentile(x, 50, axis=0))
print(np.median(x, axis=0))
# [ 7. 8. 9. 10.]
print(np.percentile(x, 50, axis=1))
print(np.median(x, axis=1))
# [ 2.5 6.5 10.5 14.5]
平均值
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])
mean与average函数均为计算均值,计算加权平均值可用average函数。
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.average(x)
print(y) # 23.0
y = np.average(x, axis=0)
print(y) # [21. 22. 23. 24. 25.]
y = np.average(x, axis=1)
print(y) # [13. 18. 23. 28. 33.]
y = np.arange(1, 26).reshape([5, 5]) # 权重为比重,是一个百分数
print(y)
# [[ 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]]
z = np.average(x, weights=y) # 指定权重后,average可以计算加权平均值
print(z) # 27.0
z = np.average(x, axis=0, weights=y) # (11*1+16*6+21*11+26*16+31*21)/(1+6+11+16+21)
print(z)
# [25.54545455 26.16666667 26.84615385 27.57142857 28.33333333]
z = np.average(x, axis=1, weights=y)
print(z)
# [13.66666667 18.25 23.15384615 28.11111111 33.08695652]
方差
numpy.var(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])
ddof为自由度个数。计算方差时分母为n,计算样本方差的无偏估计时分母为n-1。
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.var(x)
print(y) # 52.0
y = np.mean((x - np.mean(x)) ** 2) # 方差
print(y) # 52.0
y = np.var(x, ddof=1) # ddof:自由度的个数
print(y) # 54.166666666666664
y = np.sum((x - np.mean(x)) ** 2) / (x.size - 1) # 样本方差无偏估计
print(y) # 54.166666666666664
y = np.var(x, axis=0)
print(y) # [50. 50. 50. 50. 50.]
y = np.var(x, axis=1)
print(y) # [2. 2. 2. 2. 2.]
标准差
numpy.std(a[, axis=None, dtype=None, out=None, ddof=0, 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.std(x) # 标准差
print(y) # 7.211102550927978
y = np.sqrt(np.var(x)) # 方差的算术平方根
print(y) # 7.211102550927978
y = np.std(x, axis=0)
print(y)
# [7.07106781 7.07106781 7.07106781 7.07106781 7.07106781]
y = np.std(x, axis=1)
print(y)
# [1.41421356 1.41421356 1.41421356 1.41421356 1.41421356]
相关
协方差矩阵
numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,aweights=None)
协方差计算公式:
协方差矩阵:
其中
import numpy as np
# 计算协方差矩阵
x = [1, 2, 3, 4, 6]
y = [0, 2, 5, 6, 7]
print(np.cov(x)) # 3.7 两个变量相同时,样本方差即为样本协方差
print(np.cov(y)) # 8.5 样本方差
print(np.cov(x, y))
# [[3.7 5.25]
# [5.25 8.5 ]]
print(np.var(x)) # 2.96 方差
print(np.var(x, ddof=1)) # 3.7 样本方差
print(np.var(y)) # 6.8 方差
print(np.var(y, ddof=1)) # 8.5 样本方差
z = np.mean((x - np.mean(x)) * (y - np.mean(y))) # 协方差 E[(X-EX)(Y-EY)]
print(z) # 4.2
z = np.sum((x - np.mean(x)) * (y - np.mean(y))) / (len(x) - 1) # 样本协方差 (X-EX)(Y-EY)/(n-1)
print(z) # 5.25
z = np.dot(x - np.mean(x), y - np.mean(y)) / (len(x) - 1) # 样本协方差 np.dot为矩阵积运算
print(z) # 5.25
相关系数
numpy.corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue)
import numpy as np
# 计算相关系数
np.random.seed(20200623)
x, y = np.random.randint(0, 20, size=(2, 4))
print(x) # [10 2 1 1]
print(y) # [16 18 11 10]
z = np.corrcoef(x, y) # 相关系数
print(z)
# [[1. 0.48510096]
# [0.48510096 1. ]]
a = np.dot(x - np.mean(x), y - np.mean(y))
b = np.sqrt(np.dot(x - np.mean(x), x - np.mean(x)))
c = np.sqrt(np.dot(y - np.mean(y), y - np.mean(y)))
print(a / (b * c)) # 0.4851009629263671
直方图
numpy.digitize(x, bins, right=False)
返回输入数组x中每个值所属的数组bins的区间索引。
import numpy as np
x = np.array([0.2, 6.4, 3.0, 1.6])
bins = np.array([0.0, 1.0, 2.5, 4.0, 10.0]) # bins:一维单调数组,必须是升序或者降序
inds = np.digitize(x, bins) # 返回值:x在bins中的位置。
print(inds) # [1 4 3 2]
for n in range(x.size):
print(bins[inds[n] - 1], "<=", x[n], "<", bins[inds[n]])
# 0.0 <= 0.2 < 1.0
# 4.0 <= 6.4 < 10.0
# 2.5 <= 3.0 < 4.0
# 1.0 <= 1.6 < 2.5
import numpy as np
x = np.array([1.2, 10.0, 12.4, 15.5, 20.])
bins = np.array([0, 5, 10, 15, 20])
inds = np.digitize(x, bins, right=True) # 间隔包含最优
print(inds) # [1 2 3 4 4]
inds = np.digitize(x, bins, right=False) # 间隔不包含最优
print(inds) # [1 3 3 4 5]