python 决策树图

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python 决策树图相关的知识,希望对你有一定的参考价值。

import sklearn.datasets as datasets
import pandas as pd
iris=datasets.load_iris()
df=pd.DataFrame(iris.data, columns=iris.feature_names)
y=iris.target

from sklearn.tree import DecisionTreeClassifier
dtree=DecisionTreeClassifier()
dtree.fit(df,y)

from sklearn.externals.six import StringIO  
from IPython.display import Image  
from sklearn.tree import export_graphviz
import pydotplus
dot_data = StringIO()
export_graphviz(dtree, out_file=dot_data,  
                filled=True, rounded=True,
                special_characters=True)
graph = pydotplus.graph_from_dot_data(dot_data.getvalue())  
Image(graph.create_png()) 
## plot decision tree analysis of any features vs target
def analysis_tree(clf:'estimator',X:'array',y:'array',lx:list,ly:list)->'graph':
    """
    Plot decision tree analysis of any features vs target.
    clf -- Decision Tree estimator without being fitted (regressor or classificator).
    X -- numpy array 2D of features.
    y -- numpy array 1D of target.
    lx -- list of feature names.
    ly -- list of target name.
    return -- graph (it is automatically ploted in Jupyter Notebook)
    """
    # fit the estimator
    clf = clf.fit(X, y)
    # graph
    import graphviz 
    dot_data = tree.export_graphviz(clf, out_file=None, 
                             feature_names=lcol_weather,  
                             class_names=['y'],  
                             filled=True, rounded=True,  
                             special_characters=True)  
    # return
    return graphviz.Source(dot_data)  

# define estimator
from sklearn import tree
clf = tree.DecisionTreeRegressor(max_depth=3)
# plot tree
analysis_tree(clf,X,y,lx,ly)

以上是关于python 决策树图的主要内容,如果未能解决你的问题,请参考以下文章

验证决策树图

理解变量选择的大型决策树图?

如何在 r studio 中缩小决策树图?

在 jupyter notebook 中显示 scikit 决策树图

更改使用导出 graphviz 创建的决策树图的颜色

如何使用 R 在随机森林中生成决策树图和变量重要性图?