開始使用免費開始

手動計算預測值

與簡單線性迴歸相同,你也可以用模型係數手動計算預測值。平行斜率的情況只有一個差異:截距會隨著類別型解釋變數的每個類別而不同。也就是說,你需要分別處理每個類別所對應的情況。

taiwan_real_estatemdl_price_vs_bothexplanatory_data 已可使用;先前練習中的 ic_0_15ic_15_30ic_30_45slope 也已載入。

本練習屬於課程

使用 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)
編輯並執行程式碼