住宅価格を手計算で予測する
モデルの係数から手作業で予測値を計算できます。実務で予測を行うときは .predict() を使うほうがよいですが、手計算してみると予測は魔法ではなく、単なる計算だと実感できます。
実際、単回帰では予測値は「切片 + 傾き × 説明変数」にすぎません。
$$\text{response} = \text{intercept} + \text{slope} * \text{explanatory}$$
mdl_price_vs_conv と explanatory_data は利用可能です。
この演習はコースの一部です
Pythonで学ぶstatsmodelsによる回帰入門
演習の手順
mdl_price_vs_convの係数(パラメータ)を取得し、coeffsに代入します。- 切片(
coeffsの最初の要素)を取得し、interceptに代入します。 - 傾き(
coeffsの2番目の要素)を取得し、slopeに代入します。 - 切片、傾き、
explanatory_dataを用いて、式どおりにprice_twd_msqを手計算で予測します。 - コードを実行して、手計算の予測と
.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)))