本篇文章給大家分享的是有關怎么在python中使用sklearn實現(xiàn)線性回歸,小編覺得挺實用的,因此分享給大家學習,希望大家閱讀完這篇文章后可以有所收獲,話不多說,跟著小編一起來看看吧。

python常用的庫:1.requesuts;2.scrapy;3.pillow;4.twisted;5.numpy;6.matplotlib;7.pygama;8.ipyhton等。
使用一階線性方程預測波士頓房價
載入的數(shù)據(jù)是隨sklearn一起發(fā)布的,來自boston 1993年之前收集的506個房屋的數(shù)據(jù)和價格。load_boston()用于載入數(shù)據(jù)。
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
import time
from sklearn.linear_model import LinearRegression
boston = load_boston()
X = boston.data
y = boston.target
print("X.shape:{}. y.shape:{}".format(X.shape, y.shape))
print('boston.feature_name:{}'.format(boston.feature_names))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=3)
model = LinearRegression()
start = time.clock()
model.fit(X_train, y_train)
train_score = model.score(X_train, y_train)
cv_score = model.score(X_test, y_test)
print('time used:{0:.6f}; train_score:{1:.6f}, sv_score:{2:.6f}'.format((time.clock()-start),
train_score, cv_score))輸出內(nèi)容為:
X.shape:(506, 13). y.shape:(506,) boston.feature_name:['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO' 'B' 'LSTAT'] time used:0.012403; train_score:0.723941, sv_score:0.794958
可以看到測試集上準確率并不高,應該是欠擬合。
使用多項式做線性回歸
上面的例子是欠擬合的,說明模型太簡單,無法擬合數(shù)據(jù)的情況。現(xiàn)在增加模型復雜度,引入多項式。
打個比方,如果原來的特征是[a, b]兩個特征,
在degree為2的情況下, 多項式特征變?yōu)閇1, a, b, a^2, ab, b^2]。degree為其它值的情況依次類推。
多項式特征相當于增加了數(shù)據(jù)和模型的復雜性,能夠更好的擬合。
下面的代碼使用Pipeline把多項式特征和線性回歸特征連起來,最終測試degree在1、2、3的情況下的得分。
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
import time
from sklearn.linear_model import LinearRegression
from sklearn.preprocessing import PolynomialFeatures
from sklearn.pipeline import Pipeline
def polynomial_model(degree=1):
polynomial_features = PolynomialFeatures(degree=degree, include_bias=False)
linear_regression = LinearRegression(normalize=True)
pipeline = Pipeline([('polynomial_features', polynomial_features),
('linear_regression', linear_regression)])
return pipeline
boston = load_boston()
X = boston.data
y = boston.target
print("X.shape:{}. y.shape:{}".format(X.shape, y.shape))
print('boston.feature_name:{}'.format(boston.feature_names))
X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.2, random_state=3)
for i in range(1,4):
print( 'degree:{}'.format( i ) )
model = polynomial_model(degree=i)
start = time.clock()
model.fit(X_train, y_train)
train_score = model.score(X_train, y_train)
cv_score = model.score(X_test, y_test)
print('time used:{0:.6f}; train_score:{1:.6f}, sv_score:{2:.6f}'.format((time.clock()-start),
train_score, cv_score))輸出結果為:
X.shape:(506, 13). y.shape:(506,) boston.feature_name:['CRIM' 'ZN' 'INDUS' 'CHAS' 'NOX' 'RM' 'AGE' 'DIS' 'RAD' 'TAX' 'PTRATIO' 'B' 'LSTAT'] degree:1 time used:0.003576; train_score:0.723941, sv_score:0.794958 degree:2 time used:0.030123; train_score:0.930547, sv_score:0.860465 degree:3 time used:0.137346; train_score:1.000000, sv_score:-104.429619
可以看到degree為1和上面不使用多項式是一樣的。degree為3在訓練集上的得分為1,在測試集上得分是負數(shù),明顯過擬合了。
所以最終應該選擇degree為2的模型。
二階多項式比一階多項式好的多,但是測試集和訓練集上的得分仍有不少差距,這可能是數(shù)據(jù)不夠的原因,需要更多的訊據(jù)才能進一步提高模型的準確度。
正規(guī)方程解法和梯度下降的比較
除了梯度下降法來逼近最優(yōu)解,也可以使用正規(guī)的方程解法直接計算出最終的解來。
根據(jù)吳恩達的課程,線性回歸最優(yōu)解為:
theta = (X^T * X)^-1 * X^T * y
其實兩種方法各有優(yōu)缺點:
梯度下降法:
缺點:需要選擇學習率,需要多次迭代
優(yōu)點:特征值很多(1萬以上)時仍然能以不錯的速度工作
正規(guī)方程解法:
優(yōu)點:不需要設置學習率,不需要多次迭代
缺點:需要計算X的轉置和逆,復雜度O3;特征值很多(1萬以上)時特變慢
在分類等非線性計算中,正規(guī)方程解法并不適用,所以梯度下降法適用范圍更廣。
以上就是怎么在python中使用sklearn實現(xiàn)線性回歸,小編相信有部分知識點可能是我們?nèi)粘9ぷ鲿姷交蛴玫降摹OM隳芡ㄟ^這篇文章學到更多知識。更多詳情敬請關注創(chuàng)新互聯(lián)行業(yè)資訊頻道。
名稱欄目:怎么在python中使用sklearn實現(xiàn)線性回歸-創(chuàng)新互聯(lián)
當前鏈接:http://www.chinadenli.net/article46/pceeg.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供微信小程序、標簽優(yōu)化、企業(yè)網(wǎng)站制作、企業(yè)建站、虛擬主機、App設計
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉載,或轉載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容