皮尔逊系数
在统计学中,皮尔逊相关系数( 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)