D1X D1X - 29 days ago 14
Python Question

Using numpy.nanargmin() in 2 dimensional matrix

I am trying to obtain the argmin of a numpy 2 dimensional array

A
which has
nan
values. Now the problem is:


  1. numpy.nanargmin(A)
    returns only one index.

  2. numpy.unravel_index(A.argmin(), A.shape)
    returns
    [0,0]
    because it has
    nan
    values.

    And...

  3. numpy.unravel_index(A.nanargmin(), A.shape)
    throws the error:


    AttributeError Traceback (most recent call
    last) in ()
    ----> 1 np.unravel_index(dist.nanargmin(), dist.shape) AttributeError: 'numpy.ndarray' object has no attribute 'nanargmin'


Answer

NumPy arrays only have the argmin() attribute, but no nanargmin() attribute. So A.nanargmin() does not exist.

You can use numpy.argmin(A) and numpy.nanargmin(A) instead.

Comments