始める無料で始める

住宅価格を手計算で予測する

モデルの係数から手作業で予測値を計算できます。実務で予測を行うときは .predict() を使うほうがよいですが、手計算してみると予測は魔法ではなく、単なる計算だと実感できます。

実際、単回帰では予測値は「切片 + 傾き × 説明変数」にすぎません。

$$\text{response} = \text{intercept} + \text{slope} * \text{explanatory}$$

mdl_price_vs_convexplanatory_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)))
コードを編集して実行