Find differences between arrays, with probability of variation

Asked

Viewed 158 times

3

Good afternoon guys, I’m reading about the numpy library setdiff1d function (https://docs.scipy.org/doc/numpy/reference/generated/numpy.setdiff1d.html).

It basically serves to express the different values between 2 arrays, for example (found in the link itself):

>>> a = np.array([1, 2, 3, 2, 4, 1])
>>> b = np.array([3, 4, 5, 6])
>>> np.setdiff1d(a, b)
array([1, 2])

I would like to know if you have any function to insert a percentage of variation in the values to be compared.

I explain, as shown in the example, the setdiff1d function will return the values that are exactly different between the 2 arrays.

However, if I’m working with floats, and I want to allow a percentage of variation between these values, for example, considering 3.35 and 3.34 between two arrays as equal values, has some specific function to work in this way, where I can set as argument a margin of variation between the values to be compared?

grateful.

1 answer

1

Try it this way:

limit = 0.01
np.unique(a[(np.abs(np.subtract.outer(a,b)) > limit).all(1)])
  • AP wants a percentage of variation, not a specific value. It would be constructive to explain the logic of the code’s operation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.