How to extract sklearn decision tree rules to pandas boolean conditions?

前端 未结 3 1554
栀梦
栀梦 2020-12-28 17:16

There are so many posts like this about how to extract sklearn decision tree rules but I could not find any about using pandas.

Take this data and model for example,

3条回答
  •  死守一世寂寞
    2020-12-28 17:34

    Now you can use export_text.

    from sklearn.tree import export_text
    
    r = export_text(loan_tree, feature_names=(list(X_train.columns)))
    print(r)
    

    A complete example from sklearn

    from sklearn.datasets import load_iris
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.tree import export_text
    iris = load_iris()
    X = iris['data']
    y = iris['target']
    decision_tree = DecisionTreeClassifier(random_state=0, max_depth=2)
    decision_tree = decision_tree.fit(X, y)
    r = export_text(decision_tree, feature_names=iris['feature_names'])
    print(r)
    

提交回复
热议问题