計算混淆矩陣
「混淆矩陣」(有時稱為「混淆表」)是對類別型應變數(例如邏輯斯迴歸)的模型,進行效能評估時的基礎。它包含每一組「實際反應-預測反應」配對的計數。本例中有兩種可能的反應(流失或未流失),因此總共有四種結果。
- 真正例(True positive): 客戶確實流失,模型也預測會流失。
- 偽正例(False positive): 客戶沒有流失,但模型預測會流失。
- 真負例(True negative): 客戶沒有流失,模型也預測不會流失。
- 偽負例(False negative): 客戶確實流失,但模型預測不會流失。
churn 與 mdl_churn_vs_relationship 已可使用。
本練習屬於課程
使用 Python 中的 statsmodels 進行回歸入門
練習說明
- 透過篩選資料集的
has_churned欄位取得實際反應,指派給actual_response。 - 從模型取得「最可能」的預測反應,指派給
predicted_response。 - 以
actual_response和predicted_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))