下面是关于使用PythonV3的教程。6.使用scikit学习,通过机器学习完成决策树。
这里是代码;
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_图中的内容?大概,它应该是这样的;
在jupyter笔记本中,以下绘制了决策树:
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
model = DecisionTreeClassifier()
model.fit(X, y)
dot_data = tree.export_graphviz(model,
feature_names=feature_names,
class_names=class_names,
filled=True, rounded=True,
special_characters=True,
out_file=None,
)
graph = graphviz.Source(dot_data)
graph
如果你想保存为png:
graph.format = "png"
graph.render("file_name")
graphviz。Source(点图)
返回一个graphviz。文件夹。源
对象。
g = graphviz.Source(dot_graph)
使用g.render()
创建图像文件。当我在没有参数的情况下在你的代码上运行它时,我得到了一个源代码。gv。pdf
但您可以指定不同的文件名。还有一个快捷方式g.view()
,用于保存文件并在适当的查看器应用程序中打开。
如果您将代码粘贴到富终端(例如带有内联图形的Spyder/IPython或Jupyter笔记本)中,它将自动显示图像,而不是对象的Python表示。
您可以使用IPython的display。陈列以下是一个例子:
from sklearn.tree import DecisionTreeClassifier
from sklearn import tree
model = DecisionTreeClassifier()
model.fit(X, y)
from IPython.display import display
display(graphviz.Source(tree.export_graphviz(model)))