開始使用免費開始

計算混淆矩陣

「混淆矩陣」(有時稱為「混淆表」)是對類別型應變數(例如邏輯斯迴歸)的模型,進行效能評估時的基礎。它包含每一組「實際反應-預測反應」配對的計數。本例中有兩種可能的反應(流失或未流失),因此總共有四種結果。

  1. 真正例(True positive): 客戶確實流失,模型也預測會流失。
  2. 偽正例(False positive): 客戶沒有流失,但模型預測會流失。
  3. 真負例(True negative): 客戶沒有流失,模型也預測不會流失。
  4. 偽負例(False negative): 客戶確實流失,但模型預測不會流失。

churnmdl_churn_vs_relationship 已可使用。

本練習屬於課程

使用 Python 中的 statsmodels 進行回歸入門

檢視課程

練習說明

  • 透過篩選資料集的 has_churned 欄位取得實際反應,指派給 actual_response
  • 從模型取得「最可能」的預測反應,指派給 predicted_response
  • actual_responsepredicted_response 建立一個 DataFrame,指派給 outcomes
  • 以計數表格的形式列印 outcomes,作為混淆矩陣。這一步已為你完成。

動手互動練習

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

# Get the actual responses
actual_response = ____

# Get the predicted responses
predicted_response = ____

# Create outcomes as a DataFrame of both Series
outcomes = pd.DataFrame({____,
                         ____})

# Print the outcomes
print(outcomes.value_counts(sort = False))
編輯並執行程式碼