0

I have two arrays from which I have to find the accuracy of my prediction.

predictions = [1, 0, 0, 1, 1, 1, 0, 1, 1, 0]

     y_test = [1, 0, 0, 1, 0, 1, 0, 1, 1, 1]

so in this case, the accuracy is = (8/10)*100 = 80%

I have written a method to do this task. Here is my code, but I dont get the accuracy of 80% in this case.

def getAccuracy(y_test, predictions):
    correct = 0
    for x in range(len(y_test)):
        if y_test[x] is predictions[x]:
            correct += 1
    return (correct/len(y_test)) * 100.0

Thanks for helping me.

Mass17
  • 1,555
  • 2
  • 14
  • 29

3 Answers3

1

You're code should work, if the numbers in the arrays are in a specific range that are not recreated by the python interpreter. This is because you used is which is an identity check and not an equality check. So, you are checking memory addresses, which are only equal for a specific range of numbers. So, use == instead and it will always work.

For a more Pythonic solution you can also take a look at list comprehensions:

assert len(predictions) == len(y_test), "Unequal arrays"
identity = sum([p == y for p, y in zip(predictions, y_test)]) / len(predictions) * 100
Hielke Walinga
  • 2,677
  • 1
  • 17
  • 30
  • If I use "==" or assert like you mentioned, I dont get 80% accuracy in this case, instead I get 0. – Mass17 Oct 28 '18 at 13:15
  • Then I am afraid there is an issue somewhere else in the code. I have literally copies all code visible in the answer and I get 80%. Maybe take a look at all the code, see if you find the answer somewhere else. You can else put print statements in your functions to see if the arrays are really what you think they are. – Hielke Walinga Oct 28 '18 at 13:19
  • okay sorry!! I had some issue with the online compiler. Now it works!! Thanks @Hielke Walinga – Mass17 Oct 28 '18 at 13:21
0

if you want to take 80.0 as result for your example, It's doing that.

jundo
  • 66
  • 1
  • 10
-1

Your code gives 80.0 as you wanted, however you should use == instead of is, see the reason.

def getAccuracy(y_test, predictions):
   n = len(y_test) 
   correct = 0
   for x in range(n):
       if y_test[x] == predictions[x]:
           correct += 1
   return (correct/n) * 100.0


predictions = [1, 0, 0, 1, 1, 1, 0, 1, 1, 0]
y_test = [1, 0, 0, 1, 0, 1, 0, 1, 1, 1]

print(getAccuracy(y_test, predictions))
80.0

Here's an implementation using Numpy:

import numpy as np

n = len(y_test)
100*np.sum(np.isclose(predictions, y_test))/n

or if you convert your lists to numpy arrays, then

100*np.sum(predictions == y_test)/n
Andreas K.
  • 9,282
  • 3
  • 40
  • 45
  • Thanks @AndyK. It works!! I dont know why some people have downgraded. – Mass17 Oct 28 '18 at 13:22
  • I initially posted only the numpy version and only later updated my answer. That's why someone downvoted. No worries though, I am glad it worked for you. – Andreas K. Oct 28 '18 at 14:19