pybaobabdt) DT Tree Visualization 해보기

2021. 12. 18. 14:26분석 Python/Visualization

728x90

 

목차

    패키지 설치

    sudo apt-get install graphviz graphviz-dev
    pip install pygraphviz
    pip install pybaobabdt

    라이브러리 로드

    import pybaobabdt
    import pandas as pd
    from sklearn.tree import DecisionTreeClassifier
    from sklearn.ensemble import RandomForestClassifier
    import matplotlib.pyplot as plt
    import matplotlib
    from sklearn import datasets

    pybaobabdt 패키지는 시각화에 대한 새로운 관점을 제공합니다.

    주요 아이디어는 의미 있는 시각화를 통해 사용자가 트리를 이해하고 해석할 수 있도록 돕는 것입니다.

    Decision Tree

    tree 시각화

    X , y = datasets.load_wine(return_X_y = True , as_frame =True)
    clf = DecisionTreeClassifier(max_depth=5).fit(X,y)
    features = list(X)
    ax = pybaobabdt.drawTree(clf, size=10, dpi=72, features=features)
    ax.get_figure().safeaturesvefig('tree.png', format='png', dpi=300, transparent=True)

    colors = ["gray", "gray", "purple", "gray"]
    colorMap = matplotlib.colors.ListedColormap(colors)
    
    ax = pybaobabdt.drawTree(clf, size=10, dpi=72, features=features, colormap=colorMap)
    ax.get_figure().safeaturesvefig('tree.png', format='png', dpi=300, transparent=True)

    RandomForest

    randomforest의 각각의 tree별로 시각화

    clf = RandomForestClassifier(n_estimators=20, n_jobs=-1, random_state=0)
    clf.fit(X, y)
    size = (15,15)
    plt.rcParams['figure.figsize'] = size
    fig = plt.figure(figsize=size, dpi=300)
    
    for idx, tree in enumerate(clf.estimators_):
        ax1 = fig.add_subplot(5, 4, idx+1)
        pybaobabdt.drawTree(tree, model=clf, size=15, dpi=300, features=features, ax=ax1)
        
    fig.savefig('random-forest.png', format='png', dpi=1200, transparent=True)

     

     

     

     

     

     

     

     

    https://towardsdatascience.com/visualizing-decision-trees-with-pybaobabdt-f8eb5b3d0d17 

     

    Visualizing Decision Trees with Pybaobabdt

    An open-source package for decision tree visualization and model interpretation

    towardsdatascience.com

    https://pypi.org/project/pybaobabdt/

     

    pybaobabdt

    Decision tree visualization

    pypi.org

    https://ieeexplore.ieee.org/document/6102453

     

    BaobabView: Interactive construction and analysis of decision trees

    We present a system for the interactive construction and analysis of decision trees that enables domain experts to bring in domain specific knowledge. We identify different user tasks and corresponding requirements, and develop a system incorporating a tig

    ieeexplore.ieee.org

     

    728x90