開始使用免費開始

平衡類別

類別不平衡會明顯影響預測結果,這可從 recallaccuracy 的差異看出。為了解決不平衡,通常會為每個類別賦予相同的權重。使用 sklearnDecisionTreeClassifier 中的 class_weight 參數,可以讓類別變為 "balanced"

現在來修正模型的類別不平衡問題:

  • 先建立一個具有平衡類別的模型
  • 接著,將它擬合到訓練資料
  • 最後,在測試集上檢查它的準確率

變數 features_traintarget_trainfeatures_testtarget_test 已經在你的工作空間中可用。

本練習屬於課程

HR 分析:用 Python 預測員工流失

檢視課程

練習說明

  • 初始化 Decision Tree 分類器,並透過限制最大深度為 5 來修剪樹,同時平衡類別權重。
  • 擬合這個新模型。
  • 列印該模型在測試集上的預測準確率 score(百分比)。

動手互動練習

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

# Initialize the DecisionTreeClassifier 
model_depth_5_b = DecisionTreeClassifier(____=5,class_weight="____",random_state=42)

# Fit the model
model_depth_5_b.____(features_train,target_train)

# Print the accuracy of the prediction (in percentage points) for the test set
print(model_depth_5_b.____(features_test,____)*100)
編輯並執行程式碼