手動預測房價
你可以用模型係數手動計算預測值。實務上做預測時,最好使用 .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)))