python - 我该如何解决这个错误,预期的 str、字节或 os.PathLike 对象,而不是 _io.StringIO
问题描述
- 这是我的代码
import numpy as np
import pandas as pd
features=pd.read_excel('temps.xlsx')
features=pd.get_dummies(features)
features.iloc[:,5:].head(5)
features.head()
labels=np.array(features['actual'])
features=features.drop('actual', axis=1)
features_list=list(features.columns)
features=np.array(features)
from sklearn.model_selection import train_test_split
train_features, test_features, train_labels, test_labels=train_test_split(features, labels,
test_size=0.25, random_state=42)
# The baseline predictions are the historical averages
baseline_preds=test_features[:, features_list.index('average')]
#Baseline errors, and display average baseline error
baseline_errors=abs(baseline_preds - test_labels)
print('Average baseline error: ', round(np.mean(baseline_errors), 2))
import pickle
from sklearn.ensemble import RandomForestRegressor
rf=RandomForestRegressor(n_estimators=1000, random_state=42)
# Use the forest's predict method on the test data
rf.fit(train_features, train_labels)
pred=rf.predict(test_features)
# Calculate the absolute errors
errors=abs(pred- test_labels)
# Print out the mean absolute error (mae)
print('mean absolute errors:', round(np.mean(errors), 2), 'degrees.')
# Calculate mean absolute percentage error (MAPE)
MAPE=100 * (errors/test_labels)
# Calculate and display accuracy
accuracy= 100 - np.mean(MAPE)
print('Accuracy:', round(accuracy, 2), '%.')
# Import tools needed for visualization
from sklearn. tree import export_graphviz
import pydotplus
from io import StringIO
dot_data=StringIO()
# Pull out one tree from the forest
tree=rf.estimators_[5]
# Export the image to a dot file
export_graphviz(tree, out_file='dot_data', feature_names=features_list, rounded=True, precision=1)
# Use dot file to create a graph
graph=pydotplus.graph_from_dot_file(dot_data.getvalue())
# Write graph to a png file
`graph.write_png('tree.png')`
TypeError: expected str, bytes or os.PathLike object, not _io.StringIO
这是错误日志
TypeError Traceback (most recent call last)
<ipython-input-5-b03567420729> in <module>
41 export_graphviz(tree, out_file='dot_data', feature_names=features_list, rounded=True, precision=1)
42 # Use dot file to create a graph
---> 43 graph=pydotplus.graph_from_dot_file(dot_data)
44 # Write graph to a png file
45 graph.write_png('tree.png')
311 """
312
--> 313 fd = open(path, 'rb')
314 data = fd.read()
315 fd.close()
TypeError: expected str, bytes or os.PathLike object, not _io.StringIO
我试过使用:
graph=pydotplus.graph_from_dot_file(dot_data.getvalue())
but am getting this error,
-
FileNotFoundError Traceback (most recent call
last) <ipython-input-24-fd06e8541947> in <module>
45 export_graphviz(tree, out_file='dotfile', feature_names=features_list, rounded=True, precision=1)
46 # Use dot file to create a graph
---> 47 graph=pydotplus.graph_from_dot_file(dot_data.getvalue())
48 # Write graph to a png file
49 graph.write_png('tree.png')
C:\Wandia den\anaconda
packages\lib\site-packages\pydotplus\graphviz.py in
graph_from_dot_file(path)
311 """
312
--> 313 fd = open(path, 'rb')
314 data = fd.read()
315 fd.close()
FileNotFoundError: [Errno 2] No such file or directory: ''
解决方案
由于堆栈跟踪状态dot_data
是一个StringIO
实例,但需要一个路径(以 a或str
实例的形式)。bytes
PathLike
幸运的是StringIO
,允许通过其getvalue
方法检索其内容。
尝试这个应该可以解决您的问题:
graph=pydotplus.graph_from_dot_file(dot_data.getvalue())
推荐阅读
- ios - 点击推送通知:无法定向到特定的 ViewController
- javascript - JavaScript 画布 + requestAnimationFrame 口吃
- html - 如何使 2 张图像同时响应
- laravel - 将 Laravel mysql 迁移转换为 postgresql 时出现索引错误
- swift - '#selector' 的参数不引用 Swift 按钮目标中的 '@objc' 方法、属性或初始化程序
- css - 为什么 ThemeProvider (Material UI) 在这里对我不起作用?
- laravel - 一般错误:1364 字段“学生 ID”没有默认值
- configuration - Sublime Text 3 中的 set_layout 设置
- c# - AutoCAD C#/.Net:公开实体坐标
- git - 分支落后于 'GitHub/master' 1 次提交