开始使用免费开始使用

Voting 分类器

现在我们来把三个机器学习模型组合成一个,以改进之前的随机森林欺诈检测模型。您将把常用的随机森林模型,与上一个练习的逻辑回归模型,再加上一个简单的决策树组合起来。您可以使用便捷函数 get_model_results() 来立即查看该集成模型的结果。

本练习是课程的一部分

Python 中的欺诈检测

查看课程

练习说明

  • 导入 Voting Classifier 包。
  • 定义三个模型:沿用之前的逻辑回归、之前练习中的随机森林,以及一个类别权重平衡的决策树。
  • 通过传入三个分类器及其各自的标签来定义集成模型。

交互式实操练习

通过完成这段示例代码来试试这个练习。

# 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)
编辑并运行代码