我正在尝试在IPython中运行这个机器学习树算法代码:
from sklearn.datasets import load_iris
from sklearn.tree import DecisionTreeClassifier
iris = load_iris()
X = iris.data[:, 2:] # petal length and width
y = iris.target
tree_clf = DecisionTreeClassifier(max_depth=2)
tree_clf.fit(X, y)
from sklearn.tree import export_graphviz
export_graphviz(tree_clf, out_file=image_path("iris_tree.dot"),
feature_names=iris.feature_names[2:],
class_names=iris.target_names,
rounded=True,
filled=True
)
Run Code Online (Sandbox Code Playgroud)
我对export_graphviz不熟悉,有没有人知道如何纠正这个问题?
我猜你正在关注Aurelien Geron的"用Scikit-Learn和TensorFlow进行机器学习"一书.我在尝试"决策树"一章时遇到了同样的问题.你可以随时参考他的GitHub笔记本 .对于您的代码,您可以参考" 决策树 "笔记本.下面我粘贴笔记本中的代码.请继续看一下笔记本电脑.
# To support both python 2 and python 3
from __future__ import division, print_function, unicode_literals
# Common imports
import numpy as np
import os
# to make this notebook's output stable across runs
np.random.seed(42)
# To plot pretty figures
%matplotlib inline
import matplotlib
import matplotlib.pyplot as plt
plt.rcParams['axes.labelsize'] = 14
plt.rcParams['xtick.labelsize'] = 12
plt.rcParams['ytick.labelsize'] = 12
# Where to save the figures
PROJECT_ROOT_DIR = "."
CHAPTER_ID = "decision_trees"
def image_path(fig_id):
return os.path.join(PROJECT_ROOT_DIR, "images", CHAPTER_ID, fig_id)
def save_fig(fig_id, tight_layout=True):
print("Saving figure", fig_id)
if tight_layout:
plt.tight_layout()
plt.savefig(image_path(fig_id) + ".png", format='png', dpi=300)
Run Code Online (Sandbox Code Playgroud)
小智 6
为了摆脱所有混乱,只需删除image_path,现在out_file="iris_tree.dot",在运行该命令后,文件将保存在您命名的文件夹中iris_tree.在Microsoft Word中打开该文件并复制其所有内容.现在打开浏览器并输入"webgraphviz",然后单击第一个链接.然后删除在空白处写入的内容并粘贴从中复制的代码iris_tree.然后单击"生成图形".向下滚动,图表就绪.
小智 2
你必须纠正
out_file=image_path("iris_tree.dot"),
在下面的代码行中:
out_file="C:/Users/VIDA/Desktop/python/iris_tree.dot",
| 归档时间: |
|
| 查看次数: |
3388 次 |
| 最近记录: |