I have two lists.
a = [0,0,1,1,1] # actual labels
b = [1,1,0,0,1] # predicted labels
How can I calculate accuracy based on these lists?
sum(1 for x,y in zip(a,b) if x == y) / len(a)
This will give you the percentage that were correct - that is, the number correct over the total number. It works by calculating the number that are equal between the two lists then dividing by the total number of labels.
Also note that if you're not using Python 3, it will have to look like this:
sum(1 for x,y in zip(a,b) if x == y) / float(len(a))
To ensure you get a decimal representation of the number
Since you've tagged numpy
, here's a numpy
solution:
import numpy as np
a = np.array([0,0,1,1,1]) # actual labels
b = np.array([1,1,0,0,1]) # predicted labels
correct = (a == b)
accuracy = correct.sum() / correct.size
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With