[英]TypeError: a bytes-like object is required, not 'str' for Image command in Python
我正在尝试在 Python 中加载决策树的图像,但我无法这样做。
代码是:
from IPython.display import Image
#import pydotplus as pydot
from sklearn import tree
from os import system
train_char_label = ['1', '2']
park_Tree_File = open('park_tree.dot','w')
dot_data = tree.export_graphviz(dt_model, out_file=park_Tree_File,
feature_names = list(train_set),
class_names = list(train_char_label))
park_Tree_File.close()
print (pd.DataFrame(dt_model.feature_importances_, columns = ["Imp"], index
= train_set.columns))
system("dot -Tpng park_tree.dot -o park_tree.png") # This command is to OS
Image("park_tree.png") # use the image command to read the .png file
# and print on screen
我收到以下错误:
TypeError:需要一个类似字节的对象,而不是 Python 中 Image 命令的“str”
你能建议Image
命令出了什么问题吗? 之前的print
命令工作正常并且完美地打印了feature_importance
。
我有同样的问题。 我克隆了一个 github 文件夹,它出现了这个错误。 我发现文件名不同。 检查图像的路径是否正确。
import os
os.getwd()
此代码显示您的代码正在寻找图像“park_tree.png”的目录。 检查图像是否确实在目录中。 你可以这样做
os.listdir()
这将显示当前目录中的每个文件。 就我而言,文件名与代码中显示的名称不同。
我遇到了类似的问题,但几乎没有找到解决方法。
尝试改变pwd的绝对路径。 保持简单(没有任何数字、特殊字符、空格等),这个方法对我有用。 或者,如果您不想更改密码,请尝试对文件路径进行一些操作。 参考:在这里检查
如果您使用 Jupyter notebook 并且只需要插入图像一次,您可以导航到 Markdown 单元格 > 编辑(下拉)> 插入图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.