Python Is there a way to get the order of maximum values in a particular dimension?

Asked 2 years ago, Updated 2 years ago, 44 views

Noumpy or tf.Is there a way to get the order of maximum values in a particular dimension in the array?

Simply put, we're going to take the position of the maximum value of a particular row in a place like Pandas.

It's like using Softmax at the end of the Keras model.

I actually used Softmax.

[
    [ 0, 0, 0, 2, 9, 4, 5, 5, 5, 5 ], 
    [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
]

In this case, we return [4, 9].

For your information, the actual value is Softmax, so it's a mistake.

python numpy tensorflow

2022-09-20 20:13

2 Answers

There is argmax in numpy.

import numpy as np


def main():
    a = np.array([[0, 0, 0, 2, 9, 4, 5, 5, 5, 5], 
                  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]])

    amax = np.argmax(a, axis=1)
    print(amax)


if __name__ == "__main__":
    main()


2022-09-20 20:13

>>> a = [[ 0, 0, 0, 2, 9, 4, 5, 5, 5, 5 ], [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9]]
>>> list(map(lambda x: x.index(max(x)), a))
[4, 9]


2022-09-20 20:13

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.