scikit-learn
decision tree
machine learning
decision rules
Python

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.

Practice ML system design

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:

python
1from sklearn.datasets import load_iris
2from sklearn.tree import DecisionTreeClassifier
3
4# Load the iris dataset
5iris = load_iris()
6X, y = iris.data, iris.target
7
8# Initialize the classifier
9clf = DecisionTreeClassifier(max_depth=3)
10
11# Fit the model
12clf.fit(X, y)

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:

python
1from sklearn.tree import export_text
2
3# Export text representation
4rules = export_text(clf, feature_names=iris['feature_names'])
5print(rules)

Method 2: Traversing the Tree

This involves programatically traversing the tree data structure to extract rules:

python
1import numpy as np
2
3def print_decision_rules(tree, feature_names):
4    left = tree.tree_.children_left
5    right = tree.tree_.children_right
6    threshold = tree.tree_.threshold
7    features = [feature_names[i] if i != _tree.TREE_UNDEFINED else "undefined!" for i in tree.tree_.feature]
8
9    def recurse(node, depth):
10        indent = "  " * depth
11        if left[node] == _tree.TREE_LEAF:
12            print(f"{indent}Return {np.argmax(tree.tree_.value[node])}")
13        else:
14            print(f"{indent}if {features[node]} <= {threshold[node]:.2f}:")
15            recurse(left[node], depth + 1)
16            print(f"{indent}else:  # if {features[node]} > {threshold[node]:.2f}")
17            recurse(right[node], depth + 1)
18
19    recurse(0, 1)
20
21print_decision_rules(clf, iris['feature_names'])

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:

MethodDescriptionExample Code
Text ExportingUtilize export_text to get a readable version of decision rules. Suitable for smaller models.export_text(clf, feature_names=iris['feature_names'])
Tree TraversalManually 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
Course
Intermediate
27 lessons
15 hours
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 course
Track 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.

Practice ML system design

All Rights Reserved.