How to calculate the accuracy score of a model in python?

naive_bayes
python

#1

I am currently trying to solve one classification problem using naive Bayes algorithm in python.I have created a model and also used it for predication .But I want to know how I can check the accuracy of my model in python.

    from sklearn.naive_bayes import GaussianNB
    clf = GaussianNB()
    clf.fit(features_train,labels_train)
    pred = clf.predict(features_test)

#2

Hi @harry,

You can make a confusion matrix -

from sklearn.metrics import confusion_matrix
confusion_matrix(labels_train, pred)

After this, Accuracy = (Number of elements correctly classified)/(Total elements)
Hope this helps.

Regards,
Aayush


#3

Hey Aayush,

  1. Shouldn’t it be ‘labels_test’ instead of labels_train in the first input?
  2. Is the confusion matrix applicable for any ML models?

Regards,
Akshay


#4

Hi,

You can use sklearn implementation of accuracy_score function.

from sklearn.metrics import accuracy_score
accuracy_score(labels_test,pred)