波士顿房价预测实验

复制代码
 1 from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression
 2 from sklearn.datasets import load_boston
 3 from sklearn.model_selection import train_test_split
 4 from sklearn.preprocessing import StandardScaler
 5 from sklearn.metrics import mean_squared_error
 6 import joblib
 7 from sklearn.metrics import r2_score
 8 from sklearn.neural_network import MLPRegressor
 9 
10 import pandas as pd
11 import numpy as np
12 
13 lb = load_boston()
14 x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.2)
15 
16 
17 # 为数据增加一个维度,相当于把[1, 5, 10] 变成 [[1, 5, 10],]
18 y_train = y_train.reshape(-1, 1)
19 y_test = y_test.reshape(-1, 1)
20 
21 # 进行标准化
22 std_x = StandardScaler()
23 x_train = std_x.fit_transform(x_train)
24 x_test = std_x.transform(x_test)
25 
26 std_y = StandardScaler()
27 y_train = std_y.fit_transform(y_train)
28 y_test = std_y.transform(y_test)
复制代码

 


 

1 # 正规方程预测
2 lr = LinearRegression()
3 lr.fit(x_train, y_train)
4 print("r2 score of Linear regression is",r2_score(y_test,lr.predict(x_test)))

 

 


 

1 #岭回归
2 from sklearn.linear_model import RidgeCV
3 
4 cv = RidgeCV(alphas=np.logspace(-3, 2, 100))
5 cv.fit (x_train , y_train)
6 print("r2 score of Linear regression is",r2_score(y_test,cv.predict(x_test)))

 

 


 

1 #梯度下降
2 sgd = SGDRegressor()
3 sgd.fit(x_train, y_train)
4 print("r2 score of Linear regression is",r2_score(y_test,sgd.predict(x_test)))

 

posted @   靠谱杨  阅读(310)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 分享4款.NET开源、免费、实用的商城系统
· 全程不用写代码,我用AI程序员写了一个飞机大战
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· 白话解读 Dapr 1.15:你的「微服务管家」又秀新绝活了
· 上周热点回顾(2.24-3.2)
历史上的今天:
2021-03-24 高德地图和echarts结合实现地图下钻(二)

喜欢请打赏

扫描二维码打赏

了解更多

点击右上角即可分享
微信分享提示