访问随机森林模型中单个树的底层(tree_)对象(Python,scikit-learn)

我需要将一个随机森林模型转换为基于规则的模型或基于(if-then)的模型。我已经创建了我的模型,并且调优得很好。我面临的问题是,我无法“访问”到(base_estimator)或底层的(tree_对象),这使得我无法创建一个函数来从森林中的树中提取规则。如果您能帮助我解决这个问题,我将不胜感激。我创建模型时使用的是:

    estimator = RandomForestRegressor(oob_score=True, n_estimators=10,max_features='auto')

我尝试使用estimator.estimators_属性来访问单个树,然后使用例如estimator.estimators_[0].tree_来获取用于构建森林的决策树(DecisionTreeRegressor对象)。不幸的是,这种方法不起作用。

如果可能的话,我想要的是这样的:

   estimator = RandomForestRegressor(oob_score=True, n_estimators=10,max_features='auto')   estimator.fit(tarning_data,traning_target)   tree1 = estimator.estimators_[0]   leftChild = tree1.tree_.children_left   rightChild = tree1.tree_.children_right

回答:

要访问随机森林模型中DecisionTreeRegressor对象的底层结构,您需要按照以下步骤进行:

estimator = RandomForestRegressor(oob_score=True,n_estimators=10,max_features='auto')estimator.fit(tarning_data,traning_target)tree1 = estimator.estimators_[0]leftChilds = tree1.tree_.children_left # 左子节点数组rightChilds = tree1.tree_.children_right # 右子节点数组

即基本上就是问题中已经描述的内容。

Related Posts

L1-L2正则化的不同系数

我想对网络的权重同时应用L1和L2正则化。然而,我找不…

使用scikit-learn的无监督方法将列表分类成不同组别,有没有办法?

我有一系列实例,每个实例都有一份列表,代表它所遵循的不…

f1_score metric in lightgbm

我想使用自定义指标f1_score来训练一个lgb模型…

通过相关系数矩阵进行特征选择

我在测试不同的算法时,如逻辑回归、高斯朴素贝叶斯、随机…

可以将机器学习库用于流式输入和输出吗?

已关闭。此问题需要更加聚焦。目前不接受回答。 想要改进…

在TensorFlow中,queue.dequeue_up_to()方法的用途是什么?

我对这个方法感到非常困惑,特别是当我发现这个令人费解的…

发表回复

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