numpy数组的对分权

numpy数组的对分权,numpy,Numpy,我希望通过排序的numpy数组(其中查询值不在数组中)找到与查询最接近的较高值的索引 类似于python标准库中的bisect\u right,无需将numpy数组转换为python列表,并利用数组已排序的事实(即运行时应为O(log N),如numpy的searchsorted) 熊猫在使用“bfill”选项时使用了get_loc,但将其作为依赖项包含在其中似乎有点过分了。。。我可能不得不将这个数组作为python列表和numpy数组,但我想知道是否有更合理的解决方案 编辑:看来searchs

我希望通过排序的numpy数组(其中查询值不在数组中)找到与查询最接近的较高值的索引

类似于python标准库中的
bisect\u right
,无需将numpy数组转换为python列表,并利用数组已排序的事实(即运行时应为O(log N),如numpy的
searchsorted

熊猫在使用“bfill”选项时使用了
get_loc
,但将其作为依赖项包含在其中似乎有点过分了。。。我可能不得不将这个数组作为python列表和numpy数组,但我想知道是否有更合理的解决方案


编辑:看来
searchsorted
正是我所需要的。

我们可以在github上看到
bisect\u right
的代码:

def bisect_right(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.
    The return value i is such that all e in a[:i] have e <= x, and all e in
    a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
    insert just after the rightmost x already there.
    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        # Use __lt__ to match the logic in list.sort() and in heapq
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo
要查找与给定数字最接近的较高值的索引,请执行以下操作:

def closest_higher_value(array, value):
    if bisect_right(array, value) < len(array):
        return bisect_right(array, value)
    print("value too large:", value, "is bigger than all elements of:")
    print(array)


print(closest_higher_value(array, 3))
>>> 3
print(closest_higher_value(array, 7))
>>> value too large: 7 is bigger than all elements of:
>>> [1 2 3 4 5 6]
>>> None
def最近值(数组,值):
如果对分右(数组,值)>> 3
打印(最接近的\u较高的\u值(数组,7))
>>>值太大:7大于以下所有元素:
>>> [1 2 3 4 5 6]
>>>没有

我们可以在github上看到右对分的代码:

def bisect_right(a, x, lo=0, hi=None):
    """Return the index where to insert item x in list a, assuming a is sorted.
    The return value i is such that all e in a[:i] have e <= x, and all e in
    a[i:] have e > x.  So if x already appears in the list, a.insert(x) will
    insert just after the rightmost x already there.
    Optional args lo (default 0) and hi (default len(a)) bound the
    slice of a to be searched.
    """

    if lo < 0:
        raise ValueError('lo must be non-negative')
    if hi is None:
        hi = len(a)
    while lo < hi:
        mid = (lo+hi)//2
        # Use __lt__ to match the logic in list.sort() and in heapq
        if x < a[mid]: hi = mid
        else: lo = mid+1
    return lo
要查找与给定数字最接近的较高值的索引,请执行以下操作:

def closest_higher_value(array, value):
    if bisect_right(array, value) < len(array):
        return bisect_right(array, value)
    print("value too large:", value, "is bigger than all elements of:")
    print(array)


print(closest_higher_value(array, 3))
>>> 3
print(closest_higher_value(array, 7))
>>> value too large: 7 is bigger than all elements of:
>>> [1 2 3 4 5 6]
>>> None
def最近值(数组,值):
如果对分右(数组,值)>> 3
打印(最接近的\u较高的\u值(数组,7))
>>>值太大:7大于以下所有元素:
>>> [1 2 3 4 5 6]
>>>没有

我不确定问题出在哪里。您有一个已排序的NumPy数组,而searchsorted对您不起作用?@Divakar searchsorted将根据数组中不存在的值的边参数返回0或len(a)-我想获取最接近的较高值的索引。最近的和较高的?不要认为你两个都能做到。如果您正在寻找最近的,这应该是相关的-。你能给出一个最小的示例案例,其中searchsorted对你的案例不起作用吗?@Divakar谢谢,我从numpy的文档中了解到,如果一个值不存在,它会返回0或len(a),而实际上,searchsorted正是我所需要的。我不确定问题是什么。您有一个已排序的NumPy数组,而searchsorted对您不起作用?@Divakar searchsorted将根据数组中不存在的值的边参数返回0或len(a)-我想获取最接近的较高值的索引。最近的和较高的?不要认为你两个都能做到。如果您正在寻找最近的,这应该是相关的-。你能给出一个最小的示例案例,其中searchsorted对你的案例不起作用吗?@Divakar谢谢,我从numpy的文档中了解到,如果一个值不存在,它会返回0或len(a),而实际上,searchsorted正是我所需要的。谢谢,这很有效,但正如Divakar在对我的问题的评论中所指出的,numpy的searchsorted正是这样做的,所以,毕竟不需要对分。如果您的答案中包含searchsorted,我会将其标记为选中。谢谢,这是可行的,但正如Divakar在对我的问题的评论中所指出的,numpy的searchsorted正是这样做的,因此,根本不需要对分。如果你的答案中包含搜索排序,我会将其标记为选中。