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 ...].

konstantin
  • 853
  • 4
  • 16
  • 50
  • 2
    Maybe you could post an example of input and include desired output? – zipa May 23 '18 at 08: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 ...]. – konstantin May 23 '18 at 08:42
  • You should definitely add that example to your question. You can take a look on how to provide [mcve]. – zipa May 23 '18 at 08:43

1 Answers1

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]
Austin
  • 25,759
  • 4
  • 25
  • 48