python 中的数据 pickle 错误
Error in data pickle in python
我需要在 data pickle 中保存我的实习生数据集。我使用了以下代码。
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>
如何解决这个问题?
您可以使用 Pickle
您可以这样保存您的模型:
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()
我需要在 data pickle 中保存我的实习生数据集。我使用了以下代码。
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>
如何解决这个问题?
您可以使用 Pickle
您可以这样保存您的模型:
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()