xirururu - 1 month ago 4x

Python Question

I have an array and I want to find the indices of the maximum values.

For example:

`myarray = np.array([1,8,8,3,2])`

I want to get the result:

`[1,2]`

(Actually I tried

`np.argmax(myarray)`

`[1]`

Answer

Given:

```
>>> myarray = np.array([1,8,8,3,2])
```

You can do:

```
>>> np.where(myarray==myarray[np.argmax(myarray)])
(array([1, 2]),)
```

or,

```
>>> np.where(myarray==max(myarray))
(array([1, 2]),)
```

or,

```
>>> np.nonzero(myarray==max(myarray))
(array([1, 2]),)
```

Source (Stackoverflow)

Comments