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



我需要将Random Fores模型转换为基于规则的模型或基于(if-then)的模型。我现在已经创建了我的模型,并且它经过了很好的调整。我面临的问题是;访问";(base_estimator)或底层(tree_object)可以创建一个函数,该函数可以从林中的树中提取规则。如果你能帮我解决这个问题,我将不胜感激。创建我使用的模型:

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 # array of left children
rightChilds = tree1.tree_.children_right #array of right children

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

最新更新