how do i solve this error expected Str bytes or os.PathLike object not io.StringIO

  • here is my code
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

Here is the error log

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

I’ve tried using:

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: ''

This Post Has One Comment

  1. sovis

    export_graphviz(tree, out_file=’dot_data’…) or graph_from_dot_file(dot_data) is your issue…out_file : file object or string, optional (default=None) Handle or name of the output file. If None, the result is returned as a string

Leave a Reply