使用boston房价数据进行线性回归分析
理解數(shù)據(jù)
import pandas as pd import numpy as npimport sklearn.datasets as datasets from sklearn.linear_model import LinearRegression,SGDRegressor from sklearn.model_selection import train_test_split from sklearn.preprocessing import StandardScaler from sklearn.metrics import mean_squared_error,classification_report import matplotlib.pyplot as plt from sklearn.linear_model import Ridge from sklearn.externals import joblib1.加載boston房?jī)r(jià)數(shù)據(jù)
boston=datasets.load_boston() print(boston.DESCR) boston.feature_names X=boston.data Y=boston.targetboston房?jī)r(jià)數(shù)據(jù)一共有506行。13個(gè)特征,分別是:
‘CRIM’, ‘ZN’, ‘INDUS’, ‘CHAS’, ‘NOX’, ‘RM’, ‘AGE’, ‘DIS’, ‘RAD’,
‘TAX’, ‘PTRATIO’, ‘B’, ‘LSTAT’;
各個(gè)特征的含義:
CRIM:城鎮(zhèn)人均犯罪率。
ZN:住宅用地超過(guò) 25000 sq.ft. 的比例。
INDUS:城鎮(zhèn)非零售商用土地的比例。
CHAS:查理斯河空變量(如果邊界是河流,則為1;否則為0)。
NOX:一氧化氮濃度。
RM:住宅平均房間數(shù)。
AGE:1940 年之前建成的自用房屋比例。
DIS:到波士頓五個(gè)中心區(qū)域的加權(quán)距離。
RAD:輻射性公路的接近指數(shù)。
TAX:每 10000 美元的全值財(cái)產(chǎn)稅率。
PTRATIO:城鎮(zhèn)師生比例。
B:1000(Bk-0.63)^ 2,其中 Bk 指代城鎮(zhèn)中黑人的比例。
LSTAT:人口中地位低下者的比例。
MEDV:自住房的平均房?jī)r(jià),以千美元計(jì)
2.分割數(shù)據(jù)為訓(xùn)練集測(cè)試集,并可視化各個(gè)特征與目標(biāo)值的關(guān)系
X=boston.data Y=boston.target X_train,X_test,y_train,y_test=train_test_split(X,Y,random_state=33,test_size=0.2) #標(biāo)準(zhǔn)化 std=StandardScaler() x_train=std.fit_transform(X_train) x_test=std.transform(X_test) y_train=std.fit_transform(y_train.reshape(-1,1)) #y需要轉(zhuǎn)化為2維 y_test=std.transform(y_test.reshape(-1,1))可視化
#可視化 plt.rcParams['font.sans-serif']=['SimHei'] plt.rcParams['axes.unicode_minus']=False titles=['CRIM', 'ZN', 'INDUS', 'CHAS', 'NOX', 'RM', 'AGE', 'DIS', 'RAD','TAX', 'PTRATIO', 'B', 'LSTAT'] plt.figure(figsize=(12,9)) for i in range(13):plt.subplot(4,4,i+1)plt.scatter(x_train[:,i],y_train,marker='.',color='g')plt.xlabel(titles[i])plt.ylabel('$1000-price')plt.title(str(i+1)+'.'+titles[i]+'-price') plt.tight_layout() plt.suptitle('各屬性―地価との関連',x=0.5,y=1.05,fontsize=20) plt.show()
3.分別使用LinearRegression,SGDRegressor,Ridge進(jìn)行預(yù)測(cè)
LinearRegression,SGDRegressor的區(qū)別
小規(guī)模數(shù)據(jù):LinearRegression(不能解決擬合問(wèn)題)以及其它
大規(guī)模數(shù)據(jù):SGDRegressor
Ridge嶺回歸解決過(guò)擬合問(wèn)題。
① 正則化LinearRegression
lr=LinearRegression() lr.fit(x_train,y_train)y_lr_pred=std.inverse_transform(lr.predict(x_test)) error_lr=mean_squared_error(std.inverse_transform(y_test),y_lr_pred) print('斜率:',lr.coef_) print('截距:',lr.intercept_) print('均方誤差:',error_lr)得出結(jié)果:
斜率: [[-0.10652353 0.1248883 0.02144814 0.08447264 -0.1851724 0.3015255
-0.00436415 -0.33801186 0.28858221 -0.23677719 -0.19424453 0.07916941
-0.43398872]]
截距: [4.18819804e-15]
均方誤差: 22.04257921621329
② 梯度下降法SGDRegressor
sgd=SGDRegressor() sgd.fit(x_train,y_train) y_sgd_pred=std.inverse_transform(sgd.predict(x_test)) error_sgd=mean_squared_error(std.inverse_transform(y_test),y_sgd_pred) print('斜率:',sgd.coef_) print('截距:',sgd.intercept_) print('均方誤差:',error_sgd)梯度下降法SGDRegressor結(jié)果:
斜率: [-0.08556966 0.08415892 -0.02406228 0.09539855 -0.12186342 0.32945003
-0.01042155 -0.28569839 0.15298948 -0.08681529 -0.18069349 0.07871156
-0.41953704]
截距: [0.00377308]
均方誤差: 22.341906975421544
③ Ridge
rd=Ridge(alpha=1.0) rd.fit(x_train,y_train) print(rd.coef_) print(rd.intercept_) y_rd_pred=std.inverse_transform(rd.predict(x_test)) print('嶺回歸下均方誤差:',mean_squared_error(std.inverse_transform(y_test),y_rd_pred))Ridge結(jié)果:
斜率:[[-0.10546956 0.12265543 0.01810155 0.08491327 -0.18178902 0.30268784
-0.00504133 -0.33433398 0.27964259 -0.2279551 -0.19351241 0.07916974
-0.43224374]]
截距:[4.19852261e-15]
嶺回歸下均方誤差: 21.982909442895018
4.保存模型
joblib.dump(rd,'./ri.pkl')結(jié)論
由于boston房?jī)r(jià)數(shù)據(jù)量不大,使用LinearRegression預(yù)測(cè)效果比SGDRegressor梯度下降法效果要好。
總結(jié)
以上是生活随笔為你收集整理的使用boston房价数据进行线性回归分析的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: C语言系列文章之#和##
- 下一篇: WWDC22 - In App Purc