• 随机森林可视化


    今天看到别人的文章,说到了随机森林可视化,于是尝试了下。

    window安装

    windows版本安装:
    1.在下面去下载window的exe安装包,安装graphviz。

    http://www.graphviz.org/download/
    在路径选项,点击add path to computer,然后后面全部点确定就行。安装好以后,打开powershell,输入dot -version,就可以看到安装成功了。
    2.安装pygraphviz和pybaobabdt

    # 安装pygraphviz
    conda install --channel conda-forge pygraphviz
    # 安装pybaobabdt
    pip install pybaobabdt
    pybaobabdt.drawTree(decisionTreeClassifier, features=[], model=[], colormap='viridis', size=15, dpi=300, ratio=1, classes=[], maxdepth=-1, ax=-1)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    linux安装

    sudo apt-get install graphviz graphviz-dev
    vim ~/.bashrc
    
    conda install --channel conda-forge pygraphviz
    pip install pybaobabdt
    
    • 1
    • 2
    • 3
    • 4
    • 5

    主要是使用Pybaobabdt来绘制决策树

    展示

    下面这个是官网作者的图
    在这里插入图片描述
    这是我的数据的图:
    在这里插入图片描述
    不得不说,,,这个图也太丑了点,虽然用的是默认的决策树超参数。。。

    DecisionTree( criterion=‘gini’, splitter=‘best’, max_depth=None, min_samples_split=2, min_samples_leaf=1, min_weight_fraction_leaf=0.0, max_features=None, random_state=None, max_leaf_nodes=None, min_impurity_decrease=0.0, class_weight=None, ccp_alpha=0.0)

    max_depth = 10max_depth = 3
    在这里插入图片描述
    在这里插入图片描述

    我这个数据由于特征值太小,所以默认的决策树生长到5层就不生长了。所以max_depth参数只在1-5之间才有用。这加深了我对树类模型的调参认识。

    from matplotlib.colors import ListedColormap
    colors = ["gray", "purple"]
    colormap = ListedColormap(colors)
    pybaobabdt.drawTree(clf, size=10, dpi=72, features=features, colormap=colormap)
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    Usage

    pybaobabdt.drawTree(decisionTreeClassifier, features=[], model=[], colormap='viridis', size=15, dpi=300, ratio=1, classes=[], maxdepth=-1, ax=-1)
    
    • 1

    这个ratio用来控制字符显示大小,maxdepth用来控制树的显示,可以选择只显示顶端5行。

    随机森林用法

    随机森林可以选择只显示其中一棵树

    clf = RandomForestClassifier(n_estimators=20).fit(data, label)
    pybaobabdt.drawTree(clf.estimators_[0], size=10, dpi=300, features=features)
    
    • 1
    • 2
  • 相关阅读:
    七、Request&Response
    图像运算和图像增强十
    【测试开发】几种常见的自动化测试框架
    100、相同的树
    react实现keepAlive 可手动清除缓存的
    flink运行时组件和调度原理
    【故障诊断】用于轴承故障诊断的候选故障频率优化克改进包络频谱研究(Matlab代码实现)
    C++语言之组合、聚合类间关系
    麦芽糖-聚乙二醇-阿霉素maltose-Doxorubicin
    05 程序流程控制
  • 原文地址:https://blog.csdn.net/clancy_wu/article/details/127717730