Issue
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
Solution
With numpy you can do the following:
import numpy as np
acc = np.sum(np.equal(y_true, y_pred)) / len(y_true)
Answered By - Antoine Dubuis
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.