How to Extract Decision Rules from Scikit-Learn Decision Trees in Python?

Susan Sarandon
Release: 2024-10-26 12:18:03
Original
850 people have browsed it

How to Extract Decision Rules from Scikit-Learn Decision Trees in Python?

Extracting 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>
Copy after login

Example Usage:

<code class="python">tree_model = DecisionTreeClassifier().fit(X, y)
tree_to_code(tree_model, feature_names)</code>
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!