Home > other >  How to get the percentage of non Matching at particular Element value in an two arrays
How to get the percentage of non Matching at particular Element value in an two arrays

Time:08-11

I have an array like: import numpy as np

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

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

Requirement:

  where the elementvalue of 1 in an array 'a' is equal to element value of 1 in array 'b'.
  Need to access the percentage of matching only with element value 1,not at zero

I tried with:

  matching_error=(np.mean(a!=b)

Output:

   0.25
   # because out of 24 elements in an array 18 are matching(here 0,1 both the values are 
   in matching action), I need matching action at only element value 1

Required Output:

   0.83
   #because out of 24 elements, the element value is matching at 4 points

CodePudding user response:

Following your previous logic, you can do:

1-np.mean((a==b) & (a==1))  # 1 - mean(values are equal AND equal to 1)

Alternative:

np.mean((a!=b) | (a!=1))    # mean(values are different OR not equal to 1)

output: 0.8333333333333334

CodePudding user response:

Since your data is (implicitly) binary, i.e. only consisting of 0s and 1s, one could use

1 - np.mean(a b==2)
>0.8333333333333334

CodePudding user response:

Given the specified result, I believe what the question is attempting to ask is equivalent to this:

For what percentage of array positions is the item in at least one of the arrays equal to zero.

This can be calculated like this:

np.mean( (a==0) | (b==0) )

Output:

0.8333333333333334
  • Related