根据另一个参考数组从一个数组中选择紧密匹配

Phy*_*ist 2 python arrays matlab numpy similarity

我有一个数组A和一个参考数组B.尺寸A至少和B.一样大.例如

A = [2,100,300,793,1300,1500,1810,2400]
B = [4,305,789,1234,1890]
Run Code Online (Sandbox Code Playgroud)

B实际上是指定时间信号中峰值的位置,并且A包含稍后峰值的位置.但是某些元素A实际上是不是我想要的(可能是由于噪声等)的山峰,我想找到"真正的"一个A基础B.在"真实的"元件A应接近那些B,和在上面给出的例子中,在"真实"的人AA'=[2,300,793,1300,1810].在这个例子中应该很明显,这100,1500,2400不是我们想要的,因为它们与B中的任何元素相距甚远.如何在python/matlab中以最有效/准确的方式编写代码?

Div*_*kar 6

方法#1:使用NumPy broadcasting,我们可以在输入数组之间寻找绝对的逐元素减法,并使用适当的阈值来滤除不需要的元素A.对于给定的样本输入,似乎是90工作的阈值.

因此,我们会有一个实现,就像这样 -

thresh = 90
Aout = A[(np.abs(A[:,None] - B) < thresh).any(1)]
Run Code Online (Sandbox Code Playgroud)

样品运行 -

In [69]: A
Out[69]: array([   2,  100,  300,  793, 1300, 1500, 1810, 2400])

In [70]: B
Out[70]: array([   4,  305,  789, 1234, 1890])

In [71]: A[(np.abs(A[:,None] - B) < 90).any(1)]
Out[71]: array([   2,  300,  793, 1300, 1810])
Run Code Online (Sandbox Code Playgroud)

方法#2:基于this post,这是一种使用内存的高效方法np.searchsorted,这对大型阵列至关重要 -

def searchsorted_filter(a, b, thresh):
    choices = np.sort(b) # if b is already sorted, skip it
    lidx = np.searchsorted(choices, a, 'left').clip(max=choices.size-1)
    ridx = (np.searchsorted(choices, a, 'right')-1).clip(min=0)
    cl = np.take(choices,lidx) # Or choices[lidx]
    cr = np.take(choices,ridx) # Or choices[ridx]
    return a[np.minimum(np.abs(a - cl), np.abs(a - cr)) < thresh]
Run Code Online (Sandbox Code Playgroud)

样品运行 -

In [95]: searchsorted_filter(A,B, thresh = 90)
Out[95]: array([   2,  300,  793, 1300, 1810])
Run Code Online (Sandbox Code Playgroud)

运行时测试

In [104]: A = np.sort(np.random.randint(0,100000,(1000)))

In [105]: B = np.sort(np.random.randint(0,100000,(400)))

In [106]: out1 = A[(np.abs(A[:,None] - B) < 10).any(1)]

In [107]: out2 = searchsorted_filter(A,B, thresh = 10)

In [108]: np.allclose(out1, out2)  # Verify results
Out[108]: True

In [109]: %timeit A[(np.abs(A[:,None] - B) < 10).any(1)]
100 loops, best of 3: 2.74 ms per loop

In [110]: %timeit searchsorted_filter(A,B, thresh = 10)
10000 loops, best of 3: 85.3 µs per loop
Run Code Online (Sandbox Code Playgroud)

2018年1月更新,进一步提升性能

我们可以np.searchsorted(..., 'right')通过利用从中获得的指数np.searchsorted(..., 'left')以及absolute计算来避免第二次使用,例如 -

def searchsorted_filter_v2(a, b, thresh):
    N = len(b)

    choices = np.sort(b) # if b is already sorted, skip it

    l = np.searchsorted(choices, a, 'left')
    l_invalid_mask = l==N
    l[l_invalid_mask] = N-1
    left_offset = choices[l]-a
    left_offset[l_invalid_mask] *= -1    

    r = (l - (left_offset!=0))
    r_invalid_mask = r<0
    r[r_invalid_mask] = 0
    r += l_invalid_mask
    right_offset = a-choices[r]
    right_offset[r_invalid_mask] *= -1

    out = a[(left_offset < thresh) | (right_offset < thresh)]
    return out
Run Code Online (Sandbox Code Playgroud)

更新时间以测试进一步的加速 -

In [388]: np.random.seed(0)
     ...: A = np.random.randint(0,1000000,(100000))
     ...: B = np.unique(np.random.randint(0,1000000,(40000)))
     ...: np.random.shuffle(B)
     ...: thresh = 10
     ...: 
     ...: out1 = searchsorted_filter(A, B, thresh)
     ...: out2 = searchsorted_filter_v2(A, B, thresh)
     ...: print np.allclose(out1, out2)
True

In [389]: %timeit searchsorted_filter(A, B, thresh)
10 loops, best of 3: 24.2 ms per loop

In [390]: %timeit searchsorted_filter_v2(A, B, thresh)
100 loops, best of 3: 13.9 ms per loop
Run Code Online (Sandbox Code Playgroud)

深层发掘 -

In [396]: a = A; b = B

In [397]: N = len(b)
     ...: 
     ...: choices = np.sort(b) # if b is already sorted, skip it
     ...: 
     ...: l = np.searchsorted(choices, a, 'left')

In [398]: %timeit np.sort(B)
100 loops, best of 3: 2 ms per loop

In [399]: %timeit np.searchsorted(choices, a, 'left')
100 loops, best of 3: 10.3 ms per loop
Run Code Online (Sandbox Code Playgroud)

似乎searchsorted并且sort几乎占用了所有运行时,它们似乎对此方法至关重要.所以,看起来似乎没有任何进一步坚持这种基于排序的方法.