手动预测房价
您可以根据模型系数手动计算预测值。实际应用中进行预测更好的做法是使用 .predict(),但手动计算有助于让您确认预测并不神秘——本质上只是算术运算。
事实上,对于简单线性回归,预测值就是截距加上斜率乘以解释变量。
$$\text{response} = \text{intercept} + \text{slope} * \text{explanatory}$$
mdl_price_vs_conv 和 explanatory_data 已可用。
本练习是课程的一部分
使用 Python 中的 statsmodels 进行回归入门
练习说明
- 获取
mdl_price_vs_conv的系数/参数,并赋值给coeffs。 - 获取截距,即
coeffs的第 1 个元素,赋值给intercept。 - 获取斜率,即
coeffs的第 2 个元素,赋值给slope。 - 按照公式手动预测
price_twd_msq,指定截距、斜率和explanatory_data。 - 运行代码,将您手动计算的预测与
.predict()的结果进行比较。
交互式实操练习
通过完成这段示例代码来试试这个练习。
# Get the coefficients of mdl_price_vs_conv
coeffs = ____
# Get the intercept
intercept = ____
# Get the slope
slope = ____
# Manually calculate the predictions
price_twd_msq = ____
print(price_twd_msq)
# Compare to the results from .predict()
print(price_twd_msq.assign(predictions_auto=mdl_price_vs_conv.predict(explanatory_data)))