1

I am trying to load a random forest model using pickle. The model was built with version 0.20.3 (scikit-learn).

# LOAD RF MODEL FROM DISK

n_estimators = 100
model_dir = "/home/DATA/Moz/models/"
model_name = "rf_{}_trees_B4_7classes.pkl".format(n_estimators)
model_path = os.path.join(model_dir, model_name)
rf = pickle.load(open(model_path, "rb"))

print("Model {} loaded from disk!".format(model_name))

The error I get is the following:

UnicodeDecodeError                        Traceback (most recent call last)
<ipython-input-6-3a315512bee1> in <module>
      5 model_name = "rf_{}_trees_B4_7classes.pkl".format(n_estimators)
      6 model_path = os.path.join(model_dir, model_name)
----> 7 rf = pickle.load(open(model_path, "rb"))
      8 
      9 print("Model {} loaded from disk!".format(model_name))

UnicodeDecodeError: 'ascii' codec can't decode byte 0xa2 in position 29: ordinal not in range(128)
ecd
  • 165
  • 1
  • 7
  • 1
    Does this work: https://stackoverflow.com/questions/28218466/unpickling-a-python-2-object-with-python-3/28218598#28218598 ? - Maybe try the encoding='bytes' solution from the second answer. – Lydia van Dyke Apr 08 '20 at 16:19
  • yes, it worked! thank you :) – ecd Apr 16 '20 at 14:43
  • Does this answer your question? [Unpickling a python 2 object with python 3](https://stackoverflow.com/questions/28218466/unpickling-a-python-2-object-with-python-3) – Lydia van Dyke Apr 16 '20 at 16:13

0 Answers0