方法一:
import numpy as np
# 计算相关系数R
def r(y_true, y_pred):
y_true = np.array(y_true)
y_pred = np.array(y_pred)
corr = np.corrcoef(y_true, y_pred)[0][1]
return corr
corr = r(yture, ypred)
方法二
import scipy.stats
# 计算皮尔逊相关指数,并进行显著性检验
def get_pex(x,y):
aaa = scipy.stats.pearsonr(x, y)
print('皮尔逊系数是:{},显著性检测值是:{}'.format(aaa[0],aaa[1]))
pex_r = get_pex(x, y)[0]
pex_p = get_pex(x, y)[1]
参考:https://mp.weixin.qq.com/s/nABJf4WBR4Y4oEZiYKPK4Q