- Machine Learning with Swift
- Alexander Sosnovshchenko
- 281字
- 2021-06-24 18:54:56
Tree visualization
Let us take a look at the code to visualize a tree as follows:
In []: labels = df.label.astype('category').cat.categories labels = list(labels) labels Out[]: [u'platyhog', u'rabbosaurus']
Define a variable to store all the names for the features:
In []: feature_names = map(lambda x: x.encode('utf-8'), features.columns.get_values()) feature_names Out[]: ['length', 'fluffy', 'color_light black', 'color_pink gold', 'color_purple polka-dot', 'color_space gray']
Then, create the graph object using the export_graphviz function:
In []: import pydotplus dot_data = tree.export_graphviz(tree_model, out_file=None, feature_names=feature_names, class_names=labels, filled=True, rounded=True, special_characters=True) dot_data Out[]: u'digraph Tree {nnode [shape=box, style="filled, rounded", color="black", fontname=helvetica] ;nedge [fontname=helvetica] ;n0 [label=<length ≤ 26.6917<br/>entropy = 0.9971<br/>samples = 700<br/>value = [372, ... In []: graph = pydotplus.graph_from_dot_data(dot_data.encode('utf-8')) graph.write_png('tree1.png') Out[]: True
Put a markdown to the next cell to see the newly-created file as follows:


The preceding diagram shows what our decision tree looks like. During the training, it grows upside-down. Data (features) travels through it from its root (top) to the leaves (bottom). To predict the label for a sample from our dataset using this classifier, we should start from the root, and move until we reach the leaf. In each node, one feature is compared to some value; for example, in the root node, the tree checks if the length is < 26.0261. If the condition is met, we move along the left branch; if not, along the right.
Let's look closer at a part of the tree. In addition to the condition in each node, we have some useful information:
- Entropy value
- Number of samples in the training set which supports this node
- How many samples support each outcome
- The most likely outcome at this stage
- 筆記本電腦使用、維護(hù)與故障排除實(shí)戰(zhàn)
- 深入理解Spring Cloud與實(shí)戰(zhàn)
- SDL Game Development
- INSTANT Wijmo Widgets How-to
- 單片機(jī)開(kāi)發(fā)與典型工程項(xiàng)目實(shí)例詳解
- 單片機(jī)原理及應(yīng)用:基于C51+Proteus仿真
- 筆記本電腦芯片級(jí)維修從入門到精通(圖解版)
- 單片微機(jī)原理及應(yīng)用
- 單片機(jī)項(xiàng)目設(shè)計(jì)教程
- FPGA實(shí)戰(zhàn)訓(xùn)練精粹
- Learning Less.js
- 分布式存儲(chǔ)系統(tǒng):核心技術(shù)、系統(tǒng)實(shí)現(xiàn)與Go項(xiàng)目實(shí)戰(zhàn)
- ARM接口編程
- FPGA進(jìn)階開(kāi)發(fā)與實(shí)踐
- 超炫的35個(gè)Arduino制作項(xiàng)目