在 Python 中查找最近的值并返回数组的索引 [英] Finding the nearest value and return the index of array in Python

查看:33
本文介绍了在 Python 中查找最近的值并返回数组的索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我找到了这篇文章:Python:在数组中查找元素

它是关于通过匹配值返回数组的索引.

and it's about returning the index of an array through matching the values.

另一方面,我想做的事情相似但不同.我想为目标值找到最接近的值.例如,我正在寻找 4.2,但我知道数组中没有 4.2,但我想返回值 4.1 而不是 4.4 的索引.

On the other hand, what I am thinking of doing is similar but different. I would like to find the nearest value for the target value. For example I am looking for 4.2 but I know in the array there is no 4.2 but I want to return the index of the value 4.1 instead of 4.4.

最快的方法是什么?

我正在考虑用旧的方式来做它,就像我以前用 Matlab 做的那样,它使用数组 A,我想从中获取索引减去目标值并取它的绝对值,然后选择分钟像这样:-

I am thinking of doing it the old way like how I used to do it with Matlab, which is using the array A where I want to get the index from to minus the target value and take the absolute of it, then select the min. Something like this:-

[~,idx] = min(abs(A - target))

那是 Matlab 代码,但我是 Python 新手,所以我在想,在 Python 中是否有快速的方法?

That is Matlab code but I am newbie in Python so I am thinking, is there a fast way of doing it in Python?

非常感谢您的帮助!

推荐答案

这与使用 bisect_left 类似,但它允许您传入一组目标

This is similar to using bisect_left, but it'll allow you to pass in an array of targets

def find_closest(A, target):
    #A must be sorted
    idx = A.searchsorted(target)
    idx = np.clip(idx, 1, len(A)-1)
    left = A[idx-1]
    right = A[idx]
    idx -= target - left < right - target
    return idx

一些解释:

首先是一般情况:idx = A.searchsorted(target) 为每个 target 返回一个索引,使得 target 之间>A[index - 1]A[index].我称它们为 leftright,所以我们知道 left <目标 <= 正确.目标 - 左 True(或 1)当目标更接近 leftFalse(或 0)时目标更近向右.

First the general case: idx = A.searchsorted(target) returns an index for each target such that target is between A[index - 1] and A[index]. I call these left and right so we know that left < target <= right. target - left < right - target is True (or 1) when target is closer to left and False (or 0) when target is closer to right.

现在特殊情况:当target小于A的所有元素时,idx = 0.idx = np.clip(idx, 1, len(A)-1) 替换 idx 的所有值1 加 1,所以 idx=1.在这种情况下 left = A[0], right = A[1] 我们知道 target <= left <= right.因此我们知道 target - left <= 0right - target >= 0 所以 target - left <= 0right - targetTrue 除非 target == left == rightidx - True = 0.

Now the special case: when target is less than all the elements of A, idx = 0. idx = np.clip(idx, 1, len(A)-1) replaces all values of idx < 1 with 1, so idx=1. In this case left = A[0], right = A[1] and we know that target <= left <= right. Therefor we know that target - left <= 0 and right - target >= 0 so target - left < right - target is True unless target == left == right and idx - True = 0.

还有一种特殊情况,如果target 大于A 的所有元素,在这种情况下idx = A.searchsorted(target)np.clip(idx, 1, len(A)-1)len(A) 替换为 len(A) - 1 所以 idx=len(A) -1目标 - 左 False 结束,所以 idx 返回 len(A) -1.我会让你自己处理逻辑.

There is another special case if target is greater than all the elements of A, In that case idx = A.searchsorted(target) and np.clip(idx, 1, len(A)-1) replaces len(A) with len(A) - 1 so idx=len(A) -1 and target - left < right - target ends up False so idx returns len(A) -1. I'll let you work though the logic on your own.

例如:

In [163]: A = np.arange(0, 20.)

In [164]: target = np.array([-2, 100., 2., 2.4, 2.5, 2.6])

In [165]: find_closest(A, target)
Out[165]: array([ 0, 19,  2,  2,  3,  3])

这篇关于在 Python 中查找最近的值并返回数组的索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆