2

I am trying to compare two 1d numpy arrays for mismatch as follows. I have this working with partial success.

import numpy as np
a= np.array([0,1,2,3,4,13])
b= np.array([0,1,2,3,4,10,11,12])
mis = max( np.sum(~np.isin(b,a)), np.sum(~np.isin(a,b)))
print(mis)

output: 3

expected output : 4 ( 13,10,11,12 are mismatches)

2 Answers 2

3

Why are you taking the max of the two sums instead of adding the sums? You are only grabbing the missing entries from a single array (the larger one) by doing that, when you clearly want both.

mis = np.sum(~np.isin(b,a)) + np.sum(~np.isin(a,b))
Sign up to request clarification or add additional context in comments.

1 Comment

I realized sum would do after posting this thanks @David Moreau
2

Your code would work if you add the np.sum:

mis = np.sum(~np.isin(b,a)) + np.sum(~np.isin(a,b))

However, check out setxor1d:

np.setxor1d(a,b) 
# out 
# array([10, 11, 12, 13])

You can easily get the length then.

1 Comment

that's pretty slick np.setxor1d thanks @Quang Hoang

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.