python皮尔森相关性分析_python 皮尔森相关系数的实现

个人原创版:

from math import sqrt

def multipl(a,b):

sumofab=0.0

for i in range(len(a)):

temp=a[i]*b[i]

sumofab+=temp

return sumofab

def pearson(x,y):

N=len(x)

sumofx=sum(x)

sumofy=sum(y)

sumofxy=multipl(x,y)

sumofxx=multipl(x,x)

sumofyy=multipl(y,y)

upside=sumofxy-sumofx*sumofy/N

downside=sqrt((sumofxx-float(sumofx**2)/N)*(sumofyy-float(sumofy**2)/N)) #此处要转成浮点数保证精度

return upside/downside

参考书本代码:

def pearson(x,y):

n=len(x)

vals=range(n)

# Simple sums

sumx=sum([float(x[i]) for i in vals])

sumy=sum([float(y[i]) for i in vals])

# Sum up the squares

sumxSq=sum([x[i]**2.0 for i in vals])

sumySq=sum([y[i]**2.0 for i in vals])

# Sum up the products

pSum=sum([x[i]*y[i] for i in vals])

# Calculate Pearson score

num=pSum-(sumx*sumy/n)

den=((sumxSq-pow(sumx,2)/n)*(sumySq-pow(sumy,2)/n))**.5

if den==0: return 0

r=num/den

return r

你可能感兴趣的:(python皮尔森相关性分析)