Lakmal Geekiyanage Lakmal Geekiyanage - 3 months ago 6
Python Question

Error in data pickle in python

I need to save my trainee data-set in data pickle. I used following code.

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()


When run this, there was following error

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>


How to fix this issue?

Answer

You can use Pickle

You can save your model like this :

import pickle
f = open('my_classifier.pickle', 'wb')
pickle.dump(classifier, f)
f.close()

and for loading later :

import pickle
f = open('my_classifier.pickle', 'rb')
classifier = pickle.load(f)
f.close()
Comments