ML之MLiR:输入两个向量,得出两个向量之间的相关度
【摘要】 ML之MLiR:输入两个向量,得出两个向量之间的相关度
目录
输出结果
实现代码
输出结果
实现代码
import numpy as npfrom astropy.units import Ybarnimport math from statsm...
ML之MLiR:输入两个向量,得出两个向量之间的相关度
目录
输出结果
实现代码
-
import numpy as np
-
from astropy.units import Ybarn
-
import math
-
from statsmodels.graphics.tukeyplot import results
-
-
-
def computeCorrelation(X, Y):
-
xBar = np.mean(X)
-
yBar = np.mean(Y)
-
SSR = 0
-
varX = 0
-
varY = 0
-
for i in range(0 , len(X)):
-
diffXXBar = X[i] - xBar
-
diffYYBar = Y[i] - yBar
-
SSR += (diffXXBar * diffYYBar)
-
varX += diffXXBar**2
-
varY += diffYYBar**2
-
-
SST = math.sqrt(varX * varY)
-
return SSR / SST
-
-
testX = [1, 3, 8, 7, 9]
-
testY = [10, 12, 24, 21, 34]
-
-
print ("r:",computeCorrelation(testX, testY))
-
-
def polyfit(x,y,degree):
-
results={}
-
-
coeffs =np.polyfit(x,y,degree)
-
results['polynomial'] = coeffs.tolist()
-
p=np.poly1d(coeffs)
-
yhat=p(x)
-
ybar=np.sum(y)/len(y)
-
ssreg=np.sum((yhat-ybar)**2)
-
sstot=np.sum((y-ybar)**2)
-
results['determination']=ssreg/sstot
-
-
return results
-
-
print (polyfit(testX, testY, 1)["determination"])
相关文章
文章来源: yunyaniu.blog.csdn.net,作者:一个处女座的程序猿,版权归原作者所有,如需转载,请联系作者。
原文链接:yunyaniu.blog.csdn.net/article/details/80019995
【版权声明】本文为华为云社区用户转载文章,如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)