I have two arrays as below:
b = array([[1, 0, 1],
[0, 0, 1]])
c = array([[ 0.5 , 0. ],
[ 0.34, 1. ],
[ 0. , 1. ]])
How to count matching values of two arrays based on same index value AND the element value of the array should non zero. Here matching value is based on index of the element and not the exact value.
For eg. in the above matrices, my two arrays are row of b and corresponding column of c. If you see, 0.5 c[0,0] in c matches with 1 (b[0,0]) of b. The value doesn't match but since they are non zero values and present in the same position in the respective arrays (both present at 0th index), it should be counted. If the value is zero in either array position then it won't be counted.
For the above matrices, I should get 2 hits, because first row of b and first column of c has common non-zero elements at only one value: b[0,0] or c[0,0]. Similarly, the second row of b has one match with second column of c: b[1,2] or c[2,1]
I tried the following code, but it gives sum as 3 instead of 2. Because it is also counting the case where both the position have 0 as value and it matches that too. I want non zero value at the same position in two arrays.
sum=0
for x in range(b.shape[0]):
sum+=np.sum((b[x,:]==0) == (c[:,x]==0))
I then tried this: this would first get the non zero indices of both the arrays and then compare. But it gives an error.
sum=0
for x in range(b.shape[0]):
sum+= np.sum((np.nonzero(b[x,:]!=0)) == (np.nonzero(c[:,x]!=0)))
ValueError: The truth value of an array with more than one element is ambiguous. Use a.any() or a.all()
Aucun commentaire:
Enregistrer un commentaire