Question

I ran the grid-search classifier from below in iPython:

http://scikit-learn.org/dev/auto_examples/grid_search_digits.html

and then tried to serialize it by pickling, but received the following traceback:

In [6]: with open('clf.pkl', 'wb') as f:
    cPickle.dump(clf, f)
   ...:     
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-6-577559be619c> in <module>()
      1 with open('clf.pkl', 'wb') as f:
----> 2     cPickle.dump(clf, f)
      3 

/Library/Frameworks/EPD64.framework/Versions/7.3/lib/python2.7/copy_reg.pyc in     _reduce_ex(self, proto)
     68     else:
     69         if base is self.__class__:
---> 70             raise TypeError, "can't pickle %s objects" % base.__name__
     71         state = base(self)
     72     args = (self.__class__, base, state)

TypeError: can't pickle instancemethod objects

Is there a work-around?

I am using the latest beta of both iPython and sklearn.

Thanks :)

Was it helpful?

Solution

The Grid Search classifier used to keep all estimators which created the pickle problem. This problem has been noted before and solved. This patch comment tells that you can pickle the classifier by just pickling best_estimator_. In your case that would be:

cPickle.dump(clf.best_estimator_, f)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top