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]])
#用np
y = np.var(x)print(y)
52.0
#直接计算
y = np.mean((np.mean(x)-x)**2)print(y)
52.0
y = np.var(x,ddof =1)#ddof就是自由度要设置为1才是无偏的。也就是分母用n-1替换n。print(y)
54.166666666666664
y = np.mean((np.mean(x)-x)**2)/(x.size-1)print(y)
2.1666666666666665
2.3计算标准差
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)
y
7.211102550927978
y = np.sqrt(np.var(x))
y
7.211102550927978
3.相关
3.1计算协方差矩阵
#协方差(Covariance)在概率论和统计学中用于衡量两个变量的总体误差。而方差是协方差的一种特殊情况,即当两个变量是相同的情况。import numpy as np
x =[1,2,3,4,6]
y =[0,2,5,6,7]print(np.cov(x))#是无偏的方差 n-1print(np.var(x))print(np.cov(y))print(np.cov(x,y))#https://blog.csdn.net/xueluowutong/article/details/85334256
3.7
2.96
8.5
[[3.7 5.25]
[5.25 8.5 ]]
print(x,y)
[1, 2, 3, 4, 6] [0, 2, 5, 6, 7]
3.2计算相关系数
import numpy as np
np.random.seed(2020)
x,y = np.random.randint(0,20,size =(2,4))print(x)print(y)
[0 8 3 3]
[ 3 7 16 0]
z = np.corrcoef(x,y)
z
array([[1. , 0.18793271],
[0.18793271, 1. ]])
4.直方图
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)for n inrange(x.size):print(bins[inds[n]-1],'<=',x[n],'<',bins[inds[n]])