波士顿房价预测¶
学习目标¶
- 掌握Sklearn中线性回归相关API的使用方法
- 掌握模型保存和加载的方法
1 线性回归API¶
sklearn.linear_model.LinearRegression(fit_intercept=True)
- 通过正规方程优化
- 参数:fit_intercept,是否计算偏置
- 属性:LinearRegression.coef_ (回归系数) LinearRegression.intercept_(偏置)
sklearn.linear_model.SGDRegressor(loss="squared_loss", fit_intercept=True, learning_rate ='constant', eta0=0.01)
- SGDRegressor类实现了随机梯度下降学习,它支持不同的 损失函数和正则化惩罚项 来拟合线性回归模型。
- 参数:loss(损失函数类型),fit_intercept(是否计算偏置)learning_rate (学习率)
- 属性:SGDRegressor.coef_ (回归系数)SGDRegressor.intercept_ (偏置)
sklearn提供给我们两种实现的API, 可以根据选择使用
2 案例:波士顿房价预测¶
2.1 案例背景介绍¶
数据介绍
给定的这些特征,是专家们得出的影响房价的结果属性。我们此阶段不需要自己去探究特征是否有用,只需要使用这些特征。到后面量化很多特征需要我们自己去寻找
2.2 案例分析¶
回归当中的数据大小不一致,是否会导致结果影响较大。所以需要做标准化处理。
- 数据分割与标准化处理
- 回归预测
- 线性回归的算法效果评估
2.3 回归性能评估¶
均方误差(Mean Squared Error, MSE)评价机制:
\(\Large MSE = \frac{1}{m}\sum_{i=1}^{m}(y^i-\hat{y})^2\)
sklearn中的API:sklearn.metrics.mean_squared_error(y_true, y_pred)
- 均方误差回归损失
- y_true:真实值
- y_pred:预测值
- return:浮点数结果
2.4 代码实现¶
导包
from sklearn.datasets import load_boston
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import mean_squared_error
from sklearn.linear_model import SGDRegressor
from sklearn.linear_model import LinearRegression
正规方程
def linear_model1():
"""
线性回归:正规方程
:return:None
"""
# 1.获取数据
data = load_boston()
# 2.数据集划分
x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, random_state=22)
# 3.特征工程-标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.transform(x_test)
# 4.机器学习-线性回归(正规方程)
estimator = LinearRegression()
estimator.fit(x_train, y_train)
# 5.模型评估
# 5.1 获取系数等值
y_predict = estimator.predict(x_test)
print("预测值为:\n", y_predict)
print("模型中的系数为:\n", estimator.coef_)
print("模型中的偏置为:\n", estimator.intercept_)
# 5.2 评价
# 均方误差
error = mean_squared_error(y_test, y_predict)
print("误差为:\n", error)
return None
梯度下降法
def linear_model2():
"""
线性回归:梯度下降法
:return:None
"""
# 1.获取数据
data = load_boston()
# 2.数据集划分
x_train, x_test, y_train, y_test = train_test_split(data.data, data.target, random_state=22)
# 3.特征工程-标准化
transfer = StandardScaler()
x_train = transfer.fit_transform(x_train)
x_test = transfer.fit_transform(x_test)
# 4.机器学习-线性回归(特征方程)
estimator = SGDRegressor(max_iter=1000)
estimator.fit(x_train, y_train)
# 5.模型评估
# 5.1 获取系数等值
y_predict = estimator.predict(x_test)
print("预测值为:\n", y_predict)
print("模型中的系数为:\n", estimator.coef_)
print("模型中的偏置为:\n", estimator.intercept_)
# 5.2 评价
# 均方误差
error = mean_squared_error(y_test, y_predict)
print("误差为:\n", error)
return None
我们也可以尝试去修改学习率
estimator = SGDRegressor(max_iter=1000,learning_rate="constant",eta0=0.1)
此时我们可以通过调参数,找到学习率效果更好的值。
3.模型的保存和加载¶
sklearn模型的保存和加载API import joblib
- 保存:joblib.dump(estimator, 'test.pkl')
- 加载:estimator = joblib.load('test.pkl')
在上面的案例中, 我们可以添加保存模型的代码
joblib.dump(estimator, 'test.pkl')
将模型保存之后随时可以加载模型,并进行预测
estimator = joblib.load('test.pkl')
y_predict = estimator.predict(x_test)
print("预测值为:\n", y_predict)
print("模型中的系数为:\n", estimator.coef_)
print("模型中的偏置为:\n", estimator.intercept_)
4. 小结¶
正规方程
- sklearn.linear_model.LinearRegression()
梯度下降法
- sklearn.linear_model.SGDRegressor()
线性回归性能评估
- 均方误差:sklearn.metrics.mean_squared_error
模型加载与保存:
- 保存:joblib.dump(estimator, 'test.pkl')
- 加载:estimator = joblib.load('test.pkl')