本文主要是介绍回归中的相关度和R平方值(麦子学院),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
回归中的相关度和R平方值
import numpy as np
import math
import matplotlib.pylab as plt
from bokeh.charts.builders.scatter_builder import Scatterdef computeCorrelation(X, Y):xBar = np.mean(X)yBar = np.mean(Y)varX=0varY=0SSR = 0for i in range(len(X)):diffXXBar = X[i] - xBardiffYYbar = Y[i] - yBar SSR += diffXXBar*diffYYbarvarX += diffXXBar**2varY += diffYYbar**2SST= math.sqrt(varX*varY)return SSR/SST
def ployfit(x, y, degree):result={}coffs = np.polyfit(x,y, degree)result['polynomial']=coffs.tolist()
# print coffsp= np.poly1d(coffs)
# print pyhat = p(x)
# print yhat," ----"fig.scatter(x,yhat)ybar=np.sum(y)/len(y)ssreg=np.sum((yhat-ybar)**2)sstot=np.sum((y-ybar)**2)result['determination']=ssreg/sstotreturn result
fig=plt.subplot()
testX = [1, 3, 8, 7, 9]
testY = [10, 12, 24, 21, 34]
r = computeCorrelation(testX, testY)
print 'r:',r
print "r*r:",r*r
result=ployfit(testX, testY, 1)
print result
fig.scatter(testX,testY,color="green")
plt.show()
这篇关于回归中的相关度和R平方值(麦子学院)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!