It is possible to read the node values of a DecisionTree as follows:

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

clf = DecisionTreeClassifier(random_state=0)
iris = load_iris()

clf.fit(iris.data, iris.target)


Is there a possibility to manipulate those values after the training process and store the changed values back into the data-structure of the tree?

Storing the decision tree:

from sklearn.externals import joblib

joblib.dump(clf, "clf.pkl")

I am currently experimenting a little bit with random forests. I would like to learn the tree structure independently from the leaf node predictions. So the idea is to first learn a random forest with sklearn, afterwards learn the leaf prediction models separately.

Idea1: Now, it is possible to store the separately trained leaf node values into another array learned_leaf_nodes and just use node_indices = regr.apply(X) (which gives you the node indices of each respective sample), instead of regr.predict(X) (which gives you the final prediction of each sample), to look up the appropriate node index in learned_leaf_nodes. Afterwards, since I use random forests and not single trees, I would have to implement the evaluation of the final prediction.

Idea1 is an option but I prefer to be able to directly change the leaf node values in my pickled forest.

  • $\begingroup$ What are you attempting to accomplish by changing the leaves? Some kind of pruning? $\endgroup$ – jonnor May 24 '18 at 23:44
  • $\begingroup$ I would expect that you can just change these at will, have you tried? For a RandomForestClassifier you have each tree in the attribute _estimators. Each tree has, .children_left, tree.children_right, .feature, .threshold and .value - which are arrays $\endgroup$ – jonnor May 24 '18 at 23:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.