首页 > 解决方案 > 使用pickle加载模式时出现UnicodeDecodeError

问题描述

我正在尝试使用 pickle 加载随机森林模型。该模型是使用版本 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))

我得到的错误如下:

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)

标签: pythonscikit-learnpicklerandom-forest

解决方案


推荐阅读