其他分享
首页 > 其他分享> > 波士顿房价

波士顿房价

作者:互联网

'''
正规方程的优化方法对波士顿房价进行预测
'''
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.linear_model import LinearRegression
from sklearn.metrics import mean_squared_error
#1.获取数据
boston=load_boston()
#2.划分数据集
x_train,x_test,y_train,y_test=train_test_split(boston.data,boston.target,random_state=22)
#3.标准化
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)

#4.预估器
estimator=LinearRegression()
estimator.fit(x_train,y_train)

#5.得出模型
print("正规方程权重系数为:\n",estimator.coef_)
print("正规方程偏置为:\n",estimator.intercept_)

#6.模型评估
y_predict=estimator.predict(x_test)
print("预测房价:\n",y_predict)
error=mean_squared_error(y_test,y_predict)
print("正规方程_均方误差:\n",error)
正规方程权重系数为:
 [-0.64817766  1.14673408 -0.05949444  0.74216553 -1.95515269  2.70902585
 -0.07737374 -3.29889391  2.50267196 -1.85679269 -1.75044624  0.87341624
 -3.91336869]
正规方程偏置为:
 22.62137203166228
预测房价:
 [28.22944896 31.5122308  21.11612841 32.6663189  20.0023467  19.07315705
 21.09772798 19.61400153 19.61907059 32.87611987 20.97911561 27.52898011
 15.54701758 19.78630176 36.88641203 18.81202132  9.35912225 18.49452615
 30.66499315 24.30184448 19.08220837 34.11391208 29.81386585 17.51775647
 34.91026707 26.54967053 34.71035391 27.4268996  19.09095832 14.92742976
 30.86877936 15.88271775 37.17548808  7.72101675 16.24074861 17.19211608
  7.42140081 20.0098852  40.58481466 28.93190595 25.25404307 17.74970308
 38.76446932  6.87996052 21.80450956 25.29110265 20.427491   20.4698034
 17.25330064 26.12442519  8.48268143 27.50871869 30.58284841 16.56039764
  9.38919181 35.54434377 32.29801978 21.81298945 17.60263689 22.0804256
 23.49262401 24.10617033 20.1346492  38.5268066  24.58319594 19.78072415
 13.93429891  6.75507808 42.03759064 21.9215625  16.91352899 22.58327744
 40.76440704 21.3998946  36.89912238 27.19273661 20.97945544 20.37925063
 25.3536439  22.18729123 31.13342301 20.39451125 23.99224334 31.54729547
 26.74581308 20.90199941 29.08225233 21.98331503 26.29101202 20.17329401
 25.49225305 24.09171045 19.90739221 16.35154974 15.25184758 18.40766132
 24.83797801 16.61703662 20.89470344 26.70854061 20.7591883  17.88403312
 24.28656105 23.37651493 21.64202047 36.81476219 15.86570054 21.42338732
 32.81366203 33.74086414 20.61688336 26.88191023 22.65739323 17.35731771
 21.67699248 21.65034728 27.66728556 25.04691687 23.73976625 14.6649641
 15.17700342  3.81620663 29.18194848 20.68544417 22.32934783 28.01568563
 28.58237108]
正规方程_均方误差:
 20.627513763095386
'''
梯度下降的优化方法对波士顿房价进行预测
'''
from sklearn.linear_model import SGDRegressor
#1.获取数据
boston=load_boston()
#2.划分数据集
x_train,x_test,y_train,y_test=train_test_split(boston.data,boston.target,random_state=22)
#3.标准化
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)

#4.预估器
estimator=SGDRegressor()
estimator.fit(x_train,y_train)

#5.得出模型
print("梯度下降权重系数为:\n",estimator.coef_)
print("梯度下降偏置为:\n",estimator.intercept_)

#6.模型评估
y_predict=estimator.predict(x_test)
print("预测房价:\n",y_predict)
error=mean_squared_error(y_test,y_predict)
print("梯度下降_均方误差:\n",error)
梯度下降权重系数为:
 [-0.55721319  0.97886344 -0.30258341  0.75140173 -1.80715079  2.75101609
 -0.09617487 -3.2405617   1.90444639 -1.14646757 -1.72644276  0.84988455
 -3.88921227]
梯度下降偏置为:
 [22.63190541]
预测房价:
 [28.27262077 31.5587518  21.39257091 32.54594714 20.13852631 19.21924644
 21.34691145 19.39631039 19.58249632 32.89778737 21.338191   27.47584805
 15.74160915 20.01851003 36.97791289 18.77898024  9.69532686 18.60519974
 30.61785614 24.20272373 19.21663454 34.03431538 29.49601412 17.63254054
 34.74536091 26.54316582 34.23655755 27.20370819 19.3068318  15.39689811
 30.79209476 14.99428238 37.16396376  8.74623138 16.36797688 17.16917637
  7.91535295 20.0087943  40.23023668 28.92521534 25.18927847 17.9597703
 39.0339977   7.05187937 21.77660406 25.10767904 20.73331587 20.70214894
 17.06644872 26.40372885  9.61444626 27.14632848 30.46987392 16.87224017
  9.78441668 35.44291381 31.58048502 22.54374775 17.61460137 21.73821941
 23.61161115 23.99616366 20.24756604 38.11902172 25.31033443 19.88917343
 14.28033321  7.02418857 42.18756534 21.80528928 17.04740699 22.41310088
 40.73654367 21.53865267 36.82045222 27.16440967 21.37158236 20.75385469
 25.18527414 23.20983203 31.31565789 20.19361438 23.8576267  31.24885329
 27.13757713 21.01085085 29.09483028 21.81596432 26.60811643 19.27022862
 25.17338541 23.94774911 20.07210721 17.5113141  15.60723653 18.46316106
 24.62142162 16.91704258 20.91286153 26.75218678 20.90941336 18.11310462
 24.28981284 23.33489181 20.73866029 36.40024275 15.97811352 22.12388334
 32.55742565 33.76993946 20.59369245 26.25181159 23.04759622 17.65175512
 21.44963085 21.51243344 27.29652409 24.93803433 23.70676048 14.75482349
 15.75578947  4.05446281 29.20403408 20.80416148 22.26363731 27.95973198
 28.55668256]
梯度下降_均方误差:
 21.17469762487764
'''
回归性能评估
均方误差
'''

标签:predict,房价,boston,print,train,estimator,test,波士顿
来源: https://blog.csdn.net/qq_43751489/article/details/110678671