【发布时间】:2018-04-11 20:56:03
【问题描述】:
我有一个 Python 3.6 脚本,用于训练 SKLearn 模型,然后使用以下代码保存模型:
with open('filepath', 'wb') as f:
pickle.dump(trained_model, f, protocol=2)
当我尝试在 python 3.6 中加载泡菜时,一切正常:
>>with open('filepath', 'rb') as f:
>> model = pickle.load(f)
>>
>>model
RandomForestClassifier(bootstrap=True, class_weight=None, criterion='gini',
max_depth=None, max_features='auto', max_leaf_nodes=None,
min_impurity_decrease=0.0, min_impurity_split=None,
min_samples_leaf=1, min_samples_split=2,
min_weight_fraction_leaf=0.0, n_estimators=80, n_jobs=1,
oob_score=False, random_state=None, verbose=0,
warm_start=False)
当我在 Python 2.7 中运行相同的 pickle.load 命令时,我收到以下错误:
>>with open('filepath', 'rb') as f:
>> model = pickle.load(f)
ValueError: non-string names in Numpy dtype unpickling
查看文档和类似案例,将协议设置为 2应该使 pickle 文件兼容。是什么导致了这个问题,我该如何解决?
【问题讨论】:
-
这是完整的回溯吗?
-
不幸的是。
-
此时我不能再说什么,因为您没有提供任何 minimal reproducible example 供我诊断。
-
查看更新,这可能对您有很大帮助。
标签: python python-2.7 scikit-learn pickle python-3.6