import numpy as np
# 皮尔逊基相关系数的度量
def PEXJWork(x, y):
n = len(x)
#均值
x_u = np.mean(x) # 20
y_u = np.mean(y) # 20
x1 = x - x_u # -2 1 1 0
y1 = y - y_u # 1 -2 0 1
#方差
re1 = np.multiply(x1, y1) # -2 -2 0 0
re2 = np.sum(re1) # -4
s_xy = re2 / (n - 1) # -1.33333
#标准差
s_x = np.std(x, ddof=1) # 1.4142135623730951
s_y = np.std(y, ddof=1) # 1.4142135623730951
p_xy = s_xy / (s_x * s_y) # -0.6666666666666665
return p_xy
if __name__ == "__main__":
x = np.array([18,21,21,20]) # XL
y = np.array([21, 18, 20, 21]) # XR
p_xy = PEXJWork(x, y)
print('p_xy=', p_xy)
皮尔逊基相关系数的度量
最新推荐文章于 2022-10-13 18:48:11 发布