Voting Classifier
Bây giờ, hãy kết hợp ba mô hình Machine Learning vào một mô hình, để cải thiện mô hình Random Forest phát hiện gian lận trước đó. Bạn sẽ kết hợp Random Forest quen thuộc với Logistic Regression từ bài tập trước, và một Decision Tree đơn giản. Bạn có thể dùng phím tắt get_model_results() để xem ngay kết quả của mô hình ensemble.
Bài tập này là một phần của khóa học
Phát hiện gian lận với Python
Hướng dẫn bài tập
- Import gói Voting Classifier.
- Định nghĩa ba mô hình: dùng Logistic Regression từ trước, Random Forest từ các bài trước và một Decision Tree với trọng số lớp cân bằng.
- Định nghĩa mô hình ensemble bằng cách truyền vào ba bộ phân loại kèm theo nhãn tương ứng của chúng.
Bài tập tương tác thực hành trực tiếp
Hãy thử làm bài tập này bằng cách hoàn thành đoạn mã mẫu này.
# 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)