Python Pearson correlation coefficient three methods (Pearson correlation coefficient)

Pearson coefficient 0

 In statistics, the Pearson correlation coefficient (Pearson correlation coefficient), also known as the Pearson product-moment correlation coefficient (Pearson product-moment correlation coefficient, or simply referred to PPMCC PCCs). A measure of linear correlation relationship between two variables X and Y, in the range between -1 and 1.
Here Insert Picture Description

Calculation 1 python

I found three ways users can use according to their needs or comparison:

1.1 According to the formula handwriting

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

1.2 numpy function

pccs = np.corrcoef(x, y)

1.3 scipy.stats in function

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

Guess you like

Origin blog.csdn.net/qq_40260867/article/details/90667462