多元回归、岭回归、Lasso回归——python预测房子价格

数据

多元回归、岭回归、Lasso回归——python预测房子价格_第1张图片

导入包

from sklearn.datasets import load_boston
from sklearn.linear_model import LinearRegression, SGDRegressor, Ridge, LogisticRegression, Lasso
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error, classification_report, roc_auc_score
import joblib #保存训练好的模型和参数
import pandas as pd
import numpy as np

获取数据和标准化

# 获取数据
lb = load_boston()
# 分割数据集到训练集和测试集
x_train, x_test, y_train, y_test = train_test_split(lb.data, lb.target, test_size=0.25, random_state=1)
# 特征值和目标值是都必须进行标准化处理, 实例化两个标准化API
std_x = StandardScaler()
x_train = std_x.fit_transform(x_train)
x_test = std_x.transform(x_test)

# 目标值进行了标准化
std_y = StandardScaler()
y_train = std_y.fit_transform(y_train.reshape(-1, 1))  # y_train是一维的,这里需要传进去2维的,reshape(-1, 1)增加一个维度,-1代表把剩余元素都堆到那一维
y_test = std_y.transform(y_test.reshape(-1, 1))

线性回归的正规方程求解

# 正规方程求解方式预测结果,正规方程进行线性回归
lr = LinearRegression()
# #
lr.fit(x_train, y_train)
#
print('回归系数', lr.coef_)  #回归系数可以看特征与目标之间的相关性
#
y_predict = lr.predict(x_test)
# 预测测试集的房子价格,通过inverse得到真正的房子价格
y_lr_predict = std_y.inverse_transform(y_predict)
# 保存训练好的模型
joblib.dump(lr, "./tmp/test.pkl")
print("正规方程测试集里面每个房子的预测价格:", y_lr_predict)
print("正规方程的均方误差:", mean_squared_error(y_test, y_predict))
结果:
回归系数 [[-0.12026411  0.15044778  0.02951803  0.07470354 -0.28043353  0.22170939
   0.02190624 -0.35275513  0.29939558 -0.2028089  -0.23911894  0.06305081
  -0.45259462]]
正规方程测试集里面每个房子的预测价格: [[32.37816533]
 [27.95684437]
 [18.07213891]
   ......
 [21.16402252]
 [26.00459084]]
正规方程的均方误差: 0.2758842244225052
正规方程inverse后的均方误差: 21.897765396049476                     

线性回归的梯度下降求解

# 梯度下降去进行房价预测,数据量大要用这个
# 默认可以去调 eta0 = 0.008,会改变learning_rate
# learning_rate='optimal',alpha会影响学习率的值,由alpha来算学习率
sgd = SGDRegressor(eta0=0.008, penalty='l1', alpha=0.005)
# # 训练
sgd.fit(x_train, y_train)

print('梯度下降的回归系数', sgd.coef_)

# 预测测试集的房子价格
y_sgd_predict = std_y.inverse_transform(sgd.predict(x_test).reshape(-1, 1))
y_predict = sgd.predict(x_test)
print("梯度下降测试集里面每个房子的预测价格:", y_sgd_predict)
print("梯度下降的均方误差:", mean_squared_error(y_test, y_predict))
print("梯度下降的原始房价量纲均方误差:", mean_squared_error(std_y.inverse_transform(y_test), y_sgd_predict))
梯度下降的回归系数 [-0.09161381  0.07894594 -0.01997965  0.07736127 -0.18054122  0.26622108
  0.         -0.23891603  0.09441201 -0.02523685 -0.22153748  0.06690733
 -0.4268276 ]
梯度下降测试集里面每个房子的预测价格: [[30.32788625]
 [28.2472966 ]
 [18.30943245]
 [22.59556785]
    ......
 [21.59354866]
 [25.98775391]]
梯度下降的均方误差: 0.2782778128254135
梯度下降的原始房价量纲均方误差: 22.087751747792876

岭回归求解

# 岭回归去进行房价预测
rd = Ridge(alpha=0.05) #lasso回归是Lasso
rd.fit(x_train, y_train)
print(rd.coef_)

# # 预测测试集的房子价格
y_predict = rd.predict(x_test)
y_rd_predict = std_y.inverse_transform(y_predict)

# print("岭回归里面每个房子的预测价格:", y_rd_predict)
print("岭回归的均方误差:", mean_squared_error(y_test, y_predict))
print("岭回归的均方误差:", mean_squared_error(std_y.inverse_transform(y_test), y_rd_predict))
[[-0.12019408  0.15027489  0.02932631  0.07472724 -0.28019156  0.22179958
   0.0218258  -0.35250679  0.29879635 -0.20224632 -0.23906031  0.06305591
  -0.45246484]]
岭回归的均方误差: 0.27588055100713926
岭回归的均方误差: 21.897473825960407

Lasso回归

ls = Lasso(alpha=0.05) 
ls.fit(x_train, y_train)
print(ls.coef_)

# # 预测测试集的房子价格
y_predict = ls.predict(x_test) #Lasso回归把y_predict改成一维了
y_ls_predict = std_y.inverse_transform(y_predict.reshape(-1,1))#需要二维的

# print("岭回归里面每个房子的预测价格:", y_ls_predict)
print("Lasso回归的均方误差:", mean_squared_error(y_test, y_predict))
print("Lasso回归的均方误差:", mean_squared_error(std_y.inverse_transform(y_test), y_ls_predict))
[-0.02365963  0.         -0.          0.04558048 -0.03498273  0.25429207
 -0.         -0.02930847  0.         -0.         -0.18612471  0.02840277
 -0.43680997]
Lasso回归的均方误差: 0.34915312700364426
Lasso回归的均方误差: 27.71333982009004

你可能感兴趣的:(数学建模算法与应用,机器学习,python,回归,机器学习)