#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2012
    Posts
    1
    Rep Power
    0

    Numpy array indices


    Hi every body,
    I have a 2D numpy-array. What is the simplest way to get the amount and indices of its maxima?
  2. #2
  3. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,974
    Rep Power
    510
    Code:
    coordinates_of_maximum = lambda A : divmod(A.flatten().argmax(),A.shape[-1])
    
    #example
    import numpy.random
    a=numpy.random.rand(7,11)
    
    print(coordinates_of_maximum(a))
    print(a)
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. Contributing User
    Devshed God 1st Plane (5500 - 5999 posts)

    Join Date
    Aug 2011
    Posts
    5,974
    Rep Power
    510
    Oh, you also wanted to know the value as well.

    max(a.flat)
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo