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,SGDRegressor
from sklearn.metrics import mean_squared_error
def linear1():
boston=load_boston()
x_train,x_test,y_train,y_test=train_test_split(boston.data,boston.target,random_state=22)
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)
estimator=LinearRegression()
estimator.fit(x_train,y_train)
print("正规方程权重系数为:",estimator.coef_)#有几个特征就有几个偏重系数
print("正规方程偏置为:",estimator.intercept_)
y_predict=estimator.predict(x_test)
print("预测房价:",y_predict)
error=mean_squared_error(y_test,y_predict)
print("正规方程——均方误差:",error)
return None
def linear2():
boston=load_boston()
x_train,x_test,y_train,y_test=train_test_split(boston.data,boston.target,random_state=22)
transfer=StandardScaler()
x_train=transfer.fit_transform(x_train)
x_test=transfer.transform(x_test)
estimator=SGDRegressor(learning_rate="constant",eta0=0.001,max_iter=10000)#提高迭代次数max_iter可以降低均方误差
#用learning_rate让迭代次数变高,降低均方误差
estimator.fit(x_train,y_train)
print("梯度下降权重系数为:",estimator.coef_)
print("梯度下降偏置为:",estimator.intercept_)
y_predict=estimator.predict(x_test)
print("预测房价:",y_predict)
error=mean_squared_error(y_test,y_predict)
print("梯度下降——均方误差:",error)
return None
if __name__=="__main__":
linear1()
linear2()
正规方程权重系数为: [-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.6275137630954
梯度下降权重系数为: [-0.52169225 0.91970016 -0.40199357 0.82800455 -1.7173735 2.7815108
-0.12851882 -3.16098001 1.69753951 -0.96967716 -1.70915746 0.86449396
-3.88157843]
梯度下降偏置为: [22.61862011]
预测房价: [28.28198817 31.5702622 21.46118408 32.81364467 20.2092325 19.13476914
21.39078677 19.4259659 19.64811751 32.79152495 21.38347221 27.30336688
15.68472931 19.99221882 36.9258952 18.73578331 9.73501649 18.64226628
30.68035957 24.23406614 19.11260422 34.01419416 29.67879692 17.50565843
34.73610218 26.51317329 34.19242172 27.24944083 19.20164669 15.64865015
30.77374873 14.63492652 37.25983812 8.85715697 16.41881766 16.95037458
7.89886373 19.85701952 40.5218456 29.0612904 25.22163685 17.87485054
39.33698684 6.8965205 21.62313971 25.0432307 21.07756337 20.68331474
17.27413635 26.33636181 9.72474283 27.11420251 30.75906303 16.79965958
9.6693761 35.40988137 31.42475838 22.82111125 17.61524615 21.76997914
23.61207808 23.96129852 20.33710008 38.05699118 25.58823491 19.75245667
14.19236344 6.90569984 42.48955314 21.81359905 16.8262973 22.51075398
40.76590079 21.6587195 36.81037113 27.14948737 21.65878254 20.74932796
25.24844039 23.60903071 31.41461456 20.19792449 24.14398905 31.27775238
27.20061188 20.92235992 29.06911049 21.91390355 26.68841433 18.91845231
25.11834728 24.17238195 19.97337148 17.78783864 15.51273318 18.3465621
24.58303846 16.79720731 20.73971215 26.76429718 20.77959823 18.0424651
24.18005347 23.2830156 20.41364093 36.62913507 16.03211239 22.3756336
32.54252461 33.89251882 20.58302353 26.00931481 23.4373529 17.74346817
21.46961405 21.61176327 27.33270628 25.06028785 23.67679083 14.59077974
15.71984054 3.89638498 29.20773358 20.70605794 22.29300422 27.99641033
28.40600226]
梯度下降——均方误差: 21.26843613748394
|