How to extract the decision rules from scikit-learn decision-tree?
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.
Introduction
The decision tree is a popular machine learning algorithm used for both classification and regression tasks. It is valued for its simplicity and interpretability. The decision path taken by any input sample can be traced through the decision rules laid out by the tree. This article explains how to extract decision rules from a decision tree model implemented using the scikit-learn library in Python.
Understanding Decision Trees
A decision tree is a flowchart-like structure where internal nodes represent features (attributes), branches represent decision rules, and each leaf node represents an outcome. Here is a brief outline of key components:
- Nodes:
- Root Node: Represents the entire dataset, which is then divided based on attributes.
- Decision Nodes: Sub-nodes that further split based on certain conditions.
- Leaf Nodes (Terminal nodes): Representation of the outcome, the classification, or the final decision.
- Edges: The outcomes of the split decision. They basically represent the rule or condition taken to split from one node to another.
- Splits: Decision trees apply a splitting rule to divvy up the dataset into branches.
Implementing a Decision Tree in Scikit-Learn
To better understand decision trees, let's first implement a simple decision tree:
Extracting Decision Rules
Scikit-learn provides an easy way to visualize a decision tree using textual output, which can be utilized to extract decision rules. However, this only gives a direct view without dynamic manipulation. For real-world scenarios, extracting the rules in structured form helps to interpret or utilize them further.
To extract the rules, the tree structure needs to be translated to human-readable conditions:
Method 1: Export Text Representation
Using the export_text() function, you can print textual representation of the decision rules:
Method 2: Traversing the Tree
This involves programatically traversing the tree data structure to extract rules:
Important Considerations
- Tree Depth: A deeper tree might represent more complex decision surfaces but can easily overfit. Limiting the depth controls complexity.
- Feature Importance: Understand which features are critical in the decision-making process. Scikit-learn provides a
feature_importances_attribute for introspection. - Pruning: This technique addresses overfitting by removing branches that have little importance.
Summary
Below is a summary table outlining the key methods to extract decision rules from a scikit-learn decision tree:
| Method | Description | Example Code |
| Text Exporting | Utilize export_text to get a readable version of decision rules. Suitable for smaller models. | export_text(clf, feature_names=iris['feature_names']) |
| Tree Traversal | Manually traverse the tree to extract decision rules. Useful for customized outputs. Can handle large models by programatically obtaining rules. | Implement a recursive function to traverse nodes.
print_decision_rules(clf, iris['feature_names']) |
Conclusion
Extracting decision rules from scikit-learn's decision tree models allows developers to interpret and understand model decisions easily. Both textual outputs and algorithmic traversal are viable approaches to access these rules, depending on the complexity and the need for customization.
By applying these techniques, you can gain better insights into your model's predictions and refine your strategies based on clear logic paths inherent in the decision tree structure.
Related reading
- How to extract the decision rules from scikit-learn decision-tree?
- How to extract unsupervised clusters from a Dirichlet Process in PyMC3?
- How to feed back `RNN` output to input in tensorflow
- How to feed input with changing size in Tensorflow
- How to fastest count the number of set bits in php?
- how to figure out all messages with a specific groupId has been read from the queue in SQS?
- How to filter a dictionary according to an arbitrary condition function?
- How to filter Pandas dataframe using 'in' and 'not in' like in SQL

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
ML System Design practice on Codemia
Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.