Python의 Scikit-Learn 결정 트리에서 결정 규칙을 추출하는 방법은 무엇입니까?

Susan Sarandon
풀어 주다: 2024-10-26 12:18:03
원래의
850명이 탐색했습니다.

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

Scikit-Learn 결정 트리에서 결정 규칙 추출

훈련된 결정 트리에서 기본 결정 규칙을 추출하면 결정에 대한 귀중한 통찰력을 얻을 수 있습니다. -만드는 과정. Python을 사용하여 텍스트 목록 형식으로 수행하는 방법은 다음과 같습니다.

Python 함수:

<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>
로그인 후 복사

사용 예:

<code class="python">tree_model = DecisionTreeClassifier().fit(X, y)
tree_to_code(tree_model, feature_names)</code>
로그인 후 복사

이 함수는 트리 구조를 반복적으로 순회하면서 각 분기에 대한 결정 규칙을 인쇄합니다. 리프 노드와 리프가 아닌 노드를 모두 처리하고 트리의 의사 결정 프로세스를 캡슐화하는 유효한 Python 함수를 생성합니다.

위 내용은 Python의 Scikit-Learn 결정 트리에서 결정 규칙을 추출하는 방법은 무엇입니까?의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
저자별 최신 기사
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!