Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to calculate accuracy based on two lists python?

Tags:

python

numpy

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?

like image 455
user570593 Avatar asked Aug 10 '16 15:08

user570593


2 Answers

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

like image 79
James Avatar answered Nov 04 '22 11:11

James


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
like image 44
0x60 Avatar answered Nov 04 '22 11:11

0x60