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) print(clf.tree_.value)
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.