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.