開始使用免費開始

比較預測值

在前一個練習中,你已經使用 crab 資料,分別擬合了線性迴歸與 GLM(邏輯斯)模型,以 width 預測 y。換句話說,你想根據雌蟹的寬度,預測牠附近是否有附著蟹(satellite crab)的機率。

在本練習中,你會進一步檢視兩個模型輸出的機率估計值,並嘗試判斷線性擬合是否適合這個問題。

一般做法是用新的、尚未看過的資料來測試模型。這樣的資料集稱為測試樣本(test sample)。
test 樣本已為你建立並載入至工作區。請注意,你必須為模型中出現的所有變數提供測試值,在本例中是 width

crab 資料集已預先載入至工作區。

本練習屬於課程

Generalized Linear Models in Python

檢視課程

練習說明

  • 使用 print() 檢視 test 集。
  • 使用 test 樣本,對已擬合的線性模型 model_LM 呼叫 .predict() 計算機率估計並存為 pred_lm。同樣地,對已擬合的 GLM(邏輯斯)模型 model_GLM 呼叫 .predict() 並存為 pred_glm
  • 使用 pandasDataFrame() 合併兩個模型的預測結果並存為 predictions
  • testpredictions 串接後存為 all_data。使用 print() 檢視 all_data

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# View test set
print(____)

# Compute estimated probabilities for linear model: pred_lm
____ = model_LM.____(____)

# Compute estimated probabilities for GLM model: pred_glm
____ = model_GLM.____(____)

# Create dataframe of predictions for linear and GLM model: predictions
____ = pd.DataFrame({'Pred_LM': ____, 'Pred_GLM': ____})

# Concatenate test sample and predictions and view the results
all_data = pd.concat([____, ____], axis = 1)
print(____)
編輯並執行程式碼