3

I have try the code from here: Save Naive Bayes Trained Classifier in NLTK. I want to classify tweet into positive class or negative class. this is my code:

#learning.py
def main_learning():
   .......
   classifier = nltk.NaiveBayesClassifier.train(feature_set)
   save_classifier(classifier)

   classifier2 = load_classifier()
   print classifier2.classify(get_features("My tweet is bad".split())

def save_classifier(classifier):
   f = open('my_classifier.pickle', 'wb')
   pickle.dump(classifier, f)
   f.close()

def load_classifier():
   f = with open('my_classifier.pickle')
   classifier = pickle.load(f)
   f.close
   return classifier

Then the output: negative

But, when I try the save_classifier and load_classifier in different method, the output become always positive class.

this is the code:

#learning.py
def main_learning():
   .......
   classifier = nltk.NaiveBayesClassifier.train(feature_set)
   save_classifier(classifier)

def test_classify():
   classifier = load_classifier()
   print classifier.classify(get_features("My tweet is bad".split())

the def save_classifier and load_classifier() are same with the first ones.

The second output is: positive. It should be still negative class.

What happen with my code?? Thanks

Edit:

From the answer from @Cassio then I edit my code:

def save_classifier(classifier):
       f = open('my_classifier.pickle', 'wb')
       pickle.dump(classifier, f)
       f.close()

def load_classifier():
   f = with open('my_classifier.pickle', 'rb')
   classifier = pickle.load(f)
   f.close
   return classifier

Actually it works.

4

1 回答 1

3

I don't have the environment setup to test out your code, but I have the feeling it's not right in the part where you save/load the pickle.

Referring to the Storing Taggers section of the NLTK book, I would change your code and do it like this:

def save_classifier(classifier):
   f = open('my_classifier.pickle', 'wb')
   pickle.dump(classifier, f, -1)
   f.close()

def load_classifier():
   f = open('my_classifier.pickle', 'rb')
   classifier = pickle.load(f)
   f.close()
   return classifier

Hope it helps.

于 2013-07-14T01:15:23.087 回答