numpy : argmin in multidimensional arrays

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • TG

    #1

    numpy : argmin in multidimensional arrays

    Hi there.

    I am working with multi-dimensional arrays and I need to get
    coordinates of the min value in it.

    using myarray.argmin( ) returns the index in the flatten array, which is
    a first step, but I wonder if it is possible to get the coordinates
    directly as an array, rather than calculating them myself by using this
    flat index and the shape of the array.

    well, in fact i'm not sure to understand how argmin(myarray) works,
    when myarray is multidimensiona l.

    Thanks

  • Travis E. Oliphant

    #2
    Re: numpy : argmin in multidimensiona l arrays

    TG wrote:
    Hi there.
    >
    I am working with multi-dimensional arrays and I need to get
    coordinates of the min value in it.
    >
    using myarray.argmin( ) returns the index in the flatten array, which is
    a first step, but I wonder if it is possible to get the coordinates
    directly as an array, rather than calculating them myself by using this
    flat index and the shape of the array.
    >
    well, in fact i'm not sure to understand how argmin(myarray) works,
    when myarray is multidimensiona l.

    By default, the argmin method flattens the array and returns the flat
    index. You can get the corresponding element using

    myarray.flat[index]

    Alternatively, you can use the function unravel_index

    unravel_index(f lat_index, myarray.shape)

    to return an N-dimensional index.


    If you give an axis argument, then the minimum is found along the
    specified dimension and you get an N-1 dimensional array of indices that
    will all be between 1 and myarray.shape[axis]


    -Travis





    Comment

    • TG

      #3
      Re: numpy : argmin in multidimensiona l arrays

      thanks. unravel_index do the trick.

      Travis E. Oliphant wrote:
      TG wrote:
      Hi there.

      I am working with multi-dimensional arrays and I need to get
      coordinates of the min value in it.

      using myarray.argmin( ) returns the index in the flatten array, which is
      a first step, but I wonder if it is possible to get the coordinates
      directly as an array, rather than calculating them myself by using this
      flat index and the shape of the array.

      well, in fact i'm not sure to understand how argmin(myarray) works,
      when myarray is multidimensiona l.
      >
      >
      By default, the argmin method flattens the array and returns the flat
      index. You can get the corresponding element using
      >
      myarray.flat[index]
      >
      Alternatively, you can use the function unravel_index
      >
      unravel_index(f lat_index, myarray.shape)
      >
      to return an N-dimensional index.
      >
      >
      If you give an axis argument, then the minimum is found along the
      specified dimension and you get an N-1 dimensional array of indices that
      will all be between 1 and myarray.shape[axis]
      >
      >
      -Travis

      Comment

      Working...