Чтобы получить путь, выбранный для конкретной выборки в дереве решений, вы можете использовать decision_path
.Он возвращает разреженную матрицу с путями принятия решений для предоставленных выборок.
Затем эти пути принятия решений можно использовать для окрашивания / маркировки дерева, сгенерированного с помощью pydot
.Это требует перезаписи цвета и метки (что приводит к небольшому количеству уродливого кода).
Примечания
decision_path
могут брать образцы из обученияустановить или новые значения - вы можете сойти с ума с цветами и изменить цвет в зависимости от количества образцов или любой другой визуализации может потребоваться
Пример
В приведенном ниже примере посещенный узел окрашен в зеленый цвет, все остальные узлы - в белый.

import pydotplus
from sklearn.datasets import load_iris
from sklearn import tree
clf = tree.DecisionTreeClassifier(random_state=42)
iris = load_iris()
clf = clf.fit(iris.data, iris.target)
dot_data = tree.export_graphviz(clf, 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)
# empty all nodes, i.e.set color to white and number of samples to zero
for node in graph.get_node_list():
if node.get_attributes().get('label') is None:
continue
if 'samples = ' in node.get_attributes()['label']:
labels = node.get_attributes()['label'].split('<br/>')
for i, label in enumerate(labels):
if label.startswith('samples = '):
labels[i] = 'samples = 0'
node.set('label', '<br/>'.join(labels))
node.set_fillcolor('white')
samples = iris.data[129:130]
decision_paths = clf.decision_path(samples)
for decision_path in decision_paths:
for n, node_value in enumerate(decision_path.toarray()[0]):
if node_value == 0:
continue
node = graph.get_node(str(n))[0]
node.set_fillcolor('green')
labels = node.get_attributes()['label'].split('<br/>')
for i, label in enumerate(labels):
if label.startswith('samples = '):
labels[i] = 'samples = {}'.format(int(label.split('=')[1]) + 1)
node.set('label', '<br/>'.join(labels))
filename = 'tree.png'
graph.write_png(filename)