Python 線性回歸分析以及評價指標(biāo)詳解
廢話不多說,直接上代碼吧!
""" # 利用 diabetes數(shù)據(jù)集來學(xué)習(xí)線性回歸 # diabetes 是一個關(guān)于糖尿病的數(shù)據(jù)集, 該數(shù)據(jù)集包括442個病人的生理數(shù)據(jù)及一年以后的病情發(fā)展情況。 # 數(shù)據(jù)集中的特征值總共10項, 如下: # 年齡 # 性別 #體質(zhì)指數(shù) #血壓 #s1,s2,s3,s4,s4,s6 (六種血清的化驗數(shù)據(jù)) #但請注意,以上的數(shù)據(jù)是經(jīng)過特殊處理, 10個數(shù)據(jù)中的每個都做了均值中心化處理,然后又用標(biāo)準(zhǔn)差乘以個體數(shù)量調(diào)整了數(shù)值范圍。 #驗證就會發(fā)現(xiàn)任何一列的所有數(shù)值平方和為1. """ import matplotlib.pyplot as plt import numpy as np from sklearn import datasets, linear_model from sklearn.metrics import mean_squared_error, r2_score # Load the diabetes dataset diabetes = 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 sets diabetes_X_train = diabetes_X[:-20] diabetes_X_test = diabetes_X[-20:] # Split the targets into training/testing sets diabetes_y_train = diabetes.target[:-20] diabetes_y_test = diabetes.target[-20:] # Create linear regression object regr = linear_model.LinearRegression() # Train the model using the training sets regr.fit(diabetes_X_train, diabetes_y_train) # Make predictions using the testing set diabetes_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→0 print('Variance score: %.2f' % r2_score(diabetes_y_test, diabetes_y_pred)) # Plot outputs plt.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(誤差平方和)
計算公式如下:
同樣的數(shù)據(jù)集的情況下,SSE越小,誤差越小,模型效果越好
缺點:
SSE數(shù)值大小本身沒有意義,隨著樣本增加,SSE必然增加,也就是說,不同的數(shù)據(jù)集的情況下,SSE比較沒有意義
二、R-square(決定系數(shù))
數(shù)學(xué)理解: 分母理解為原始數(shù)據(jù)的離散程度,分子為預(yù)測數(shù)據(jù)和原始數(shù)據(jù)的誤差,二者相除可以消除原始數(shù)據(jù)離散程度的影響
其實“決定系數(shù)”是通過數(shù)據(jù)的變化來表征一個擬合的好壞。
理論上取值范圍(-∞,1], 正常取值范圍為[0 1] ------實際操作中通常會選擇擬合較好的曲線計算R²,因此很少出現(xiàn)-∞
越接近1,表明方程的變量對y的解釋能力越強,這個模型對數(shù)據(jù)擬合的也較好
越接近0,表明模型擬合的越差
經(jīng)驗值:>0.4, 擬合效果好
缺點:
數(shù)據(jù)集的樣本越大,R²越大,因此,不同數(shù)據(jù)集的模型結(jié)果比較會有一定的誤差
三、Adjusted R-Square (校正決定系數(shù))
n為樣本數(shù)量,p為特征數(shù)量
消除了樣本數(shù)量和特征數(shù)量的影響
以上這篇Python 線性回歸分析以及評價指標(biāo)詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
pycharm 2019 最新激活方式(pycharm破解、激活)
這篇文章主要介紹了最新2019pycharm激活方式(pycharm破解、激活),本文給大家介紹的非常詳細(xì),具有一定的參考借鑒價值,需要的朋友可以參考下2020-01-01AI生成圖片Stable?Diffusion環(huán)境搭建與運行方法
Stable?Diffusion是一種基于擴(kuò)散過程的生成模型,由Ge?et?al.在2021年提出,該模型利用了隨機變量的穩(wěn)定分布,通過遞歸地應(yīng)用擴(kuò)散過程來生成高質(zhì)量的圖像,這篇文章主要介紹了AI圖片生成Stable?Diffusion環(huán)境搭建與運行,需要的朋友可以參考下2023-05-05python3編寫ThinkPHP命令執(zhí)行Getshell的方法
這篇文章主要介紹了python3編寫ThinkPHP命令執(zhí)行Getshell的方法,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-02-02python3.6.5基于kerberos認(rèn)證的hive和hdfs連接調(diào)用方式
這篇文章主要介紹了python3.6.5基于kerberos認(rèn)證的hive和hdfs連接調(diào)用方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-06-06