Error pickling NaiveBayesClassifier

54 views
Skip to first unread message

Toddy Mladenov

unread,
Nov 7, 2012, 4:48:10 PM11/7/12
to nltk-...@googlegroups.com
Hello Everybody,

My understanding was that the simplest way to save trained classifier is to pickle it to a file however when I do the following:

>>> classifier = nltk.NaiveBayesClassifier.train(train_set)
>>> output = open('/Users/toddysm/Development/temp/classifier.pickle', 'wb')
>>> pickle.dump(mydic, classifier, pickle.HIGHEST_PROTOCOL)

I get the following error:

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 1370, in dump
    Pickler(file, protocol).dump(obj)
  File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pickle.py", line 203, in __init__
    self.write = file.write
AttributeError: 'NaiveBayesClassifier' object has no attribute 'write'

Any ideas how to save it?

Thanks!
Toddy

Michael Deeringer

unread,
Nov 7, 2012, 4:50:31 PM11/7/12
to nltk-...@googlegroups.com
I believe you want

pickle.dump(classifier, output, pickle.HIGHEST_PROTOCOL)



Toddy

--
 
 

Toddy Mladenov

unread,
Nov 7, 2012, 5:02:48 PM11/7/12
to nltk-...@googlegroups.com
wow! copy-and-paste programming can send me to jail :) 

Thanks!
Toddy

--
 
 

Toddy Mladenov

unread,
Nov 7, 2012, 5:13:53 PM11/7/12
to nltk-...@googlegroups.com
As a follow up on this. Is this still an issue? https://groups.google.com/forum/?fromgroups=#!topic/nltk-users/vijtEvhzhOo

I am having the same error when I load the pickle back. Changing to 1 solved the problem though so I am fine - just curious.

Toddy
Reply all
Reply to author
Forward
0 new messages