在scikit-learn中,如果我从
中取出决策树RandomForestClassifer().estimators_
有没有办法可以手动更改某些功能?我可以使用
迭代它们for estimator in rfc.estimators_:
for feature in estimator.tree_.feature:
但我想在这种情况下手动更改功能。我该怎么做呢?
答案 0 :(得分:0)
如果我已正确理解您的问题,那么您想要更改随机林内决策树的参数吗?我不确定你为什么要那样做。
我将解决方案分为两部分
首先,我们将尝试更改决策树的参数
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)
注意:这可能没有您想要的效果。我的意思是它可能不会产生你想要的确切分类器。