0

I need to compare two arrays made only by 0 and 1:

A = [0,1,0,0,1]
B = [1,1,0,0,1]

I want to get the number of elements that aren't equals and the percentage, i tried this code:

print(numpy.mean(A == B))

but sometimes i get 0 when some elements are equal...

5 Answers5

2

To get the number of elements that aren't equal in the two lists index wise, you can do this :

noneq = sum(i==j for i, j in zip(A, B))

To get the percentage, you can simple calculate noneq/len(A) or noneq/len(B)

Arkistarvh Kltzuonstev
  • 6,824
  • 7
  • 26
  • 56
  • thanks, +1 from me. I have a question though: I would expect this `sum(i==j for i, j in zip(A, B))` (your solution) to be faster that `sum([i==j for i, j in zip(A, B)])` (same, but a list is created and summed) but, from the few tests I have performed, it does seem the opposite. Any idea why? – nikeros Dec 02 '21 at 11:31
  • @nikeros Take a look at multiple discussions on this [in this thread](https://stackoverflow.com/questions/47789/generator-expressions-vs-list-comprehensions) – Arkistarvh Kltzuonstev Dec 02 '21 at 11:38
0

You could use list comprehension to create list of whether the values in indexes are equal and then calculate the mean of the said list.

numpy.mean([x == y for x, y in zip(A, B)])
tiitinha
  • 54
  • 1
  • 3
0
import numpy as np

A = np.array([0,1,0,0,1])

B = np.array([1,1,0,0,1])

# number of
(A!=B).sum() #1

# percentage
(A!=B).sum()*100 / A.size
Lee
  • 29,398
  • 28
  • 117
  • 170
0

use count_nonzero to Count the number of elements satisfying the condition

A = np.array([0,1,0,0,1]) 
B = np.array([1,1,0,0,1])
print(np.count_nonzero((A==B) == True))

for the percentage use np.count_nonzero/shape

0

While list comprehension is always the most elegant solution - also esthetically, in my view - I found the following slightly faster:

def calc(v1, v2):
    s = 0
    for i, x in enumerate(v1):
        s += x == v2[i]
    return s / len(v1)

Clearly, in all cases, you should always check/be aware of what happens if A and B hace different lengths (for example, the code I have just share would return an IndexError if len(v1) > len(v2); in that respect, zip behaves differently in different version of python)

nikeros
  • 3,302
  • 2
  • 10
  • 26
  • What happen in Python 3.9.9? – VincentSama Dec 02 '21 at 14:50
  • @VincentSama it will truncate to the shortest one. In other words, if you pass `A=[1,2,3,4]` and `B=[5,6,7]`, `zip(A,B)` will only return 3 tuples, `(1, 5), (2, 6), (3, 7)`. I think the default behaviour does not change in 3.10 but you can pass a parameter `strict=True`, which iwll essentially throw an exception in case they have different lengths – nikeros Dec 02 '21 at 15:21