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

查看:188
本文介绍了查找最接近的值并在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返回一个索引,以使targetA[index - 1]A[index]之间.我将它们称为leftright,因此我们知道left < target <= right.当目标靠近left时,target - left < right - targetTrue(或1),当目标靠近rightFalse(或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 < right - 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) - 1替换len(A),因此idx=len(A) -1target - left < right - target结束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天全站免登陆