手動計算預測值
與簡單線性迴歸相同,你也可以用模型係數手動計算預測值。平行斜率的情況只有一個差異:截距會隨著類別型解釋變數的每個類別而不同。也就是說,你需要分別處理每個類別所對應的情況。
taiwan_real_estate、mdl_price_vs_both 和 explanatory_data 已可使用;先前練習中的 ic_0_15、ic_15_30、ic_30_45 和 slope 也已載入。
本練習屬於課程
使用 Python 的 statsmodels 進行迴歸分析:中級
練習說明
- 建立一個清單
conditions,包含三個條件敘述:house_age_years為"0 to 15"、house_age_years為"15 to 30",以及house_age_years為"30 to 45"。 - 建立一個清單
choices,放入從mdl_price_vs_both擷取的截距。這些會對應到各個條件。 - 使用
np.select()建立截距的陣列。 - 建立
prediction_data:從explanatory_data開始,指派intercept為上述截距陣列,並將price_twd_msq設為手動計算的預測值。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
# Define conditions
conditions = [____]
# Define choices
choices = [____]
# Create array of intercepts for each house_age_year category
intercept = ____(____, ____)
# Create prediction_data with columns intercept and price_twd_msq
prediction_data = ____.____(
____ = ____,
____ = ____ + ____ * ____[____])
print(prediction_data)