使用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时进行这样的检查。(这是因为他们在网格搜索等过程中克隆估计器的方式。)因此,在你调用fit之前,你的明确输入不太可能发生任何变化。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注