python - TypeError:需要一个类似字节的对象,而不是 Python 中 Image 命令的“str”
问题描述
我正在尝试在 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()
这将显示当前目录中的每个文件。就我而言,文件的名称与代码中显示的名称不同。
推荐阅读
- azure-devops - 将 PFX 添加到 VSTS 上的强名称 CSP
- office-js - 在 Windows Server 上加载 Word 加载项
- docker - 詹金斯:从奴隶推送到 ECR
- node.js - 如何下载部署在 Google Cloud 上的 Node.js 项目
- c# - How to restore a SQL Server backup in .NET Core
- javascript - 如何使用 JavaScript 按块读取任何本地文件?
- ansible - 使用在 1.7 中运行良好的 with_subelements 时出现 Ansible v2.2 错误
- javascript - 如何在另一个页面中创建表单提交链接并获取链接页面上提交的文本内容?
- laravel - 如何在 Laravel 5.4 中使用 ask() 测试 Artisan 命令
- java - 在不抑制警告的情况下删除 Java 中的未经检查的异常