2

I have a numpy array being generated from a function as follows

 circles = [[ 56, 152, 26],
 [288, 300, 25],
 [288, 362,  25],
 [288, 238,  24],
 [318, 298,  45],
 [220, 366, 29]]

I want to check if all the values in the first element of each subarray are consistent (mathematically close, not differing by a large amount i.e. > 5) and remove the subarrays that don't conform to this condition. So in this case, i want to remove any subarray that is greater than 288 + 5 or less than 288 - 5. Any thoughts?

2
  • what's your desired output in this case? Commented Sep 7, 2018 at 6:08
  • [[288, 300, 25], [288, 362, 25], [288, 238, 24]] Commented Sep 7, 2018 at 13:17

1 Answer 1

2

A possible solution using mode:

>>> from scipy.stats import mode
>>> eps = 5
>>> most_freq = mode(circles[:, 0])[0][0]
>>> mask = np.abs(circles[:, 0] - most_freq) <= eps
>>> circles[mask]
array([[288, 300,  25],
   [288, 362,  25],
   [288, 238,  24]])

Edit: if your circles array is limited to to non-negative integers, you can use the following expression for most_freq:

most_freq = np.bincount(circles[:, 0]).argmax()
Sign up to request clarification or add additional context in comments.

3 Comments

When i try that it gives me this error TypeError: list indices must be integers or slices, not tuple
@HarshVedant, Looks, your circles is a list if lists. Try circles = np.array(circles) before processing it.
Never mind, i was messing around in the python terminal and not my actual code, where it wasn't a numpy array. This is brilliant, thanks!

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.