【机器学习入门笔记】第九篇-回归

线性回归的损失:真实值和随意假定的差(成本函数,目标函数cost)

优化损失-损失最小-最小二乘法

优化方法?

正规方程(天才)直接求解W

梯度下降(小镇做题家)先给一个权重值,试错改进,如果数据很大的话,梯度下降结果可能更好

波士顿房价预测步骤:
1)获取数据集
2)划分数据集
3)特征工程
4)预估器流程
    fit()-->模型
    coef_intercept_
5)模型评估
 

1.正规方程

# 1)获取数据集
from sklearn.datasets import load_boston
boston = load_boston()

# 2)划分数据集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, random_state=22);

# 3)特征工程
from sklearn.preprocessing import StandardScaler
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)

# 4)预估器流程--正规方程
from sklearn.linear_model import LinearRegression
estimator = LinearRegression()
estimator.fit(x_train,y_train)

#5)得出模型
print("正规方程权重系数为:\n",estimator.coef_)
print("正规方程偏置为:\n",estimator.intercept_)
print("特征数量",boston.data.shape)

from sklearn.metrics import mean_squared_error
#6)模型评估
y_predict = estimator.predict(x_test)
print("预测房价:\n",y_predict)
error = mean_squared_error(y_test,y_predict)
print("正规方程-均方误差:\n",error)

2.梯度下降

# 1)获取数据集
from sklearn.datasets import load_boston
boston = load_boston()

# 2)划分数据集
from sklearn.model_selection import train_test_split
x_train, x_test, y_train, y_test = train_test_split(boston.data, boston.target, test_size=0.2, random_state=22);

# 3)特征工程
from sklearn.preprocessing import StandardScaler
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)

# 4)预估器流程--梯度下降
from sklearn.linear_model import SGDRegressor
estimator1 = SGDRegressor(eta0=0.01,max_iter=10000)
estimator1.fit(x_train,y_train)
#5)得出模型-梯度下降
print("梯度下降权重系数为:\n",estimator1.coef_)
print("梯度下降偏置为:\n",estimator1.intercept_)

from sklearn.metrics import mean_squared_error
#6)模型评估
y_predict = estimator1.predict(x_test)
print("预测房价:\n",y_predict)
error1 = mean_squared_error(y_test,y_predict)
print("正规方程-均方误差:\n",error1)

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值