简体   繁体   English

查找间隔索引的有效方法

[英]Efficient way to find index of interval

I'm writing a spline class in Python. 我正在用Python编写样条类。 The method to calculate the the spline interpolated value requires the index of the closest x data points. 计算样条插值的方法需要最接近的x个数据点的索引。 Currently a simplified version looks like this: 当前,简化版本如下所示:

def evaluate(x):
    for ii in range(N): # N = len(x_data)
        if x_data[ii] <= x <= x_data[ii+1]:
            return calc(x,ii)

So it iterates through the list of x_data points until it finds the lower index ii of interval in which x lies and uses that in the function calc , which performs the spline interpolation. 因此,它将遍历x_data点的列表,直到找到x所在的区间的下标ii ,然后在执行样条插值的函数calc使用它。 While functional, it seems like this would be inefficient for large x_data arrays if x is close to the end of the data set. 虽然功能正常,但如果x接近数据集的末尾,这对于大型x_data数组似乎效率不高。 Is there a more efficient or elegant way to perform the same functionality, which does not require every interval to be checked iteratively? 是否有一种更有效或更优雅的方法来执行相同的功能,而不需要迭代检查每个间隔?

Note: x_data may be assumed to be sorted so x_data[ii] < x_data[ii+1] , but is not necessarily equally spaced. 注意:可以假定x_data已排序,因此x_data[ii] < x_data[ii+1] ,但不一定相等。

this is exactly what bisect is for https://docs.python.org/2/library/bisect.html 这正是bisect的含义, https: //docs.python.org/2/library/bisect.html

from bisect import bisect
index = bisect(x_data,x)
#I dont think you actually need the value of the 2 closest but if you do here it is
point_less = x_data[index-1]  # note this will break if its index 0 so you probably want a special case for that
point_more = x_data[index]

closest_value = min([point_less,point_more],key=lambda y:abs(x-y))

alternatively you should use binary search(in fact im pretty sure thats what bisect uses under the hood) .... it should be worst case O(log n) (assuming your input array is already sorted) 或者,您应该使用二进制搜索(实际上我很确定那是bisect在幕后使用的)....应该是最坏的情况O(log n) (假设您的输入数组已经排序)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 以开放间隔加入的有效方式(在其他df中找到“下一行”) - Efficient way to join on open interval (find “next” row in other df) 查找列表中重复序列索引的有效方法? - Efficient way to find the index of repeated sequence in a list? 在Python数组中查找对象索引的更有效方法 - More efficient way to find index of objects in Python array 从顶部(或任意索引)开始查找字典的*有序*交集的有效方法? - Efficient way to find *ordered* intersection of dictionaries starting at top (or an arbitrary index)? 在numpy数组中找到最大上三角条目索引的有效方法? - Efficient way to find the index of the max upper triangular entry in a numpy array? Python在部分更改的数组中查找最大值索引的最有效方法 - Python most efficient way to find index of maximum in partially changed array 有没有更有效(通用)的方法来在 Python 或 R 中为给定间隔上的函数找到 arg max? - is there a more efficient (general) way to find a arg max for a function on a given interval in Python or R? 获取索引值的有效方法 - Efficient way to grab an index value 在给定子列表的元素(Python)的情况下,查找列表中元素索引的最有效方法是什么 - What is the most efficient way to find the index of an element in a list, given only an element of a sublist (Python) Python:匹配2个不同长度数组并在较大数组中查找索引的有效方法 - Python: efficient way to match 2 different length arrays and find index in larger array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM