Datawhale 第十九期 Numpy下 之 Task03:统计相关
次序相关
1 计算最小值
numpy.amin(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])
import numpy as np
x = np.random.random([5, 3])
y = np.amin(x)
print("x=",x)
#x= [[0.897 0.387 0.902]
# [0.135 0.886 0.373]
# [0.299 0.069 0.602]
# [0.263 0.081 0.172]
# [0.377 0.366 0.835]]
print("y=",y) # y= 0.06948618757223479
y = np.amin(x, axis=0)
print("y=",y) # y= [0.135 0.069 0.172]
y = np.amin(x, axis=1)
print("y=",y) # y= [0.387 0.135 0.069 0.081 0.366]
2 计算最大值
numpy.amax(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])
import numpy as np
x = np.random.random([4, 3])
print("x=",x)
#x= [[0.291 0.669 0.633]
# [0.84 0.154 0.947]
# [0.055 0.708 0.187]
# [0.188 0.176 0.132]]
y = np.amax(x)
print("y=",y) # y= 0.9473156730862662
y = np.amax(x, axis=0)
print("y=",y) # y= [0.84 0.708 0.947]
y = np.amax(x, axis=1)
print("y=",y) # y= [0.669 0.947 0.708 0.188]
3 计算极差
numpy.ptp(a, axis=None, out=None, keepdims=np._NoValue)
import numpy as np
np.random.seed(20201125)
x = np.random.randint(0, 20, size=[4, 5])
print("x=",x)
# x= [[ 3 13 15 6 11]
# [13 8 17 18 4]
# [19 1 14 17 3]
# [13 17 4 14 7]]
print("np.ptp(x)=",np.ptp(x)) # np.ptp(x)= 18
print("np.ptp(x, axis=0)=",np.ptp(x, axis=0)) # np.ptp(x, axis=0)= [16 16 13 12 8]
print("np.ptp(x, axis=1)=",np.ptp(x, axis=1)) # np.ptp(x, axis=1)= [12 14 18 13]
4 计算分位数
numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation='linear', keepdims=False)
a:array,用来算分位数的对象,可以是多维的数组。
q:介于0-100的float,用来计算是几分位的参数,如四分之一位就是25,如要算两个位置的数就[25,75]。
axis:坐标轴的方向,一维的就不用考虑了,多维的就用这个调整计算的维度方向,取值范围0/1。
import numpy as np
np.random.seed(20201125)
x = np.random.randint(0, 20, size=[5, 5])
print("x=",x)
# x= [[ 3 13 15 6 11]
# [13 8 17 18 4]
# [19 1 14 17 3]
# [13 17 4 14 7]
# [ 8 19 6 14 0]]
print("np.percentile(x, [25, 50])=",np.percentile(x, [25, 50]))
# np.percentile(x, [25, 50])= [ 6. 13.]
print("np.percentile(x, [25, 50], axis=0)=",np.percentile(x, [25, 50], axis=0))
# np.percentile(x, [25, 50], axis=0)= [[ 8. 8. 6. 14. 3.]
# [13. 13. 14. 14. 4.]]
print("np.percentile(x, [25, 50], axis=1)=",np.percentile(x, [25, 50], axis=1))
# np.percentile(x, [25, 50], axis=1)= [[ 6. 8. 3. 7. 6.]
# [11. 13. 14. 13. 8.]]
5 计算中位数
numpy.median(a, axis=None, out=None, overwrite_input=False, keepdims=False)
import numpy as np
np.random.seed(20201125)
x = np.random.randint(0, 20, size=[5, 5])
print("x=",x)
# x= [[ 3 13 15 6 11]
# [13 8 17 18 4]
# [19 1 14 17 3]
# [13 17 4 14 7]
# [ 8 19 6 14 0]]
print("np.percentile(x, 50)=",np.percentile(x, 50))#np.percentile(x, 50)= 13.0
print("np.median(x)=",np.median(x))
# np.median(x)= 13.0
print("np.percentile(x, 50, axis=0)=",np.percentile(x, 50, axis=0))#np.percentile(x, 50, axis=0)= [13. 13. 14. 14. 4.]
print("np.median(x, axis=0)=",np.median(x, axis=0))
# np.median(x, axis=0)= [13. 13. 14. 14. 4.]
print("np.percentile(x, 50, axis=1)=",np.percentile(x, 50, axis=1))#np.percentile(x, 50, axis=1)= [11. 13. 14. 13. 8.]
print("np.median(x, axis=1)=",np.median(x, axis=1))
# np.median(x, axis=1)= [11. 13. 14. 13. 8.]
6 计算平均值
numpy.mean(a[, axis=None, dtype=None, out=None, keepdims=np._NoValue)])
import numpy as np
np.random.seed(20201125)
x = np.random.randint(0, 20, size=[5, 5])
print("x=",x)
#x= [[ 3 13 15 6 11]
# [13 8 17 18 4]
# [19 1 14 17 3]
# [13 17 4 14 7]
# [ 8 19 6 14 0]]
y = np.mean(x)
print("y=",y) # y= 10.56
y = np.mean(x, axis=0)
print("y=",y) # y= [11.2 11.6 11.2 13.8 5. ]
y = np.mean(x, axis=1)
print("y=",y) # y= [ 9.6 12. 10.8 11. 9.4]
7 计算加权平均值
numpy.average(a[, axis=None, weights=None, returned=False])
mean和average都是计算均值的函数,在不指定权重的时候average和mean是一样的。指定权重后,average可以计算加权平均值
import numpy as np
np.random.seed(20201125)
x = np.random.randint(0, 20, size=[5, 5])
print("x=",x)
#x= [[ 3 13 15 6 11]
# [13 8 17 18 4]
# [19 1 14 17 3]
# [13 17 4 14 7]
# [ 8 19 6 14 0]]
y = np.average(x)
print("y=",y) # y= 10.56
y = np.average(x, axis=0)
print("y=",y) # y= [11.2 11.6 11.2 13.8 5. ]
y = np.average(x, axis=1)
print("y=",y) # y= [ 9.6 12. 10.8 11. 9.4]
y = np.arange(25, 50).reshape([5, 5])
print("y=",y)
# y= [[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]]
z = np.average(x, weights=y)
print("z=",z) # z= 10.467027027027028
z = np.average(x, axis=0, weights=y)
print("z=",z)
# z= [11.486 12.183 10.362 14.116 4.513]
z = np.average(x, axis=1, weights=y)
print("z=",z)
# z= [ 9.667 11.95 10.714 10.929 9.311]
8 计算方差
numpy.var(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])
方差公式中分母上是n;样本方差无偏估计公式中分母上是n-1(n为样本个数)。
import numpy as np
np.random.seed(20201125)
x = np.random.randint(20, 40, size=[5, 5])
print("x=",x)
# x= [[23 33 35 26 31]
# [33 28 37 38 24]
# [39 21 34 37 23]
# [33 37 24 34 27]
# [28 39 26 34 20]]
y = np.var(x)
print("y=",y) # y= 34.6464
y = np.mean((x - np.mean(x)) ** 2)
print("y=",y) # y= 34.6464
y = np.var(x, ddof=1)
print("y=",y) # y= 36.09
y = np.sum((x - np.mean(x)) ** 2) / (x.size - 1)
print("y=",y) # y= 36.09
y = np.var(x, axis=0)
print("y=",y) # y= [28.96 42.24 26.96 17.76 14. ]
y = np.var(x, axis=1)
print("y=",y) # y= [19.84 28.4 54.56 22.8 43.04]
9 计算标准差
numpy.std(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])
标准差是一组数据平均值分散程度的一种度量,是方差的算术平方根。
import numpy as np
np.random.seed(20201125)
x = np.random.randint(10, 30, size=[5, 5])
print("x=",x)
# x= [[13 23 25 16 21]
# [23 18 27 28 14]
# [29 11 24 27 13]
# [23 27 14 24 17]
# [18 29 16 24 10]]
y = np.std(x)
print("y=",y) # y= 5.886119264846746
y = np.sqrt(np.var(x))
print("y=",y) # y= 5.886119264846746
y = np.std(x, axis=0)
print("y=",y)
# y= [5.381 6.499 5.192 4.214 3.742]
y = np.std(x, axis=1)
print("y=",y)
# y= [4.454 5.329 7.386 4.775 6.56 ]
10 计算协方差矩阵
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)=",np.cov(x)) # np.cov(x)= 3.7 #样本方差
print("np.cov(y)=",np.cov(y)) # np.cov(y)= 8.5 #样本方差
print("np.cov(x,y)=",np.cov(x, y))
# np.cov(x,y)= [[3.7 5.25]
# [5.25 8.5 ]]
print("np.var(x)=",np.var(x)) # np.var(x)= 2.96 #方差
print("np.var(x, ddof=1)=",np.var(x, ddof=1)) # np.var(x, ddof=1)= 3.7 #样本方差
print("np.var(y)=",np.var(y)) # np.var(y)= 6.8 #方差
print("np.var(y, ddof=1)=",np.var(y, ddof=1)) # np.var(y, ddof=1)= 8.5 #样本方差
z = np.mean((x - np.mean(x)) * (y - np.mean(y))) #协方差
print("z=",z) # z= 4.2
z = np.sum((x - np.mean(x)) * (y - np.mean(y))) / (len(x) - 1) #样本协方差
print("z=",z) # z= 5.25
z = np.dot(x - np.mean(x), y - np.mean(y)) / (len(x) - 1) #样本协方差
print("z=",z) # z= 5.25
11 计算相关系数
numpy.corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue)
np.cov()描述的是两个向量协同变化的程度,它的取值可能非常大,也可能非常小,这就导致没法直观地衡量二者协同变化的程度。相关系数实际上是正则化的协方差,n个变量的相关系数形成一个n维方阵。
import numpy as np
np.random.seed(20201125)
x, y = np.random.randint(10, 60, size=(2, 4))
print("x=",x) # x= [37 37 13 23]
print("y=",y) # y= [57 48 53 34]
z = np.corrcoef(x, y)
print("z=",z)
# z= [[1. 0.216]
# [0.216 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)=",a / (b * c)) # a / (b * c)= 0.21571940307422416
12 直方图
numpy.digitize(x, bins, right=False)
x:numpy数组
bins:一维单调数组,必须是升序或者降序
right:间隔是否包含最右
返回值:x在bins中的位置。
import numpy as np
x = np.array([2.2, 5.4, 3.1, 1.9])
bins = np.array([0.3, 1.7, 2.6, 4.4, 10.8])
inds = np.digitize(x, bins)
print("inds=",inds) # [1 4 3 2]
for n in range(x.size):
print(bins[inds[n] - 1], "<=", x[n], "<", bins[inds[n]])
# 1.7 <= 2.2 < 2.6
# 4.4 <= 5.4 < 10.8
# 2.6 <= 3.1 < 4.4
# 1.7 <= 1.9 < 2.6
z = np.array([1.2, 10.0, 12.4, 15.5, 20.])
bins1 = np.array([0, 5, 10, 15, 20])
inds1 = np.digitize(z, bins1, right=True)
print("inds1=",inds1) # inds1= [1 2 3 4 4]
inds = np.digitize(z, bins1, right=False)
print("inds=",inds) # inds= [1 3 3 4 5]
参考文献
https://blog.csdn.net/qq_27326125/article/details/109632190