【问题标题】:Random Forest classifier severly overfits随机森林分类器严重过拟合
【发布时间】:2018-06-07 04:39:18
【问题描述】:

我有 4 个变量作为随机森林的输入。即。 ['superType1','superType2','superType3','superTypeProbability']。其中前 3 列是 Wikidata 项目 ID,最后一列是概率列。我正在使用 GridSearchCV 进行最佳参数选择。然而,尽管使用了所有可能的选项,但这个模型严重过度拟合。特征“superTypeProbability”在这里过拟合。但是,我想使用此功能,因为在我的情况下,这是唯一可以提高 RF 性能的参数。

roc_auc_score_train:0.994399847095
roc_auc_score_validation:0.402392359246

仅将“superTypeProbability”特征与逻辑回归结合使用,ROC 如下:

roc_auc_score for only superTypeProbability feature:0.762852724493
roc_auc_score for only superTypeProbability feature:0.691760825723

仅使用前 3 个特征,RF 给出的 ROC 为:

roc_auc_score_train:0.974928760078
roc_auc_score_validation:0.790185294454

我的射频代码如下:

def train_model(self):
        logger.info("Using random forest classifier......")
        train = self.feature_preprocessing(self.train)
        X_train = pd.DataFrame(data=train, columns=['superType1', 'superType2', 'superType3'])
        logger.info("Using features: %s", X_train.columns)
        y_train = train['ROLLBACK_REVERTED']

        rfc = RandomForestClassifier(n_jobs=-1, max_features=None, n_estimators=1000, oob_score=True,
                                     random_state=50, min_samples_leaf=1, max_depth=9)

        param_grid = {
            'n_estimators': [500, 600, 700, 800],
            'max_depth': [8, 9, 10, 11],
            'min_samples_leaf': [1],
        }

        search = sklearn.grid_search.GridSearchCV(rfc, param_grid, n_jobs=-1, verbose=0, scoring='roc_auc', cv=3)
        search.fit(X_train, y_train)

        logger.info("All Scores: %s", search.grid_scores_)
        logger.info("Best Score: %s", search.best_score_)
        logger.info("Best Params: %s", search.best_params_)

        predictedProbVal = search.predict_proba(X_train)
        roc_auc_score_train = metrics.roc_auc_score(y_train, predictedProbVal[:, 1])
        logger.info("roc_auc_score_train:%s", roc_auc_score_train)

        validationProb = self.predict_probabilities(search)
        return validationProb

    def predict_probabilities(self, rfModel):
        validation = self.feature_preprocessing(self.validation)
        X_val = pd.DataFrame(data=validation, columns=['superType1', 'superType2', 'superType3', 'superTypeProbability'])
        y_val = validation['ROLLBACK_REVERTED']

        # Predict the result for test data
        predictedProbVal = rfModel.predict_proba(X_val)
        validation['vandalismScore'] = pd.DataFrame(predictedProbVal[:, 1])
        roc_auc_score_val = metrics.roc_auc_score(y_val, predictedProbVal[:, 1])
        logger.info("roc_auc_score_validation:%s", roc_auc_score_val)
        return validation

【问题讨论】:

  • 您知道您正在使用一千个决策树来实现一些功能,是吗?你如何期望它不会过拟合?
  • 我从 50 开始尝试,它在 1000 上运行良好。我刚刚尝试了 50 和 100,以下是结果 roc_auc_score_train:0.99316618015 roc_auc_score_validation:0.386707500727
  • 我对您的数据了解不多,所以我大胆假设前三个特征是分类的,每个特征的类别都不多。然后我倾向于怀疑你是否真的需要 max_depth 这么多。此外,您可以尝试增加 min_samples_leaf 以便需要更多样本来启用每个拆分,从而减少过度拟合第四个特征的微小范围的机会。
  • 是的,定义一个辅助数据集 (20%),通常在其中网格搜索 RF 参数并尝试最小化测试 (20%) 和训练 (60%) 准确度之间的差异,为 3 /4 特征 50、深度 9 的 100 在我看来也太多了,平均 50 棵树,每棵树有 9 个 if 语句,在我看来它很容易过拟合,你能尝试从 2 到 20 棵树并报告它的进展吗?还有,你有没有试过其他型号?
  • 感谢您的建议 :)

标签: python random-forest


【解决方案1】:

问题出在superTypeProbability。我通过对superTypeProbability 功能进行一些更改来解决它。现在 ROC=0.82。在计算 superTypeProbability 之前,我正在计算 typeProbability 特征。将typeProbability 与随机森林一起使用时,ROC 为=0.74。我想改进这个结果。此功能有几个 NaN 值,例如 500 是 NaN,共 1000 个。为了减少这个数字,我派生了新功能 superTypeProbability。如果typeProbabilitysuperTypeProbability 都存在,则将更高的值分配给superTypeProbability。有了这个,superTypeProbability 的 NaN 值少于 1000 个中的 300 个。现在,为了填充这个 NaN 值,我替换了 average superTypeProbability 值,它 smaller平均 typeProbability 值。这导致了这个问题。所以我现在正在使用 平均类型概率来填充NaN

features['superTypeProbability'] = features['superTypeProbability'].fillna(features['typeProbability'][features.typeProbability!='None'].mean())

【讨论】:

  • 有什么变化?您能否分享一下经验并给我们一些导致问题的示例?
  • 我已在评论中添加了详细信息。
猜你喜欢
  • 2019-04-01
  • 2016-03-01
  • 2015-11-21
  • 2018-02-18
  • 2020-03-31
  • 2020-06-24
  • 2018-05-20
  • 2017-08-11
  • 2018-03-05
相关资源
最近更新 更多