线性回归的损失:真实值和随意假定的差(成本函数,目标函数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)