1

I have a set of numbers arranged in a numpy array:

large_set = np.array([5, 6, 7, 8, 9])

I have three smaller sets:

small_set_1 = np.array([4.65, 4.66, 4.88])
small_set_2 = np.array([6.32, 6.77, 6.98, 7.46])
small_set_3 = np.array([9.75, 9.99, 10.34])

Is there any direct way to determine in python that small_set_x falls within large_set or not?

I did like this but its not very direct:

if (min(small_set_1) > min(large_set) or min(small_set_1) > max(large_set)): 
    pass
elif (min(small_set_1) < min(large_set) or min(small_set_1) < max(large_set)):
    pass
elif (max(small_set_1) > min(large_set) or max(small_set_1) > max(large_set)): 
    pass
elif (max(small_set_1) < min(large_set) or max(small_set_1) < max(large_set)):
    pass

In the end, I should simply print("The small set is within the large set"). If possible, it can be nice to be able to print something like 'The small range doesn't lie in the large set, they are at the beginning of the large_set' (matches small_set_1 in this case).

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Lalu
  • 137
  • 9
  • 1
    Will the arrays always be sorted? – Tomerikoo Dec 08 '20 at 15:56
  • I am confused. You mentioned small_set_2 but in your sample code they are all small_set_1 – Z Li Dec 08 '20 at 15:56
  • 1
    The small set is within the large set only if `large.min() < small.min() and large.max() > small.max()`. That should be all you need to check. What are all the other conditions for? – Pranav Hosangadi Dec 08 '20 at 15:56
  • yes the arrays are always sorted..and the i try for each small_set starting with small_set_1. Ofcourse, small_set_2 is within the large set. – Lalu Dec 08 '20 at 15:58
  • 1
    If the arrays are ***guaranteed*** to be sorted, then you can just do `if large[0] <= small[0] and small[-1] <= large[-1]`. I would post that as an answer but your `elif`s confuse me so I'm not sure what the question is... – Tomerikoo Dec 08 '20 at 15:59
  • Assuming that the arrays are sorted we can check to see if the first value of the small is larger than the first value of the large, and the same for the last element of each. – shn Dec 08 '20 at 16:00
  • Could you please write it an answer because `large.min() < small.min() and large.max() > small.max()` works! Thank you very much. – Lalu Dec 08 '20 at 16:01
  • See https://stackoverflow.com/questions/12200580/numpy-function-for-simultaneous-max-and-min – Peter Wood Dec 08 '20 at 16:04
  • It would be nice it `numpy.array` could return `minmax`, but it's an open issue: https://github.com/numpy/numpy/issues/9836 – Peter Wood Dec 08 '20 at 16:16

1 Answers1

2

Consider the blue box is the small set and the red box is the large set. enter image description here

The small set is completely within the large set only if the minimum of the large set is less than the minimum of the small one AND the maximum of the large set is greater than the maximum of the small one. In python, large.min() < small.min() and large.max() > small.max().

Like @Tomerikoo says here, if the arrays are pre-sorted, the first element is the minimum and the last element is the maximum, so all you need is large[0] < small[0] and large[-1] > small[-1].

The other if..elif conditions cover the other cases:

  1. large.min() > small.min() and large.max() > small.max() enter image description here
  2. large.min() < small.min() and large.max() < small.max() enter image description here
  3. large.min() > small.min() and large.max() < small.max() (the "small" set isn't really smaller in this case) enter image description here

Your problem is that you use the or condition instead of the and condition, which returns True if any of the conditions are True, and that you mixed up the min and max calls. You only ever need to compare the two mins with each other and the two maxes with each other to check for overlap.

For non-overlapping sets, you need to compare the min of one to the max of the other.

  1. large.max() < small.min() (Obviously large.min() is less than small.max(), so no need to check) enter image description here
  2. large.min() > small.max() (Obviously large.max() is greater than small.min(), so no need to check) enter image description here
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70
  • Is there also a way to know..for small_set_1, print('the range doesnt lie in the large set, they are at the begining of the large_set) and for small_set_2, print('the range doesnt lie in the large set, they are at the end of the large_set) – Lalu Dec 08 '20 at 16:09
  • 1
    Wow! +1 for the visual demonstrations!!! Love it. And without intending, you just answered the above comment by OP – Tomerikoo Dec 08 '20 at 16:11
  • 1
    @Tomerikoo haha, I saw their comment and tailored my edit to answer it. Thanks, I'm glad the visual demonstration helps. – Pranav Hosangadi Dec 08 '20 at 16:29
  • @Lalu the other conditions I listed should help you figure out exactly how `small` and `large` are positioned relative to each other. – Pranav Hosangadi Dec 08 '20 at 16:30