使用带有scikit-learn决策树的for循环时的问题

问题描述 投票:0回答:1

我正尝试使用scikit-learn树库通过使用tree.export_graphviz()函数生成.dot文件来绘制决策树。我想使用点bash命令行将这些.dot文件转换为.pdf文件。

我的python代码:

from sklearn.datasets import load_iris
iris=load_iris()
from sklearn import tree
for i in range(3,10):
    clf=tree.DecisionTreeClassifier(max_leaf_nodes=i)
    clf=clf.fit(iris.data,iris.target)
    file_name = 'tpsk1-' + str(i) + '.dot'
    tree.export_graphviz(clf,out_file=file_name)

在这里,我正在编写一个for循环,i的范围是3到10,以导出7个点文件。但是当我执行bash脚本将它们转换为pdf文件时,发生了一些奇怪的事情。

我的bash脚本:

for i in 3 4 5 6 7 8 9
do
        dot_file="tpsk1-$i.dot"
        pdf_file="tpsk1-$i.dot"
        dot -Tpdf $dot_file -o $pdf_file
done

结果:

Error: tpsk1-3.dot: syntax error in line 12 near '�S'
Warning: syntax ambiguity - badly delimited number '.0S' in line 12 of tpsk1-3.dot splits into two tokens
Warning: syntax ambiguity - badly delimited number '3r' in line 49 of tpsk1-3.dot splits into two tokens
Error: tpsk1-4.dot: syntax error in line 16 near 'X'
Warning: syntax ambiguity - badly delimited number '3r' in line 56 of tpsk1-4.dot splits into two tokens
Error: tpsk1-5.dot: syntax error in line 20 near 'ػ0'
Error: tpsk1-6.dot: syntax error in line 24 near '`'
Error: tpsk1-7.dot: syntax error in line 28 near '��'
Warning: syntax ambiguity - badly delimited number '1�' in line 31 of tpsk1-7.dot splits into two tokens
Warning: syntax ambiguity - badly delimited number '3r' in line 68 of tpsk1-7.dot splits into two tokens
Error: tpsk1-8.dot: syntax error in line 32 near '��'
Warning: syntax ambiguity - badly delimited number '0�' in line 32 of tpsk1-8.dot splits into two tokens
Warning: syntax ambiguity - badly delimited number '8z' in line 32 of tpsk1-8.dot splits into two tokens
Error: tpsk1-9.dot: syntax error in line 36 near '�Cb'

我试图删除for循环以编写一个单个点文件,但效果很好。

我的新python脚本:

from sklearn.datasets import load_iris
iris=load_iris()
from sklearn import tree
clf=tree.DecisionTreeClassifier(max_leaf_nodes=3)
clf=clf.fit(iris.data,iris.target)
file_name = 'tpsk1-3.dot'
tree.export_graphviz(clf,out_file=file_name)

我的dot bash命令:

dot -Tpdf tpsk1-3.dot -o tpsk1-3.pdf

有人可以向我解释发生了什么,我想我在这里怀念for循环中的一些智慧吗?非常感谢。

python-3.x bash scikit-learn dot
1个回答
0
投票

您的示例扩展名错误:

for i in 3 4 5 6 7 8 9
do
        dot_file="tpsk1-$i.dot"
        pdf_file="tpsk1-$i.dot"
        dot -Tpdf $dot_file -o $pdf_file
done

应为pdf_file="tpsk1-$i.pdf"

© www.soinside.com 2019 - 2024. All rights reserved.