R平方、两个变量之间的关联程度,关联系数、正相关 负相关 样例代码(python3)
代码:
1 # -*- coding:utf-8 -*- 2 3 import numpy as np 4 import math 5 6 7 # 相关度 8 def computeCorrelation(X, Y): 9 xBar = np.mean(X) 10 yBar = np.mean(Y) 11 SSR = 0 12 varX = 0 13 varY = 0 14 for i in range(0, len(X)): 15 diffXXbar = X[i] - xBar 16 diffYYbar = Y[i] - yBar 17 SSR += (diffXXbar * diffYYbar) 18 varX += diffXXbar ** 2 19 varY += diffYYbar ** 2 20 SST = math.sqrt(varX * varY) 21 return SSR / SST 22 23 24 25 26 # print("相关度r:", computeCorrelation(testX, testY)) 27 # 相关度r: 0.940310076545 28 29 # R平方 30 # 简单线性回归: 31 # print("r^2:", str(computeCorrelation(testX, testY)**2)) 32 # r^2: 0.884183040052 33 34 # 多个x自变量时: 35 def polyfit(x, y, degree): # degree自变量x次数 36 result = {} 37 coeffs = np.polyfit(x, y, degree) 38 result['polynomial'] = coeffs.tolist() 39 40 p = np.poly1d(coeffs) 41 yhat = p(x) 42 ybar = np.sum(y) / len(y) 43 ssreg = np.sum((yhat - ybar) ** 2) 44 sstot = np.sum((y - ybar) ** 2) 45 result['determination'] = ssreg / sstot 46 47 return result 48 49 # 测试 50 testX = [1, 3, 8, 7, 9] 51 testY = [1,1,1,1,1] 52 53 inf=float("inf") 54 ninf=float("-inf") 55 nan=float("nan") 56 result=polyfit(testY, testX, 1)["determination"] 57 58 print(inf) 59 if result == inf: 60 print("PPPP") 61 # 测试 62 print(result) 63 # r^2:0.884183040052