最小二乘法線性回歸:sklearn.linear_model.LinearRegression(fit_intercept=True, normalize=False,copy_X=True, n_jobs=1)
主要參數(shù)說(shuō)明:
fit_intercept:布爾型娶吞,默認(rèn)為True盖矫,若參數(shù)值為True時(shí),代表訓(xùn)練模型需要加一個(gè)截距項(xiàng);若參數(shù)為False時(shí)管行,代表模型無(wú)需加截距項(xiàng)格带。
normalize:布爾型,默認(rèn)為False饿悬,若fit_intercept參數(shù)設(shè)置False時(shí)令蛉,normalize參數(shù)無(wú)需設(shè)置;若normalize設(shè)置為True時(shí)狡恬,則輸入的樣本數(shù)據(jù)將(X-X均值)/||X||珠叔;若設(shè)置normalize=False時(shí),在訓(xùn)練模型前弟劲, 可以使用sklearn.preprocessing.StandardScaler進(jìn)行標(biāo)準(zhǔn)化處理祷安。
屬性:
coef_:回歸系數(shù)(斜率)
intercept_:截距項(xiàng)
主要方法:
①fit(X, y, sample_weight=None)
②predict(X)
③score(X, y, sample_weight=None),其結(jié)果等于1-(((y_true - y_pred) **2).sum() /?((y_true - y_true.mean()) ** 2).sum())
利用sklearn自帶的糖尿病數(shù)據(jù)集兔乞,建立最簡(jiǎn)單的一元回歸模型
In [1]:importnumpyasnp
...:fromsklearnimportdatasets , linear_model
...:fromsklearn.metricsimportmean_squared_error , r2_score
...:fromsklearn.model_selectionimporttrain_test_split
...:#加載糖尿病數(shù)據(jù)集
? ?...: diabetes = datasets.load_diabetes()
...: X = diabetes.data[:,np.newaxis ,2]#diabetes.data[:,2].reshape(diabetes
...: .data[:,2].size,1)
? ?...: y = diabetes.target
...: X_train , X_test , y_train ,y_test = train_test_split(X,y,test_size=0.2
...: ,random_state=42)
? ?...: LR = linear_model.LinearRegression()
? ?...: LR.fit(X_train,y_train)
...: print('intercept_:%.3f'% LR.intercept_)
...: print('coef_:%.3f'% LR.coef_)
...: print('Mean squared error: %.3f'% mean_squared_error(y_test,LR.predict
...: (X_test)))##((y_test-LR.predict(X_test))**2).mean()
...: print('Variance score: %.3f'% r2_score(y_test,LR.predict(X_test)))#1-(
...: (y_test-LR.predict(X_test))**2).sum()/((y_test - y_test.mean())**2).sum
? ?...: ()
...: print('score: %.3f'% LR.score(X_test,y_test))
...: plt.scatter(X_test , y_test ,color ='green')
...: plt.plot(X_test ,LR.predict(X_test) ,color='red',linewidth =3)
? ?...: plt.show()
? ?...:
intercept_:152.003
coef_:998.578
Mean squared error:4061.826
Variance score:0.233
score:0.233
效果如下: