-1

I have a prediction in the format of np.argmax(model.predict(X),axis=2) which returns one element.How to predict top k elements using numpy

desertnaut
  • 46,107
  • 19
  • 109
  • 140
  • googling "find top k elements numpy array" gives [this SO thread](https://stackoverflow.com/questions/6910641/how-to-get-indices-of-n-maximum-values-in-a-numpy-array) as a first result, and [`numpy.argsort`](https://docs.scipy.org/doc/numpy-1.14.0/reference/generated/numpy.argsort.html) as second - http://idownvotedbecau.se/noresearch/ – desertnaut Jun 22 '18 at 16:13
  • Possible duplicate of [How to get indices of N maximum values in a numpy array?](https://stackoverflow.com/questions/6910641/how-to-get-indices-of-n-maximum-values-in-a-numpy-array) – desertnaut Jun 22 '18 at 16:13

1 Answers1

1

The link provided by @desertnaut covers the 1D case. It is, however, not entirely trivial to generalize the good answer to "ND along axis".

Here is an example where we find the top 2 along axis 1:

>>> a = np.random.randint(0, 9, (3, 5, 6))
>>> b = a.argpartition(-2, axis=1)[:, -2:]
>>> i, j, k = a.shape
>>> i, j, k = np.ogrid[:i, :j, :k]
>>> b = b[i, a[i, b, k].argsort(axis=1), k]
>>> a
array([[[8, 4, 1, 2, 4, 8],
        [0, 1, 3, 4, 2, 7],
        [4, 2, 7, 8, 1, 4],
        [1, 6, 2, 0, 3, 7],
        [1, 0, 0, 2, 8, 1]],

       [[1, 6, 3, 3, 0, 6],
        [7, 2, 0, 3, 8, 5],
        [5, 0, 1, 1, 7, 4],
        [2, 2, 4, 2, 6, 2],
        [5, 5, 7, 6, 8, 1]],

       [[4, 4, 4, 6, 2, 5],
        [2, 7, 8, 2, 6, 0],
        [5, 6, 7, 5, 1, 6],
        [6, 5, 3, 2, 2, 3],
        [5, 1, 8, 1, 6, 8]]])
>>> a[i, b, k]
array([[[4, 4, 3, 4, 4, 7],
        [8, 6, 7, 8, 8, 8]],

       [[5, 5, 4, 3, 8, 5],
        [7, 6, 7, 6, 8, 6]],

       [[5, 6, 8, 5, 6, 6],
        [6, 7, 8, 6, 6, 8]]])

A general function could look like

def argtopk(A, k, axis=0):
    tk = A.argpartition(-k, axis=axis)[(*axis*(slice(None),), slice(-k, None))]
    I = np.ogrid[(*map(slice, A.shape),)]
    I[axis] = tk
    I[axis] = A[I].argsort(axis=axis)
    return tk[I]
Paul Panzer
  • 47,318
  • 2
  • 37
  • 82
  • @desertnaut Then what do you suggest they had in mind? – Paul Panzer Jun 22 '18 at 21:54
  • Don't know - as I said, it's unclear! :) Let's wait for the feedback... – desertnaut Jun 22 '18 at 21:57
  • @paulpanzer i was expecting the same concept but the function argtopk is not working .please modify it accordingly – rachana_sharma003 Jun 23 '18 at 03:43
  • 1
    @rachana_sharma003 It is working perfectly for me. Please make sure you are on reasonably up-to-date python and numpy versions and that you provide proper inputs. The function takes a numpy array, an int and optinally another int. If after making a reasonable effort you still cannnot get it to work you may come back with a proper description of your setup, the inputs you tried and the error you received. – Paul Panzer Jun 23 '18 at 04:04