将 for 循环与 scikit-learn 决策树一起使用时出现问题

Problem when using for loop with scikit-learn decision tree

我正在尝试使用 scikit-learn 树库通过使用 tree.export_graphviz() 函数生成 .dot 文件来绘制决策树。我想使用 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)

我的点bash命令:

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

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

您的示例中的扩展名有误:

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"