我可以从决策树中的受过训练的树中提取基础决策规则(或"决策路径")作为文本列表吗?
就像是:
if A>0.4 then if B<0.2 then if C>0.8 then class='X'
谢谢你的帮助.
python machine-learning decision-tree random-forest scikit-learn
我正在使用构建决策树
clf = tree.DecisionTreeClassifier()
clf = clf.fit(X_train, Y_train)
Run Code Online (Sandbox Code Playgroud)
一切正常.但是,我如何探索决策树?
例如,如何找到X_train中的哪些条目出现在特定的叶子中?
扩展先前的问题: 更改使用导出 graphviz 创建的决策树图的颜色
我将如何根据主导类(鸢尾花的种类)而不是二元区分为树的节点着色?这应该需要 iris.target_names(描述类的字符串)和 iris.target(类)的组合。
import pydotplus
from sklearn.datasets import load_iris
from sklearn import tree
import collections
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)
nodes = graph.get_node_list()
edges = graph.get_edge_list()
colors = ('brown', 'forestgreen')
edges = collections.defaultdict(list)
for edge in graph.get_edge_list():
edges[edge.get_source()].append(int(edge.get_destination()))
for edge in edges:
edges[edge].sort()
for i in range(2):
dest = graph.get_node(str(edges[edge][i]))[0]
dest.set_fillcolor(colors[i])
graph.write_png('tree.png')
Run Code Online (Sandbox Code Playgroud) decision-tree graph-visualization python-3.x pydot scikit-learn