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))
20201214 线性回归实现 波士顿房价预测
最新推荐文章于 2023-10-20 16:58:17 发布