条件nd argmin:如何找到多维数组子集的min的坐标?

时间:2017-02-19 22:34:14

标签: numpy

我知道我可以使用argminunravel_index来查找ndarray中最小值的索引,但是如果我想找到最小的非零元素,或者找不到的最小元素,该怎么办?喃?

2 个答案:

答案 0 :(得分:2)

这是一种使用扁平指数的方法 -

def flatnonzero_based(a,condition): # condition = a!= or ~np.isnan(a)
    idx = np.flatnonzero(condition)
    return np.unravel_index(idx[np.take(a, idx).argmin()], a.shape)

<强>基准

方法 -

def flatnonzero_based(a,condition): # Proposed soln
    idx = np.flatnonzero(condition)
    return np.unravel_index(idx[np.take(a, idx).argmin()], a.shape)

def where_based(a, condition):  # @Paul Panzer's soln
    nz = np.where(condition)
    return np.array(nz)[:, np.argmin(a[nz])]

计时和验证 -

In [233]: a = np.random.rand(40,50,30)

In [234]: nan_idx = np.random.choice(range(a.size), size = a.size//100, replace=0)

In [235]: a.ravel()[nan_idx] = np.nan

In [236]: condition = ~np.isnan(a)

In [237]: where_based(a, condition)
Out[237]: array([16, 10,  8])

In [238]: flatnonzero_based(a, condition)
Out[238]: (16, 10, 8)

In [239]: %timeit where_based(a, condition)
1000 loops, best of 3: 877 µs per loop

In [240]: %timeit flatnonzero_based(a, condition)
10000 loops, best of 3: 143 µs per loop

使用4D数据 -

In [255]: a = np.random.rand(40,50,30,30)

In [256]: nan_idx = np.random.choice(range(a.size), size = a.size//100, replace=0)

In [257]: a.ravel()[nan_idx] = np.nan

In [258]: condition = ~np.isnan(a)

In [259]: where_based(a, condition)
Out[259]: array([34, 14,  5, 10])

In [260]: flatnonzero_based(a, condition)
Out[260]: (34, 14, 5, 10)

In [261]: %timeit where_based(a, condition)
10 loops, best of 3: 64.9 ms per loop

In [262]: %timeit flatnonzero_based(a, condition)
100 loops, best of 3: 5.32 ms per loop

合并@user7138814's suggestion -

In [267]: np.unravel_index(np.nanargmin(a), a.shape)
Out[267]: (34, 14, 5, 10)

In [268]: %timeit np.unravel_index(np.nanargmin(a), a.shape)
100 loops, best of 3: 4.54 ms per loop

答案 1 :(得分:1)

这应该有效(条件是数据!= 0或~np.isnan(数据))

nz = np.where(condition)
cond_arg_min = np.array(nz)[:, np.argmin(data[nz])]