Skip to content
Advertisement

how to calculate accuracy based on two lists python?

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?

Advertisement

Answer

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement