当前位置:   article > 正文

python 随机森林可视化_用python做随机森林需要安装那些库

用python做随机森林需要安装那些库

随机森林进行可视化
安装一些需要的库:

pip install graphviz
  • 1
pip install pydotplus
  • 1

在Jupyter notebook 中进行随机森林可视化:

from sklearn import datasets
from sklearn.ensemble import RandomForestClassifier
from IPython.core.display import HTML, display
from sklearn import tree
import pydotplus


# 使用自带的iris数据
iris = datasets.load_iris()
X = iris.data
y = iris.target

# 训练模型,限制树的最大深度4
clf = RandomForestClassifier(max_depth=4)
#拟合模型
clf.fit(X, y)

estimators = clf.estimators_
for m in estimators:
    dot_data = tree.export_graphviz(m, out_file=None,
                         feature_names=iris.feature_names,
                         class_names=iris.target_names,
                         filled=True, rounded=True,
                         special_characters=True)
    graph = pydotplus.graph_from_dot_data(dot_data)
    # 使用ipython的终端jupyter notebook显示。
    svg = graph.create_svg()
    if hasattr(svg, "decode"):
         svg = svg.decode("utf-8")
     html = HTML(svg)
     display(html)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

结果图就不放了

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/blog/article/detail/97675
推荐阅读
相关标签
  

闽ICP备14008679号