Python: Loaded NLTK Classifier not working -
i'm trying train nltk classifier sentiment analysis , save classifier using pickle. freshly trained classifier works fine. however, if load saved classifier classifier either output 'positive', or 'negative' examples.
i'm saving classifier using
classifier = nltk.naivebayesclassifier.train(training_set) classifier.classify(words_in_tweet) f = open('classifier.pickle', 'wb') pickle.dump(classifier, f) f.close()
and loading classifier using
f = open('classifier.pickle', 'rb') classifier = pickle.load(f) f.close() classifier.classify(words_in_tweet)
i'm not getting errors. idea problem be, or how debug correctly?
the place pickled classifier can go wrong feature extraction function. must used generate feature vectors classifier works with.
the naivebayesclassifier
expects feature vectors both training , classification; code looks if passed raw words classifier instead (but presumably after unpickling, otherwise wouldn't different behavior before , after unpickling). should store feature extraction code in separate file, , import
in both training , classifying (or testing) script.
i doubt applies op, nltk classifiers take feature extraction function argument constructor. when have separate scripts training , classifying, can tricky ensure unpickled classifier finds same function. because of way pickle
works: pickling saves data, not code. work, put extraction function in separate file (module) scripts import. if put in in "main" script, pickle.load
in wrong place.
Comments
Post a Comment