Python 線性回歸分析以及評價指標(biāo)詳解
廢話不多說,直接上代碼吧!
'''# 利用 diabetes數(shù)據(jù)集來學(xué)習(xí)線性回歸 # diabetes 是一個關(guān)于糖尿病的數(shù)據(jù)集, 該數(shù)據(jù)集包括442個病人的生理數(shù)據(jù)及一年以后的病情發(fā)展情況。 # 數(shù)據(jù)集中的特征值總共10項(xiàng), 如下: # 年齡 # 性別 #體質(zhì)指數(shù) #血壓 #s1,s2,s3,s4,s4,s6 (六種血清的化驗(yàn)數(shù)據(jù)) #但請注意,以上的數(shù)據(jù)是經(jīng)過特殊處理, 10個數(shù)據(jù)中的每個都做了均值中心化處理,然后又用標(biāo)準(zhǔn)差乘以個體數(shù)量調(diào)整了數(shù)值范圍。 #驗(yàn)證就會發(fā)現(xiàn)任何一列的所有數(shù)值平方和為1. ''' import matplotlib.pyplot as pltimport numpy as npfrom sklearn import datasets, linear_modelfrom sklearn.metrics import mean_squared_error, r2_score # Load the diabetes datasetdiabetes = datasets.load_diabetes() # Use only one feature # 增加一個維度,得到一個體質(zhì)指數(shù)數(shù)組[[1],[2],...[442]]diabetes_X = diabetes.data[:, np.newaxis,2]print(diabetes_X) # Split the data into training/testing setsdiabetes_X_train = diabetes_X[:-20]diabetes_X_test = diabetes_X[-20:] # Split the targets into training/testing setsdiabetes_y_train = diabetes.target[:-20]diabetes_y_test = diabetes.target[-20:] # Create linear regression objectregr = linear_model.LinearRegression() # Train the model using the training setsregr.fit(diabetes_X_train, diabetes_y_train) # Make predictions using the testing setdiabetes_y_pred = regr.predict(diabetes_X_test) # The coefficients # 查看相關(guān)系數(shù) print(’Coefficients: n’, regr.coef_) # The mean squared error # 均方差# 查看殘差平方的均值(mean square error,MSE) print('Mean squared error: %.2f' % mean_squared_error(diabetes_y_test, diabetes_y_pred)) # Explained variance score: 1 is perfect prediction # R2 決定系數(shù)(擬合優(yōu)度)# 模型越好:r2→1# 模型越差:r2→0print(’Variance score: %.2f’ % r2_score(diabetes_y_test, diabetes_y_pred)) # Plot outputsplt.scatter(diabetes_X_test, diabetes_y_test, color=’black’)plt.plot(diabetes_X_test, diabetes_y_pred, color=’blue’, linewidth=3) plt.xticks(())plt.yticks(()) plt.show()
對于回歸模型效果的判斷指標(biāo)經(jīng)過了幾個過程,從SSE到R-square再到Ajusted R-square, 是一個完善的過程:
SSE(誤差平方和):The sum of squares due to error
R-square(決定系數(shù)):Coefficient of determination
Adjusted R-square:Degree-of-freedom adjusted coefficient of determination
下面我對以上幾個名詞進(jìn)行詳細(xì)的解釋下,相信能給大家?guī)硪欢ǖ膸椭。?/p>
一、SSE(誤差平方和)
計(jì)算公式如下:
同樣的數(shù)據(jù)集的情況下,SSE越小,誤差越小,模型效果越好
缺點(diǎn):
SSE數(shù)值大小本身沒有意義,隨著樣本增加,SSE必然增加,也就是說,不同的數(shù)據(jù)集的情況下,SSE比較沒有意義
二、R-square(決定系數(shù))
數(shù)學(xué)理解: 分母理解為原始數(shù)據(jù)的離散程度,分子為預(yù)測數(shù)據(jù)和原始數(shù)據(jù)的誤差,二者相除可以消除原始數(shù)據(jù)離散程度的影響
其實(shí)“決定系數(shù)”是通過數(shù)據(jù)的變化來表征一個擬合的好壞。
理論上取值范圍(-∞,1], 正常取值范圍為[0 1] ------實(shí)際操作中通常會選擇擬合較好的曲線計(jì)算R²,因此很少出現(xiàn)-∞
越接近1,表明方程的變量對y的解釋能力越強(qiáng),這個模型對數(shù)據(jù)擬合的也較好
越接近0,表明模型擬合的越差
經(jīng)驗(yàn)值:>0.4, 擬合效果好
缺點(diǎn):
數(shù)據(jù)集的樣本越大,R²越大,因此,不同數(shù)據(jù)集的模型結(jié)果比較會有一定的誤差
三、Adjusted R-Square (校正決定系數(shù))
n為樣本數(shù)量,p為特征數(shù)量
消除了樣本數(shù)量和特征數(shù)量的影響
以上這篇Python 線性回歸分析以及評價指標(biāo)詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持好吧啦網(wǎng)。
相關(guān)文章:
1. XML入門的常見問題(四)2. 使用css實(shí)現(xiàn)全兼容tooltip提示框3. 詳解CSS偽元素的妙用單標(biāo)簽之美4. 詳解PHP實(shí)現(xiàn)HTTP服務(wù)器過程5. 低版本IE正常運(yùn)行HTML5+CSS3網(wǎng)站的3種解決方案6. html小技巧之td,div標(biāo)簽里內(nèi)容不換行7. ASP刪除img標(biāo)簽的style屬性只保留src的正則函數(shù)8. XML入門的常見問題(一)9. HTML DOM setInterval和clearInterval方法案例詳解10. ASP中格式化時間短日期補(bǔ)0變兩位長日期的方法
