为什么减少 RandomForestClassifier 中的 n_estimators 会提高准确性?

数据挖掘 Python scikit-学习 随机森林
2022-03-13 06:40:27

我正在学习一门向我介绍 sklearn.ensemble.RandomForestClassifier 的课程。起初它使用n_estimators默认值 10,结果准确度约为 0.28。如果我更改n_estimators为 15,则准确度变为 0.32

这是一些代码:

pl = Pipeline([
        ('union', FeatureUnion(
            transformer_list = [
                ('numeric_features', Pipeline([
                    ('selector', get_numeric_data),
                    ('imputer', Imputer())
                ])),
                ('text_features', Pipeline([
                    ('selector', get_text_data),
                    ('vectorizer', CountVectorizer())
                ]))
             ]
        )),
        ('clf', RandomForestClassifier())
    ])

我认为增加n_estimatorsRandomForestClassifier中树的数量(有人可以解释一下吗?您如何找到获得最高准确度的最小值?

1个回答

如果您在这种情况下谈论测试准确性(即您正在比较未训练的数据的结果) - 添加更多估计器可能会过度拟合您的训练集,因此在您的保留集上表现不佳。如果是这种情况,我建议使用更基本的方法(例如 LogisticRegression)来解决问题 - 因为与集成方法相比,它不太可能过度拟合。

至于寻找最佳参数 - 尝试使用 sklearn 的 RandomizedSearchCV 来微调您的超参数以最大限度地提高性能。