Numpy_统计相关
参考文档/博客
分位数的计算.
无偏估计与方差.
协方差矩阵的解释.
dot()函数的解释.
1. 次序统计
1.1 计算最小值
numpy.amin(a[, axis=None, out=None, keepdims=np._NoValue, initial=np._NoValue, where=np._NoValue])
numpy中amin()方法中维度axis=0 1 2 的理解
axis=0 从最外一层的维度来比较(把两块面包变成一块面包)
axis=1 从中间一层的维度来比较(比较行,将多行压缩成了一行)
axis=2 从最内一层的维度来比较(比较列,把多列压缩成了一列)
import numpy as np
a = np.random.randint(2,40,size=(2,3,4))
print(a)
print("="*90)
print(np.amin(a,0))
print("="*90)
print(np.amin(a,1))
print("="*90)
print(np.amin(a,2))
print("="*90)
print(np.amin(a,(0,2)))
1.2 计算最大值
numpy.amax()计算最大值,参数和np.amin()一样
1.3 计算极差
numpy.ptp(a, axis=None, out=None, keepdims=np._NoValue)
最大数-最小数的值(ptp means ‘peak to peak’)
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]
1.4 计算分位数
numpy.percentile(a, q, axis=None, out=None, overwrite_input=False, interpolation=‘linear’, keepdims=False)
1)第一四分位数(Q1),又称“较小四分位数”,等于该样本中所有数值由小到大排列后第25%的数字;
2)第二四分位数(Q2),又称“中位数”,等于该样本中所有数值由小到大排列后第50%的数字;
3)第三四分位数(Q3),又称“较大四分位数”,等于该样本中所有数值由小到大排列后第75%的数字。
第三四分位数与第一四分位数的差距又称四分位距。
1.4.1 分位数解法1
求P分位点;(P是一个小于等于1的小数)
假如有n个点,但是实际上只有n-1个距离;所以直接使用n*p得到的位置的数字是有问题的,所以真正的P分位点的绝对位置为:(n - 1)*p + 1
那么问题就转化为了去求得这个位置上的数字,设绝对位置为X,X向下取整为Y,则有data[Y] + (data[Y+1] - data[Y]) * (X-Y)
1.4.2 分位数解法2
条件与解法1一样;
np这个数字一定是小于或者等于真正位置的,所以取大于等于np位置的第一数字;
设大于np的第一个整数为X,则有:
data[X]+(data[X+1]-data[X]) * (P-(X-1)/(N - 1)) / (N - 1);
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.]]
2. 均值与方差
2.1 中位数
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.]
2.2 平均值
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.]
2.3 加权平均值
numpy.average(a[, axis=None, weights=None, returned=False])
mean和average都是计算均值的函数,在不指定权重的时候average和mean是一样的。指定权重后,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)
print(z) # 27.0
z = np.average(x, axis=0, weights=y)
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]
2.4 计算方差
numpy.var(a[, axis=None, dtype=None, out=None, ddof=0, keepdims=np._NoValue])
要注意方差和样本方差的无偏估计,方差公式中分母上是n;
样本方差无偏估计公式中分母上是n-1(n为样本个数)。
ddof=0:是“Delta Degrees of Freedom”,表示自由度的个数。
方差的计算公式:
自由度:
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)
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.]
2.5 计算标准差
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) #直接计算标准差
y = np.sqrt(np.var(x)); print(y) #方差开根号
y = np.std(x, axis=0); print(y) #压缩行,计算标准差
y = np.std(x, axis=1); print(y) #压缩列计算标准差
3. 相关
3.1 计算协方差矩阵
numpy.cov(m, y=None, rowvar=True, bias=False, ddof=None, fweights=None,aweights=None)
协方差矩阵:
dot()返回的是两个数组的点积(dot product)
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] [c(x,x) c(c,y)]
# [5.25 8.5 ]] [c(y,x) c(y,y]
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))) #协方差
print(z) # 4.2
z = np.sum((x - np.mean(x)) * (y - np.mean(y))) / (len(x) - 1) #样本协方差
print(z) # 5.25
z = np.dot(x - np.mean(x), y - np.mean(y)) / (len(x) - 1) #样本协方差
print(z) # 5.25
3.2 计算相关系数
numpy.corrcoef(x, y=None, rowvar=True, bias=np._NoValue, ddof=np._NoValue)
np.cov()描述的是两个向量协同变化的程度,它的取值可能非常大,也可能非常小,这就导致没法直观地衡量二者协同变化的程度。相关系数实际上是正则化的协方差,n个变量的相关系数形成一个n维方阵。
Cov(x, y) / sqrt(D(x)) * sqrt(D(Y))
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. ]]
#约分直接约掉了1/n,所以这里都没有除n
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
4. 直方图
列子:
没有包括右端点;right=False
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])
inds = np.digitize(x, bins)
print(inds) # [1 4 3 2]
for n in range(x.size):
print(bins[inds[n] - 1], "<=", x[n], "<", bins[inds[n]])
#1 (-00, 0.0)
#2 [0.0, 1.0)
#3 [1.0, 2.5)
#4 [2.5, 4.0)
#5 [4.0, 10.0)
#6 [10.0, +00)
包括了右端点
right=True
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]
5. 练习
5.1 y=X β
对于简单线性回归,向量计法等同于
给定X跟y我们可以使用 NumPy 库解出β值
5.2 计算给定数组中每行的最大值
a = np.random.randint(1, 10, [5, 3])
如何在二维numpy数组的每一行中找到最大值?
解题
思路:使用max()函数,并且设置axis=1,这样就直接得到了数组每一行的最大值
代码:
import numpy as np
np.random.seed(20201127)
a = np.random.randint(1, 10, [5, 3])
print(a)
b = np.max(a, axis = 1)
print(b)
5.3 计算数组的元素最大值与最小值之差(极值)
数组为:
A=[[3 7 5]
[8 4 3]
[2 4 9]]
思路:使用ptp()函数,不用设置axis,直接得到了数组中的极差
import numpy as np
a = np.array([[3,7,5],
[8,4,3],
[2,4,9]])
print(np.ptp(a))
5.4 计算s的均值,方差,标准差,协方差
s=[9.7, 10, 10.3, 9.7,10,10.3,9.7,10,10.3]
import numpy as np
s=[9.7, 10, 10.3, 9.7,10,10.3,9.7,10,10.3]
a = np.array(s)
#均值 mean()
b = np.mean(a); print(b)
b = np.average(a); print(b)
#方差
b = np.var(a); print(b)
b = np.mean((a-np.mean(a))**2); print(b)
#标准差
b = np.std(a); print(b)
b = np.sqrt(np.mean((a - np.mean(a)) **2)); print(b)
#样本协方差
b = np.cov(a); print(b)
b = np.sum((a - np.mean(a)) ** 2) / (a.size - 1); print(b)
#协方差
b = np.mean((a - np.mean(a)) ** 2); print(b)
调味趣事:
对于协方差我是采用如下的方式计算的:
b = np.cov(a); print(b)
b = np.mean((a - np.mean(a)) ** 2); print(b)
后来发现两种方式得到的结果不一样;
后来发现原来cov()计算的是样本协方差.为此我纠结了一会儿并向小伙伴们请教,想着是不是函数内部有问题,有点儿小激动;结果是:原来又是自己短路了。打扰了,打扰了。