Python 如何在sklearn中手动更改决策树的特征值?

Python 如何在sklearn中手动更改决策树的特征值?,python,machine-learning,scikit-learn,Python,Machine Learning,Scikit Learn,在scikit学习中,如果我从 RandomForestClassifer().estimators_ 有没有办法手动更改某些功能?我可以使用 for estimator in rfc.estimators_: for feature in estimator.tree_.feature: 但在这种情况下,我想手动更改该功能。我该怎么做呢?如果我正确理解了您的问题,那么您想更改随机林中决策树的参数吗?我不太清楚你为什么要这么做 我将把解决方案分成两部分 首先,我们将尝试更改决策树的参

在scikit学习中,如果我从

RandomForestClassifer().estimators_
有没有办法手动更改某些功能?我可以使用

for estimator in rfc.estimators_:
    for feature in estimator.tree_.feature:

但在这种情况下,我想手动更改该功能。我该怎么做呢?

如果我正确理解了您的问题,那么您想更改随机林中决策树的参数吗?我不太清楚你为什么要这么做

我将把解决方案分成两部分

首先,我们将尝试更改决策树的参数

from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import load_iris

clf = DecisionTreeClassifier(random_state=0)

iris = load_iris()

clf.fit(iris.data,iris.target)
#DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=None,
#       max_features=None, max_leaf_nodes=None,
#        min_impurity_decrease=0.0, min_impurity_split=None,
#        min_samples_leaf=1, min_samples_split=2,
#        min_weight_fraction_leaf=0.0, presort=False, random_state=0,
#        splitter='best')

#Now extract the parameters
parameters_dt = clf.get_params()

#Now change the parameter you want
parameters_dt['max_depth'] = 3

#Now create a new classifier
new_clf = DecisionTreeClassifier(**parameters_dt)
#DecisionTreeClassifier(class_weight=None, criterion='gini', max_depth=2,
#        max_features=None, max_leaf_nodes=None,
#        min_impurity_decrease=0.0, min_impurity_split=None,
#        min_samples_leaf=1, min_samples_split=2,
#        min_weight_fraction_leaf=0.0, presort=False, random_state=0,
#        splitter='best')
现在让我们回到随机森林

from sklearn.ensemble import RandomForestClassifier
from sklearn.tree import DecisionTreeClassifier
from sklearn.datasets import make_classification

X, y = make_classification(n_samples=1000, n_features=4,
                        n_informative=2, n_redundant=0,
                       random_state=0, shuffle=False)

clf = RandomForestClassifier(max_depth=2, random_state=0)

clf.fit(X, y)

clf_list = clf.estimators_
for idx in range(0,len(clf_list)):
    #Get the current Decision Tree in Random Forest
    estimator = clf_list[idx]

    #Get the params
    temp_params = estimator.get_params()

    #Change the params you want
    temp_params['max_depth'] = 3

    #Create a new decision tree
    temp_decision_tree = DecisionTreeClassifier(**temp_params)

    #Remove the old decision tree
    clf.estimators_.pop(idx)

    #Then insert the new decision tree at the current position
    clf.estimators_.insert(idx, temp_decision_tree)

注意:这可能没有您想要的效果。我的意思是它可能不会产生您想要的准确分类器。

这与我想要的非常接近,谢谢。我的目标不是用新参数创建新树,而是简单地更改决策树中的节点。原因是我正在处理的树中的节点是(x,y,z)坐标,其值关联为决策点。我想看看,如果我在某个n-球中移动坐标,决策树会发生多大的变化。总的来说,我希望能够手动编辑已构建决策树的节点。在您的情况下,您实际上无法编辑节点以查看更改。您更改的任何节点参数都将反映在整个树中,因此不会产生任何差异。对于e、 g.不能将某些节点的属性
max\u depth
设置为2,而将其他节点的属性
max\u depth
设置为3。这些参数始终保持不变,但不同的最大深度属性没有意义,不。我想做的是更改节点正在询问的特定“问题”。如果决策树上的一个节点说“如果X>5,那么在树中向左走”我想将X更改为某个值。该决策基于熵阈值或最小阈值,该阈值在整个树中是固定的。那是不可能的。我想说的是,你设置的任何规则或属性在整个树中都是固定的。明白了。非常感谢。那么,是否可以手动构建决策树?即逐节点?我想不会。