【发布时间】:2017-07-26 01:27:57
【问题描述】:
我正在关注关于使用 python v3.6 使用 scikit-learn 进行机器学习的决策树的教程。
这里是代码;
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import mglearn
import graphviz
from sklearn.datasets import load_breast_cancer
from sklearn.model_selection import train_test_split
from sklearn.tree import DecisionTreeClassifier
cancer = load_breast_cancer()
X_train, X_test, y_train, y_test = train_test_split(cancer.data, cancer.target, stratify=cancer.target, random_state=42)
tree = DecisionTreeClassifier(random_state=0)
tree.fit(X_train, y_train)
tree = DecisionTreeClassifier(max_depth=4, random_state=0)
tree.fit(X_train, y_train)
from sklearn.tree import export_graphviz
export_graphviz(tree, out_file="tree.dot", class_names=["malignant", "benign"],feature_names=cancer.feature_names, impurity=False, filled=True)
import graphviz
with open("tree.dot") as f:
dot_graph = f.read()
graphviz.Source(dot_graph)
如何使用 Graphviz 查看 dot_graph 中的内容?据推测,它应该看起来像这样;
【问题讨论】:
-
检查export_graphviz函数,您可以通过该函数将.dot转换为其他格式,例如.png
标签: python python-3.x scikit-learn graphviz decision-tree