Home  >  Article  >  Backend Development  >  How to Extract Decision Rules from Scikit-Learn Decision Trees in Python?

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

Susan Sarandon
Susan SarandonOriginal
2024-10-26 12:18:03908browse

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>

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!

Statement:
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