sklearn随机森林分类器的奇怪行为

时间:2021-05-25 10:40:52

标签: python machine-learning scikit-learn random-forest

我正在尝试使用 sklearn 随机森林分类器(在 python 中),但得到了一些奇怪的结果

我的功能是:

    rf = tree(data_handler.train_dataset, data_handler.train_labels, num_of_estimemtors, tree_depth, 42, tree_max_featrues)        
    # evaluate(rf, data_handler.train_dataset, data_handler.train_labels)
    evaluate(rf, data_handler.test_dataset, data_handler.test_labels)

(“树”和“评估”的实现见下文)

当第二行有注释时,结果很糟糕:

 0.4772727272727273
[[ 0 23]
 [ 0 21]]
              precision    recall  f1-score   support

           0       0.00      0.00      0.00        23
           1       0.48      1.00      0.65        21

    accuracy                           0.48        44
   macro avg       0.24      0.50      0.32        44
weighted avg       0.23      0.48      0.31        44

但是,当取消注释这一行时,结果会发生巨大变化:

0.9846153846153847
[[1235    0]
 [  38 1197]]
              precision    recall  f1-score   support

           0       0.97      1.00      0.98      1235
           1       1.00      0.97      0.98      1235

    accuracy                           0.98      2470
   macro avg       0.99      0.98      0.98      2470
weighted avg       0.99      0.98      0.98      2470

0.5909090909090909
[[ 8 15]
 [ 3 18]]
              precision    recall  f1-score   support

           0       0.73      0.35      0.47        23
           1       0.55      0.86      0.67        21

    accuracy                           0.59        44
   macro avg       0.64      0.60      0.57        44
weighted avg       0.64      0.59      0.56        44

这个函数不会改变 rf(随机森林)。我试图理解这一点半天,但我失败了。这里有什么问题吗?

函数实现:

def evaluate(rf, x, y):
    pred = rf.predict(x)
    print(accuracy_score(y, pred))
    print(confusion_matrix(y, pred))
    print(classification_report(y, pred))
    return accuracy_score(y, pred)
    
def tree(x, y, est, depth, seed=42, max_features="auto"):
    rf = RandomForestClassifier(n_estimators = est, max_depth=depth, random_state=seed, bootstrap=True, max_features=max_features)
    rf.fit(x,y)
    return rf

1 个答案:

答案 0 :(得分:0)

如果我理解正确,当您使用 test_datasettest_labels 评估模型时,您会看到模型的性能指标大幅下降。

<块引用>

这个函数不会改变 rf(随机森林)。我试图理解这一点半天,但我失败了。这里有什么问题吗?

您没有显示传递给 tree 的值,但您的模型可能过度拟合。我建议您使用较低的 rf、较多的 max_depth 或较少的 n_estimators 重新训练 max_features。事实上,您可以运行 Grid Search 来找到超参数的最佳组合。

再说一次,我不知道您当前的超参数是什么,但根据个人经验,我发现 max_depth 约为 5 会导致模型通常泛化良好。

相关问题