首页 > 编程语言 > 详细

R平方、两个变量之间的关联程度,关联系数、正相关 负相关 样例代码(python3)

时间:2020-03-30 10:10:52      阅读:91      评论:0      收藏:0      [点我收藏+]

代码:

 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

 

R平方、两个变量之间的关联程度,关联系数、正相关 负相关 样例代码(python3)

原文:https://www.cnblogs.com/smartisn/p/12596092.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!