Difference between score and accuracy_score in sklearn

Question:

Whats the difference between score() method in sklearn.naive_bayes.GaussianNB() module and accuracy_score method in sklearn.metrics module? Both appears to be same. Is that correct?

Asked By: A.R Naseef

||

Answers:

In general, different models have score methods that return different metrics. This is to allow classifiers to specify what scoring metric they think is most appropriate for them (thus, for example, a least-squares regression classifier would have a score method that returns something like the sum of squared errors). In the case of GaussianNB the docs say that its score method:

Returns the mean accuracy on the given test data and labels.

The accuracy_score method says its return value depends on the setting for the normalize parameter:

If False, return the number of correctly classified samples. Otherwise, return the fraction of correctly classified samples.

So it would appear to me that if you set normalize to True you’d get the same value as the GaussianNB.score method.

One easy way to confirm my guess is to build a classifier and call both score with normalize = True and accuracy_score and see if they match. Do they?

Answered By: Oliver Dain

Please find below the proof.

# Test score vs accuracy_score

from sklearn.naive_bayes import MultinomialNB 
multinom = MultinomialNB()
multinom.fit(X_train, y_train)
y_train_pred_multinom = multinom.predict(X_train)
y_test_pred_multinom = multinom.predict(X_test)

score = multinom.score(X_test, y_test)
acc_score = accuracy_score(y_test, y_test_pred_multinom, normalize = True)

print(f'Score:', score)
print(f'Accuracy Score:', acc_score)

Result:

Score: 0.9686274509803922
Accuracy Score: 0.9686274509803922
Answered By: heavenlyflower
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.