Calculate the accuracy of a machine learning model without sklearn

Viewed 5306

I'm trying to calculate the accuracy of a model I created using the function below:

def accuracy(y_true, y_pred):
    accuracy = np.mean(y_pred == y_true)
    return accuracy

Sometimes it displays the accuracy correctly and sometimes its incorrect. Can someone explain how can i fix the function to have it display the same accuracy as sklearn accuracy_score. Here's an example of the results I am getting from my method.

y_true
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0]

y_pred
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0]

KNN classification accuracy:  0.0
KNN classification accuracy sklearn:  0.9428571428571428
2 Answers

With numpy you can do the following:

import numpy as np
acc = np.sum(np.equal(y_true, y_pred)) / len(y_true)

The way you set it up, y_pred == y_true will always be False if even one value in your list is not the same. When you do np.mean(False) you will get a value of 0.0.

What you want to do is iteratively find whether each item in your lists are equal. Here is the solution I made:

def accuracy(y_true,y_pred,normalize=True):
    accuracy=[]
    for i in range(len(y_pred)):
        if y_pred[i]==y_true[i]:
            accuracy.append(1)
        else:
            accuracy.append(0)
    if normalize==True:
        return np.mean(accuracy)
    if normalize==False:
        return sum(accuracy)

The accuracy list will keep track of if each item of y_pred is equal to y_true. Like in https://scikit-learn.org/stable/modules/model_evaluation.html#accuracy-score, if they are equal, then 1 should be appended. If they are not, then 0 should be appended.

You did not include this in your original function, but just in case you were curious, I added the normalize option just like sklearn. Normalize will automatically be true and will find the mean, but this is the code if you wanted the count of how many were accurate.

Related