欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Python--计算相关系数

程序员文章站 2024-03-07 21:40:33
...

计算两个列表变量的相关系数

Python--计算相关系数

#计算特征和类的平均值
def calcMean(x,y):
   sum_x = sum(x)
   sum_y = sum(y)
   n = len(x)
   if n==0:
	   return 0,0
   else:
	   x_mean = float(sum_x+0.0)/n
	   y_mean = float(sum_y+0.0)/n
	   return x_mean,y_mean


#计算Pearson系数
def calcPearson(x,y):
    x_mean,y_mean = calcMean(x,y)   #计算x,y向量平均值
    n = len(x)
    sumTop = 0.0
    sumBottom = 0.0
    x_pow = 0.0
    y_pow = 0.0
    for i in range(n):
        sumTop += (x[i]-x_mean)*(y[i]-y_mean)
    for i in range(n):
        # x_pow += math.pow(x[i]-x_mean,2)
        x_pow += (x[i] - x_mean)*(x[i]-x_mean)
    for i in range(n):
        # y_pow += math.pow(y[i]-y_mean,2)
        y_pow += (y[i] - y_mean) * (y[i] - y_mean)
    sumBottom = sqrt(x_pow*y_pow)
    p = sumTop/sumBottom
    return p

相关标签: Python 相关系数