1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > Python--如何计算皮尔逊相关系数(Pearson correlation coefficient)

Python--如何计算皮尔逊相关系数(Pearson correlation coefficient)

时间:2023-10-17 03:27:12

相关推荐

Python--如何计算皮尔逊相关系数(Pearson correlation coefficient)

皮尔逊系数

在统计学中,皮尔逊相关系数( Pearson correlation coefficient),又称皮尔逊积矩相关系数(Pearson product-moment correlation coefficient,简称 PPMCC或PCCs)。用于衡量两个变量X和Y之间的线性相关相关关系,值域在-1与1之间。

python计算方法

有三种方式

根据公式手写

def cal_pccs(x, y, n):"""warning: data format must be narray:param x: Variable 1:param y: The variable 2:param n: The number of elements in x:return: pccs"""sum_xy = np.sum(np.sum(x*y))sum_x = np.sum(np.sum(x))sum_y = np.sum(np.sum(y))sum_x2 = np.sum(np.sum(x*x))sum_y2 = np.sum(np.sum(y*y))pcc = (n*sum_xy-sum_x*sum_y)/np.sqrt((n*sum_x2-sum_x*sum_x)*(n*sum_y2-sum_y*sum_y))return pcc

numpy的函数

pccs = np.corrcoef(x, y)

scipy.stats中的函数

from scipy.stats import pearsonrpccs = pearsonr(x, y)

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。