0

I want to print out the index of an array that has the maximum value (and since indexing begins at 0, I need to add one to the index value to get 1-indexed). Example:

rslt = np.amax(final_array)
print("The maximum value is :", rslt)
print("The optimal choice that has that value is :", rslt.index[])

Context: I am writing some multi-criteria decision analysis code in Python. I import numpy to handle arrays of alternatives, criteria and weights. I use np.amax to find the maximum value in the final array.

smci
  • 32,567
  • 20
  • 113
  • 146
  • what do you mean by "optimal choice" ? can you define that – pyeR_biz Jun 12 '19 at 01:23
  • Is that index guaranteed to be unique? could there be multiple such indices? – smci Jun 12 '19 at 02:56
  • Near-duplicate: [Find row where values for column is maximal in a pandas DataFrame](https://stackoverflow.com/questions/10202570/find-row-where-values-for-column-is-maximal-in-a-pandas-dataframe), [How to make numpy.argmax return all occurrences of the maximum?](https://stackoverflow.com/questions/17568612/how-to-make-numpy-argmax-return-all-occurrences-of-the-maximum), [Get the position of the biggest item in a multi-dimensional numpy array](https://stackoverflow.com/questions/3584243/get-the-position-of-the-biggest-item-in-a-multi-dimensional-numpy-array) ... – smci Jun 12 '19 at 03:03

2 Answers2

1

use numpy.argmax to find the index of the max value.

DSC
  • 1,153
  • 7
  • 21
0
import numpy as np

#some list
f = [1,2,3,4,5,6,6,6,6,6]

#max value
print (f"the max value is : { np.amax(f)}")

#indices where max values are located
max_indices = np.argwhere( f == np.amax(f))

#adding 1 to get position
max_positions = [i+1 for i in max_indices.flatten().tolist()]

print(f"Max values are located at : {max_positions}")

#first max value
print(f"First max value occurs at : {max_positions[0]}")
pyeR_biz
  • 986
  • 12
  • 36