I have trained Random Forest Regressor from sklearn. I am able to return text representation from each Decision Tree rule using tree.export_text (sklearn documentation here). But it shows rules for the model trained using the whole train set, rather than rules that were applied to predict values of given sample.

Of course, I can go through each rule and check if it fits to the data from my sample, but when the decision tree returns 150 rules, it becomes a really hard job.

I would be grateful for any ideas! Thank you :)

Answer: Using a tree attributes and code from sklearn documentation I was able to find rules that were used to predict a given sample.

  • 2
    $\begingroup$ decision_path gives the list of node indices the sample went through, then you can use the tree_ attributes to connect that to features and thresholds, see scikit-learn.org/stable/auto_examples/tree/…. If I get some time I might flesh that out into an answer, but happy if anybody else gets there first. $\endgroup$
    – Ben Reiniger
    Oct 3, 2023 at 14:14
  • $\begingroup$ @BenReiniger thank you. Using the link you posted I found out how the predicted value was created for the given sample. It was exactly what I needed $\endgroup$
    – Paulina
    Oct 4, 2023 at 11:22
  • $\begingroup$ Please add your solution as an answer here. $\endgroup$
    – Ben Reiniger
    Oct 4, 2023 at 12:08


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.