Find if a sorted array of floats contains numbers in a certain range efficiently

2024/10/7 14:29:16

I have a sorted numpy array of floats, and I want to know whether this array contains number in a given range. Note that I'm not interested in the positions of the number in the array. I only want to know if there is at least one number that falls in the range.

Since I have to do the very same operation on a large numbers of intervals (the array remains constant during the entire operation), I'm concerned about the efficiency.

Can anyone help me?

Answer

As talked about in the comments, np.searchsorted could be useful and indeed it is. As stated in the question, the array stays the same, while we have different ranges across iterations. So, let's say the ranges are stored in a (n,2) shaped array, such that the first column represents the start, while the second column are the stop values of those ranges.

We would have a solution with np.searchsorted, like so -

np.searchsorted(a,ranges[:,0]) != np.searchsorted(a,ranges[:,1])

The idea is that if there's any number within a range, then the sorted indices found using the first column (start values) would be lesser than and thus not equal to the indices from the second column (stop values).

Sample run -

In [75]: a   # Input data array
Out[75]: 
array([ 13.,  20.,  22.,  24.,  36.,  50.,  52.,  60.,  64.,  65.,  65.,66.,  72.,  76.,  81.,  84.,  88.,  88.,  90.,  97.])In [76]: ranges # Array of ranges
Out[76]: 
array([[ 19.,  26.],[ 22.,  33.],[ 25.,  35.],[ 38.,  62.]])In [77]: np.searchsorted(a,ranges[:,0]) != np.searchsorted(a,ranges[:,1])
Out[77]: array([ True,  True, False,  True], dtype=bool)
https://en.xdnf.cn/q/118817.html

Related Q&A

Django Operation error: (2026, SSL connection error: SSL_CTX_set_tmp_dh failed)

I can not start my django server after running a statement through manage.py for generating class diagrams from db. And I always get this error but I dont know how to deal with it. OperationalError: (2…

TypeError with module object is not callable

I have a test folder the structure within the folder__init.py__ aa.py test.pyfor aa.pyclass aa:def __init__(self,max):self.max=maxprint max+1def hello(self):print(max)for test.pyimport aa abc = aa(100)…

How to access the GUI output?

Im developing one test bench which runs multiple tests via python gui and prints the output as below.A Passed B Passed C Passed D Passed E PassedButton from gui should be changed to Passed only when A,…

Is it possible to have an api call another api, having them both in same application?

I have a python application running on my localhost:3978. Is it possible to make an api call to http://localhost:3978/api/users from http://localhost:3978/api/accounts? @routes.get("/api/accounts…

Find word near other word, within N# of words

I need an enumerating regex function that identifies instances in a string when Word 1 is within N# words of Word 2 For example, here is my dataframe and objective: Pandas Dataframe Input data = [[ABC…

Create new files, dont overwrite existing files, in python

Im writing to a file in three functions and im trying not overwrite the file. I want every time i run the code i generate a new filewith open("atx.csv", w)as output:writer = csv.writer(output…

Python List comprehension execution order [duplicate]

This question already has answers here:Understanding nested list comprehension [duplicate](2 answers)Closed 4 years ago.matrix = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] squared = [[x**2 for x in row] for row…

Subtract two strings in python

I should calculate the difference between elements two different list. This is my code :import operator a = [5, 35.1, FFD] b = [8.5, 11.3, AMM] difference = [each[0] - each[1] for each in zi…

Python assignment for a phonebook

This weeks lab is based on the example on pages 53,54 of the wikibook "Non-Programmers Tutorial For Python" by Josh Cogliati (2005), (see http://en.wikibooks.org/wiki/Non-Programmer%27s_Tutor…

ImportError: No module named application [duplicate]

This question already has answers here:What is __init__.py for?(14 answers)Closed 6 years ago.I am running a flask application and connecting to database with Flask-mysqlAlchemy when I am running my s…