一、概率论数理统计常见的统计量Python实现总结
1、求数学期望
#coding=utf-8
importnumpy asnp
arr = [1,2,3,4,5,6]
#1、数学期望(俗称平均值)
num_avg = np.mean(arr)
print(num_avg)
2、求方差、标准差
#coding=utf-8
importnumpy asnp
arr = [1,2,3,4,5,6]
# 求方差
num_var = np.var(arr)
print(num_var)
# 求标准差
num_std = np.std(arr,ddof=1)
print(num_std)
3、求协方差
#coding=utf-8
importnumpy asnp
#求协方差
x=np.array([[1 ,2 ,3] ,
[2 ,5 ,6 ],
[ 7 ,8 ,9],
[ 11 ,11 ,12]])
cov_xy = np.cov(x)
print(cov_xy)
二、求相关系数的Python实现总结
1、公式法
#coding=utf-8
importnumpy
importpandas
X = [1,2,3,4,5]
Y = [1.01 , 2.02 , 3.03 ,4.04 , 5.05]
# 均值
XMean = numpy.mean(X)
YMean = numpy.mean(Y)
#标准差
XSD = numpy.std(X)
YSD = numpy.std(Y)
#z分数
ZX = (X-XMean)/XSD
ZY = (Y-YMean)/YSD#相关系数
r = numpy.sum(ZX*ZY)/(len(X))
print(r)
2、通过numpy的corrcoef方法计算相关性系数
#coding=utf-8
importnumpy
X = [10.11, 20.11, 33.11]
Y = [10.22, 20.22, 30.22 ]
t=numpy.corrcoef(X,Y)