I am using scikit to do text classification of short phrases to their meaning. Some examples are:
"Yes" - label.yes
"Yeah" - label.yes
...
"I don't know" - label.i_don't_know
"I am not sure" - label.i_don't_know
"I have no idea" - label.i_don't_know
Everything worked pretty well using TfidfVectorizer and a MultinomialNB classifier.
The problem occurred when I added a new text/label pair:
"I" - label.i
Predicting the class for "I" still returns label.i_don't_know even though the text is exactly in the training data like this, which is probably due to the fact that the unigram "I" occurs more often in label.i_don't_know than in label.i.
Is there a classifier that will give comparable or better performance on this task and guarantee that predictions of training data elements are returned correctly?
This code illustrates the problem further:
from sklearn.feature_extraction.text import TfidfVectorizer
from sklearn.naive_bayes import MultinomialNB
#instantiate classifier and vectorizer
clf=MultinomialNB(alpha=.01)
vectorizer =TfidfVectorizer(min_df=1,ngram_range=(1,2))
#Apply vectorizer to training data
traindata=['yes','yeah','i do not know','i am not sure','i have no idea','i'];
X_train=vectorizer.fit_transform(traindata)
#Label Ids
y_train=[0,0,1,1,1,2];
#Train classifier
clf.fit(X_train, y_train)
print clf.predict(vectorizer.transform(['i']))
The code outputs label 1, but the correct classification would be label 2.