Reputation: 13
I want to classify many sentences with Naive Bayes classifier with 5 categories and I can do, but I can not create a confusion matrix. I researched much but I could not find. These are my code, please teach me how to create a confusion matrix over these code:
nb_classifier = NaiveBayesClassifier.train(trainData)
print(accuracy(nb_classifier, testData))
#Output is 0.8152876648699048
print(nb_classifier.labels())
#Output is ['cat1', 'cat2', 'cat3', 'cat4', 'cat5']
Upvotes: 0
Views: 8160
Reputation: 4004
It might be worth using the following way to build the classifier. The confusion matrix is also included.
from sklearn.naive_bayes import MultinomialNB # or any other NB model
from sklearn.metrics import accuracy_score
from sklearn.metrics import confusion_matrix
nb_classifier = MultinomialNB()
nb_classifier.fit(X_train, y_train)
y_pred = nb_classifier.predict(X_test)
acc_score = accuracy_score(y_test, y_pred)
conf_mat = confusion_matrix(
y_test, y_pred, labels = ['cat1', 'cat2', 'cat3', 'cat4', 'cat5'])
print(acc_score)
print(conf_mat)
Upvotes: 2