python中的数据pickle错误[重复]
Posted
技术标签:
【中文标题】python中的数据pickle错误[重复]【英文标题】:Error in data pickle in python [duplicate] 【发布时间】:2017-01-03 19:17:10 【问题描述】:我需要将我的实习生数据集保存在数据泡菜中。我使用了以下代码。
self.featureCounts = collections.defaultdict(lambda :0)
self.featureVectors = []
self.labelCounts = collections.defaultdict(lambda :0)
def SaveOnPickle(self):
f = open('dict.pickle', 'wb')
pickle.dump(self.featureCounts, f)
f.close()
运行时出现如下错误
Traceback (most recent call last):
File "C:/wamp64/www/M360/TrainClassifier.py", line 76, in <module>
Predic.SaveOnPickle()
File "C:/wamp64/www/M360/TrainClassifier.py", line 50, in SaveOnPickle
pickle.dump(self.featureCounts, f)
File "C:\Python27\lib\pickle.py", line 1376, in dump
Pickler(file, protocol).dump(obj)
File "C:\Python27\lib\pickle.py", line 224, in dump
self.save(obj)
File "C:\Python27\lib\pickle.py", line 331, in save
self.save_reduce(obj=obj, *rv)
File "C:\Python27\lib\pickle.py", line 401, in save_reduce
save(args)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 568, in save_tuple
save(element)
File "C:\Python27\lib\pickle.py", line 286, in save
f(self, obj) # Call unbound method with explicit self
File "C:\Python27\lib\pickle.py", line 754, in save_global
(obj, module, name))
pickle.PicklingError: Can't pickle <function <lambda> at 0x021391B0>: it's not found as __main__.<lambda>
如何解决这个问题?
【问题讨论】:
Pickling class instances 【参考方案1】:你可以用泡菜
你可以这样保存你的模型:
import pickle
f = open('my_classifier.pickle', 'wb')
pickle.dump(classifier, f)
f.close()
以及稍后加载:
import pickle
f = open('my_classifier.pickle', 'rb')
classifier = pickle.load(f)
f.close()
【讨论】:
我试过这段代码。但是出现了错误。我把它放在我该如何解决这个问题中?以上是关于python中的数据pickle错误[重复]的主要内容,如果未能解决你的问题,请参考以下文章
python3中的Pickle vs cPickle(?)[重复]
分析多进程 Python 脚本时出现神秘的 pickle 错误