计算两个列表变量的相关系数
#计算特征和类的平均值
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