I have 2 np.array arrays, both consist of 0 and 1. It is necessary to compare them and count the number of inconsistencies. I tried to do this:

unique, counts = np.unique(X2==X3, return_counts=True) print(dict(zip(unique, counts))) 

But as a result, it gives about 300 million true and false values, although the length of both is 25000

    1 answer 1

    To calculate the number of inconsistencies of the corresponding elements of two 1D Numpy vectors:

     (X2 != X3).sum() 

    Example:

     In [42]: X2 = np.array([0,0,1,1,0,0]) In [43]: X3 = np.array([1,0,0,1,0,0]) In [44]: X2 != X3 Out[44]: array([ True, False, True, False, False, False]) # индексы несовпадающих значений In [45]: np.where(X2 != X3) Out[45]: (array([0, 2], dtype=int64),) # количество несоответствий In [46]: (X2 != X3).sum() Out[46]: 2 
    • Thanks, and serial numbers of incorrect values ​​can be deduced? - Midnight
    • @Midnight, added an example ... - MaxU