20201214 线性回归实现 波士顿房价预测

1、波士顿预测房价
from sklearn.datasets import load_boston

from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge
from sklearn.preprocessing import StandardScaler
from sklearn.datasets import load_boston
from sklearn.metrics import mean_squared_error,classification_report
from sklearn.cluster import KMeans


boston = load_boston()
print(boston)
from sklearn.model_selection  import train_test_split
import numpy as np
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.25)

# 训练与测试数据标准化处理
from sklearn.preprocessing import StandardScaler

std_x = StandardScaler()
std_y = StandardScaler()

x_train = std_x.fit_transform(X_train)
x_test = std_x.transform(X_test)
y_train = std_y.fit_transform(y_train.reshape(-1,1))
y_test = std_y.transform(y_test.reshape(-1,1))

# 估计器流程
# 正规方程求解
lr = LinearRegression()
# 训练系数
lr.fit(x_train, y_train)
# 打印回归系数
print(lr.coef_)

y_lr_predict = lr.predict(x_test)

y_lr_predict = std_y.inverse_transform(y_lr_predict)

print("Lr预测值:", y_lr_predict)

# SGDRegressor 梯度下降预测
sgd = SGDRegressor()

sgd.fit(x_train, y_train)

print(sgd.coef_)
y_sgd_predict = sgd.predict(x_test)

y_sgd_predict = std_y.inverse_transform(y_sgd_predict)

print("SGD预测值:", y_sgd_predict)

# 带有正则化的岭回归

rd = Ridge(alpha=0.01)

rd.fit(x_train, y_train)

y_rd_predict = rd.predict(x_test)

y_rd_predict = std_y.inverse_transform(y_rd_predict)

print(rd.coef_)

# 两种模型评估结果   数据量下使用lr, 大使用sgd

print("lr的均方误差为:", mean_squared_error(std_y.inverse_transform(y_test), y_lr_predict))

print("SGD的均方误差为:", mean_squared_error(std_y.inverse_transform(y_test), y_sgd_predict))

print("Ridge的均方误差为:", mean_squared_error(std_y.inverse_transform(y_test), y_rd_predict))
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值