開始使用免費開始

預設閾值

你想確認 DecisionTreeClassifier() 是否使用與上一節提到的一樣的預設分類閾值,也就是 0.5。你覺得所有分類器都用相同閾值有點奇怪。來檢查一下吧!我們已經為你預先載入訓練好的決策樹分類器 clf,以及訓練與測試資料,名稱分別為 X_trainX_testy_trainy_test。你需要使用 .predict_proba() 方法,從分類器中取出機率分數。

本練習屬於課程

在 Python 設計機器學習工作流程

檢視課程

練習說明

  • 使用預先載入的分類器 clf,為測試樣本產生分數。
  • 接著從分數中取出標籤。記得每個樣本都有一對分數,而第二個元素代表正類別的機率。
  • 現在用標準的 .predict() 方法為測試資料產生標籤。
  • 最後,將結果與你先前得到的預測相比。它們是否完全相同?

動手互動練習

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

# Score the test data using the given classifier
scores = clf.____(____)

# Get labels from the scores using the default threshold
preds = [s[____] > ____ for s in scores]

# Use the predict method to label the test data again
preds_default = clf.____(____)

# Compare the two sets of predictions
____(preds == preds_default)
編輯並執行程式碼