4

我有一个任意形状的numpy数组,例如:

a = array([[[ 1,  2],
            [ 3,  4],
            [ 8,  6]],

          [[ 7,  8],
           [ 9,  8],
           [ 3, 12]]])
a.shape = (2, 3, 2)

以及最后一个轴上 argmax 的结果:

np.argmax(a, axis=-1) = array([[1, 1, 0],
                               [1, 0, 1]])

我想得到最大值:

np.max(a, axis=-1) = array([[ 2,  4,  8],
                            [ 8,  9, 12]])

但无需重新计算一切。我试过了:

a[np.arange(len(a)), np.argmax(a, axis=-1)]

但得到:

IndexError: shape mismatch: indexing arrays could not be broadcast together with shapes (2,) (2,3) 

怎么做?二维的类似问题:numpy 2d array max/argmax

4

3 回答 3

9

你可以使用advanced indexing-

In [17]: a
Out[17]: 
array([[[ 1,  2],
        [ 3,  4],
        [ 8,  6]],

       [[ 7,  8],
        [ 9,  8],
        [ 3, 12]]])

In [18]: idx = a.argmax(axis=-1)

In [19]: m,n = a.shape[:2]

In [20]: a[np.arange(m)[:,None],np.arange(n),idx]
Out[20]: 
array([[ 2,  4,  8],
       [ 8,  9, 12]])

对于任意维数的通用 ndarray 情况,如 中所述comments by @hpaulj,我们可以使用np.ix_,如下所示 -

shp = np.array(a.shape)
dim_idx = list(np.ix_(*[np.arange(i) for i in shp[:-1]]))
dim_idx.append(idx)
out = a[dim_idx]
于 2016-11-01T09:56:44.020 回答
0

对于具有任意形状的 ndarray,您可以展平 argmax 索引,然后恢复正确的形状,如下所示:

idx = np.argmax(a, axis=-1)
flat_idx = np.arange(a.size, step=a.shape[-1]) + idx.ravel()
maximum = a.ravel()[flat_idx].reshape(*a.shape[:-1])
于 2019-07-13T17:23:33.620 回答
0

对于任意形状的数组,以下应该有效:)

a = np.arange(5 * 4 * 3).reshape((5,4,3))

# for last axis
argmax = a.argmax(axis=-1)
a[tuple(np.indices(a.shape[:-1])) + (argmax,)]

# for other axis (eg. axis=1)
argmax = a.argmax(axis=1)
idx = list(np.indices(a.shape[:1]+a.shape[2:]))
idx[1:1] = [argmax]
a[tuple(idx)]

或者

a = np.arange(5 * 4 * 3).reshape((5,4,3))

argmax = a.argmax(axis=0)
np.choose(argmax, np.moveaxis(a, 0, 0))

argmax = a.argmax(axis=1)
np.choose(argmax, np.moveaxis(a, 1, 0))

argmax = a.argmax(axis=2)
np.choose(argmax, np.moveaxis(a, 2, 0))

argmax = a.argmax(axis=-1)
np.choose(argmax, np.moveaxis(a, -1, 0))
于 2020-04-26T02:40:02.930 回答