使用 VotingClassifier() 构建随机森林模型的集合

Building an ensemble of Random Forest models with VotingClassifier()

我正在尝试使用 Sklearn 的 VotingClassifier() 构建一些模型的集合,看看它是否比单个模型效果更好。我正在尝试两种不同的方式。

  1. 我正在尝试使用单独的随机森林、梯度提升和 XGBoost 模型来做到这一点。
  2. 我正在尝试使用许多随机森林模型的集合来构建它(对 n_estimators 和 max_depth 使用不同的参数。

第一种情况,我是这样做的

estimator = []
estimator.append(('RF', RandomForestClassifier(bootstrap=True, ccp_alpha=0.0, class_weight=None,
                       criterion='gini', max_depth=8, max_features='auto',
                       max_leaf_nodes=None, max_samples=None,
                       min_impurity_decrease=0.0, min_impurity_split=None,
                       min_samples_leaf=1, min_samples_split=2,
                       min_weight_fraction_leaf=0.0, n_estimators=900,
                       n_jobs=-1, oob_score=True, random_state=66, verbose=0,
                       warm_start=True)))
estimator.append(('GB', GradientBoostingClassifier(ccp_alpha=0.0, criterion='friedman_mse', init=None,
                           learning_rate=0.03, loss='deviance', max_depth=5,
                           max_features=None, max_leaf_nodes=None,
                           min_impurity_decrease=0.0, min_impurity_split=None,
                           min_samples_leaf=1, min_samples_split=2,
                           min_weight_fraction_leaf=0.0, n_estimators=1000,
                           n_iter_no_change=None, presort='deprecated',
                           random_state=66, subsample=1.0, tol=0.0001,
                           validation_fraction=0.1, verbose=0,
                           warm_start=False)))
estimator.append(('XGB', xgb.XGBClassifier(base_score=0.5, booster='gbtree', colsample_bylevel=1,
              colsample_bynode=1, colsample_bytree=1, gamma=0,
              learning_rate=0.1, max_delta_step=0, max_depth=9,
              min_child_weight=1, n_estimators=1000, n_jobs=1,
              nthread=None, objective='binary:logistic', random_state=0,
              reg_alpha=0, reg_lambda=1, scale_pos_weight=1, seed=None,
              silent=None, subsample=1, verbosity=1)))

当我这样做时

ensemble_model_churn = VotingClassifier(estimators = estimator, voting ='soft')

并显示 ensemble_model_churn,我在输出中得到了所有内容。

但是在第二种情况下,我是这样做的

estimator = []
estimator.append(('RF_1',RandomForestClassifier(n_estimators=500,max_depth=5,warm_start=True)))
estimator.append(('RF_2',RandomForestClassifier(n_estimators=500,max_depth=6,warm_start=True)))
estimator.append(('RF_3',RandomForestClassifier(n_estimators=500,max_depth=7,warm_start=True)))
estimator.append(('RF_4',RandomForestClassifier(n_estimators=500,max_depth=8,warm_start=True)))

等等。我有 30 种不同的模型。

但是这一次,当我这样做的时候

ensemble_model_churn = VotingClassifier(estimators = estimator, voting ='soft')

显示出来,我只得到第一个,其他的都没有

print(ensemble_model_churn)
>>>VotingClassifier(estimators=[('RF_1',
                              RandomForestClassifier(bootstrap=True,
                                                     ccp_alpha=0.0,
                                                     class_weight=None,
                                                     criterion='gini',
                                                     max_depth=5,
                                                     max_features='auto',
                                                     max_leaf_nodes=None,
                                                     max_samples=None,
                                                     min_impurity_decrease=0.0,
                                                     min_impurity_split=None,
                                                     min_samples_leaf=1,
                                                     min_samples_split=2,
                                                     min_weight_fraction_leaf=0.0,
                                                     n_estimators=500,
                                                     n_jobs=None,
                                                     oob_score=...
                                                     criterion='gini',
                                                     max_depth=5,
                                                     max_features='auto',
                                                     max_leaf_nodes=None,
                                                     max_samples=None,
                                                     min_impurity_decrease=0.0,
                                                     min_impurity_split=None,
                                                     min_samples_leaf=1,
                                                     min_samples_split=2,
                                                     min_weight_fraction_leaf=0.0,
                                                     n_estimators=500,
                                                     n_jobs=None,
                                                     oob_score=False,
                                                     random_state=None,
                                                     verbose=0,
                                                     warm_start=True))],
                 flatten_transform=True, n_jobs=None, voting='soft',
                 weights=None)

为什么会这样?不可以运行同款合奏吗?

您看到的不止一个估算器,只是有点难以分辨。请注意第一个 oob_score 参数之后的省略号 (...),并且在这些参数之后会重复一些超参数。 Python 只是不想打印这么大的文字墙,并且已经剪掉了中间的大部分内容。您可以检查 len(ensemble_model_churn.estimators) > 1.

另一个注意事项:sklearn 非常反对在模型启动时进行任何验证,而更愿意在适合时进行此类检查。 (这是因为他们在网格搜索等中克隆估算器的方式。)因此,在您调用 fit.

之前,您的显式输入不太可能发生任何变化。