再來看一次混淆矩陣
在 Python 中建立混淆矩陣很簡單。最大的挑戰是確保你了解矩陣的排列方向。本練習要確認你理解 sklearn 對混淆矩陣的實作。在這裡,你已經使用 tic_tac_toe 資料集訓練出隨機森林模型 rfc,用來預測玩家一的結果是 0(輸)或 1(贏)。
注意: 如果你在其他網站或其他程式語言中查閱混淆矩陣,值的位置可能會相反。
本練習屬於課程
Python 的模型驗證
練習說明
- 匯入
sklearn用來建立混淆矩陣的函式。 - 使用模型
rfc,在測試集X_test上建立分類預測。 - 使用
sklearn建立一個混淆矩陣。 - 列印
cm中代表「實際為 1 且被預測為 1」的值(真陽性)。
動手互動練習
試著完成這個範例程式碼,體驗一下這個練習。
from sklearn.metrics import ____
# Create predictions
test_predictions = rfc.____(____)
# Create and print the confusion matrix
cm = ____(____, ____)
print(cm)
# Print the true positives (actual 1s that were predicted 1s)
print("The number of true positives is: {}".format(cm[____, ____]))