0

I have a numpy vector of size (N,) which contains integers betwen 1-5 and by using the keras function to_categorical I am constructing the corresponing binary matrix that have size (Nx5). For example if the first value of the vector is 1 then the first row of the array it is (1, 0, 0, 0, 0). How can I do the opposite? By having an array of values (could also double values) to return the index with the highest value? Is there any command that can do that automatically?

As an example my input could be an 2D array with doubles for example one row could be [[0.025, 0.022, 0.58, 0.011, 0.22 ]....] and the result to be for that row [3 ...].

3
  • 2
    Maybe you could post an example of input and include desired output? Commented May 23, 2018 at 8:38
  • My input is a 2D array with doubles for examle one row could be [[0.025, 0.022, 0.58, 0.011, 0.22 ]....] and the result to be for that row [3 ...]. Commented May 23, 2018 at 8:42
  • You should definitely add that example to your question. You can take a look on how to provide minimal reproducible example. Commented May 23, 2018 at 8:43

1 Answer 1

1

If a is your array, use:

a.argmax(axis=1)

This returns the index of maximum value of each row of the array.

Demo:

>>> a = np.array([[5,2,3],[1,3,1]])
>>> a.argmax(axis=1)
[0 1]
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.