


How to Extract Decision Rules from Scikit-Learn Decision Trees in Python?
Oct 26, 2024 pm 12:18 PMExtracting Decision Rules from Scikit-Learn Decision Trees
Extracting the underlying decision rules from a trained decision tree can provide valuable insights into its decision-making process. Here's how to do it in a textual list format using Python.
Python Function:
<code class="python">from sklearn.tree import _tree def tree_to_code(tree, feature_names): tree_ = tree.tree_ feature_name = [ feature_names[i] if i != _tree.TREE_UNDEFINED else "undefined!" for i in tree_.feature ] print("def tree({}):".format(", ".join(feature_names))) def recurse(node, depth): indent = " " * depth if tree_.feature[node] != _tree.TREE_UNDEFINED: name = feature_name[node] threshold = tree_.threshold[node] print("{}if {} <= {}:".format(indent, name, threshold)) recurse(tree_.children_left[node], depth + 1) print("{}else: # if {} > {}".format(indent, name, threshold) + depth) recurse(tree_.children_right[node], depth + 1) else: print("{}return {}".format(indent, tree_.value[node])) recurse(0, 1)</code>
Example Usage:
<code class="python">tree_model = DecisionTreeClassifier().fit(X, y) tree_to_code(tree_model, feature_names)</code>
This function iteratively traverses the tree structure, printing out decision rules for each branch as it encounters them. It handles both leaf nodes and non-leaf nodes and generates a valid Python function that encapsulates the decision-making process of the tree.
The above is the detailed content of How to Extract Decision Rules from Scikit-Learn Decision Trees in Python?. For more information, please follow other related articles on the PHP Chinese website!

Hot Article

Hot tools Tags

Hot Article

Hot Article Tags

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

How Do I Use Beautiful Soup to Parse HTML?

How to Use Python to Find the Zipf Distribution of a Text File

How to Perform Deep Learning with TensorFlow or PyTorch?

Introduction to Parallel and Concurrent Programming in Python

Serialization and Deserialization of Python Objects: Part 1

How to Implement Your Own Data Structure in Python

Mathematical Modules in Python: Statistics
