Pythonのピアソン相関係数の三つの方法(ピアソン相関係数)

ピアソン係数0

 統計に、また、ピアソンの積率相関係数(ピアソンの積率相関係数、又は単にPPMCCのPCCと呼ばれる)として知られるピアソン相関係数(ピアソン相関係数)。-1と1の間の範囲内の2つの変数XとYとの間の線形相関関係の尺度。
ここに画像を挿入説明

計算1パイソン

私は、ユーザーが自分のニーズや比較に応じて使用できる3つの方法を見つけました:

1.1式の手書きによると、

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の機能

pccs = np.corrcoef(x, y)

機能で1.3 scipy.stats

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

おすすめ

転載: blog.csdn.net/qq_40260867/article/details/90667462