開始使用免費開始

RF 模型的效能指標

在前一個練習中,你已經取得隨機森林模型的 accuracy 分數。不過在詐欺偵測的情境下,我們知道「accuracy 可能會有誤導」。當詐欺資料高度不平衡時,AUROC 曲線是一個更可靠的效能指標,可用來比較不同分類器。此外,classification report 會告訴你模型的 precision 與 recall,而 confusion matrix 則會顯示實際能正確預測的詐欺案例數。現在就來取得這些效能指標。

你會延續上一題的隨機森林模型。你的模型定義為 model = RandomForestClassifier(random_state=5),已經對訓練資料完成擬合,且 X_train, y_train, X_test, y_test 都已可使用。

本練習屬於課程

Fraud Detection in Python

檢視課程

練習說明

  • 從 sklearn.metrics 匯入 classification report、confusion matrix 與 ROC score。
  • 取得你已訓練完成的隨機森林 model 的二元預測結果。
  • 執行 predict_proba() 以取得預測機率。
  • 將 y_test 與 predicted 比較,取得 classification report 與 confusion matrix。

動手互動練習

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

# Import the packages to get the different performance metrics
from sklearn.metrics import ____, ____, ____

# Obtain the predictions from our random forest model 
predicted = model.____(X_test)

# Predict probabilities
probs = ____.____(X_test)

# Print the ROC curve, classification report and confusion matrix
print(____(y_test, probs[:,1]))
print(____(____, predicted))
print(____(____, ____))
編輯並執行程式碼