政策资讯

Policy Information


ML之MLiR:输入两个向量,得出两个向量之间的相关度

来源: 重庆市软件正版化服务中心    |    时间: 2022-09-20    |    浏览量: 64969    |   

ML之MLiR:输入两个向量,得出两个向量之间的相关度

目录

输出结果

实现代码


输出结果

实现代码

  1. import numpy as np
  2. from astropy.units import Ybarn
  3. import math
  4. from statsmodels.graphics.tukeyplot import results
  5. def computeCorrelation(X, Y):
  6. xBar = np.mean(X)
  7. yBar = np.mean(Y)
  8. SSR = 0
  9. varX = 0
  10. varY = 0
  11. for i in range(0 , len(X)):
  12. diffXXBar = X[i] - xBar
  13. diffYYBar = Y[i] - yBar
  14. SSR += (diffXXBar * diffYYBar)
  15. varX += diffXXBar**2
  16. varY += diffYYBar**2
  17. SST = math.sqrt(varX * varY)
  18. return SSR / SST
  19. testX = [1, 3, 8, 7, 9]
  20. testY = [10, 12, 24, 21, 34]
  21. print ("r:",computeCorrelation(testX, testY))
  22. def polyfit(x,y,degree):
  23. results={}
  24. coeffs =np.polyfit(x,y,degree)
  25. results['polynomial'] = coeffs.tolist()
  26. p=np.poly1d(coeffs)
  27. yhat=p(x)
  28. ybar=np.sum(y)/len(y)
  29. ssreg=np.sum((yhat-ybar)**2)
  30. sstot=np.sum((y-ybar)**2)
  31. results['determination']=ssreg/sstot
  32. return results
  33. print (polyfit(testX, testY, 1)["determination"])

 

相关文章

ML之MLiR:输入两个向量,得出两个向量之间的相关度

 

 

评论

QQ咨询 扫一扫加入群聊,了解更多平台咨询
微信咨询 扫一扫加入群聊,了解更多平台咨询
意见反馈
立即提交
QQ咨询
微信咨询
意见反馈