【问题标题】:get the value of decision trees in random forests获取随机森林中决策树的值
【发布时间】:2021-03-24 09:55:19
【问题描述】:

是否有可能知道我得到预测值的树的数量? 我使用随机森林算法做了一个对象检测项目

from sklearn.ensemble import RandomForestClassifier
RF_model = RandomForestClassifier(n_estimators = 50, random_state = 42)

# Train the model on training data
RF_model.fit(X_for_RF, y_train)

这里我有 50 棵树,我需要知道有多少棵树可以得到这个预测值。

假设随机森林提取给我这是苹果,那么我需要得到它决定它的树的数量苹果像“有 30 棵树告诉我这是苹果,其他 20 棵决定它是香蕉 ”。

【问题讨论】:

    标签: python scikit-learn random-forest


    【解决方案1】:

    是的,这可以使用 estimators_ 属性:

    RF_model = RandomForestClassifier(n_estimators = 50, random_state = 42)
    RF_model.fit(X_for_RF, y_train)
    
    trees = RF_model.estimators_
    # Get all 50 tree predictions for the first instance
    preds_for_0 = [tree.predict(X_for_RF[0].reshape(1, -1))[0] for tree in trees]
    

    这里的trees 是来自sklearn.treen_estimators(这里是50DecisionTreeClassifier() 对象的列表。要从每棵树中获取类标签,您可以简单地使用predict() 方法。

    列表preds_for_0(长度50)存储每个组成树为X_for_RF[0] 预测的标签。探索此列表将很容易为您提供多数标签以及哪个树为实例提供了哪个标签。

    【讨论】:

      猜你喜欢
      • 2017-03-15
      • 2017-12-24
      • 2015-04-26
      • 2017-05-03
      • 2021-12-25
      • 2021-02-20
      • 2017-12-04
      • 2021-11-05
      • 2020-03-17
      相关资源
      最近更新 更多