開始使用免費開始

Voting Classifier

現在來把三個機器學習模型結合成一個,以改進我們先前的 Random Forest 詐欺偵測模型。你會把常用的 Random Forest、上一個練習的 Logistic Regression,以及一個簡單的 Decision Tree 合併。你可以使用捷徑 get_model_results() 來立即查看這個集成模型的結果。

本練習屬於課程

Fraud Detection in Python

檢視課程

練習說明

  • 匯入 Voting Classifier 套件。
  • 定義三個模型:使用先前的 Logistic Regression、前面練習的 Random Forest,以及一個類別權重平衡(balanced class weights)的 Decision Tree。
  • 透過輸入三個分類器及其標籤,定義集成模型。

動手互動練習

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

# Import the package
from sklearn.ensemble import ____

# Define the three classifiers to use in the ensemble
clf1 = LogisticRegression(class_weight={0:1, 1:15}, random_state=5)
clf2 = ____(class_weight={0:1, 1:12}, criterion='gini', max_depth=8, max_features='log2',
            min_samples_leaf=10, n_estimators=30, n_jobs=-1, random_state=5)
clf3 = DecisionTreeClassifier(random_state=5, class_weight="____")

# Combine the classifiers in the ensemble model
ensemble_model = ____(estimators=[('lr', ____), ('rf', ____), ('dt', ____)], voting='hard')

# Get the results 
get_model_results(X_train, y_train, X_test, y_test, ensemble_model)
編輯並執行程式碼