4

Simple question, I want to get a 1D numpy array.

Given a 2D array where each row contains a single '1' value, how can it be converted to a 1D array, consisting of the column index of the '1' in the 2D array

[[ 0.  0.  1.]
 [ 0.  0.  1.]
 [ 0.  0.  1.]
 [ 0.  1.  0.]
 [ 0.  1.  0.]
 [ 0.  1.  0.]
 [ 0.  1.  0.]
 [ 1.  0.  0.]]

to

[2 2 1 1 1 1 0]

How would I do it in python? I don't know the terminology for it, do let me know the proper terms for such transformation.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
5Volts
  • 179
  • 3
  • 13

3 Answers3

6

You are looking for the index with the maximum value along the first axis:

>>> a.argmax(axis=1)
array([2, 2, 2, 1, 1, 1, 1, 0])

a.argmax(axis=None, out=None)

Return indices of the maximum values along the given axis.

If the other values are not necessarily less than one, filter for 1 first. This gives an array of True and False values. Now, use argmax():

>>> (a == 1).argmax(axis=1)
array([2, 2, 2, 1, 1, 1, 1, 0])

True acts like a 1 and False like a 0, because bool inherits from int.

Mike Müller
  • 82,630
  • 20
  • 166
  • 161
0

Seems like you want to get the hot index of a one hot encoded vector for each row.

This could be done as follows:

  1. Multiply your matrix with a vector [0 1 2], or more generic: numpy.arange(matrix.shape[1])
  2. sum up your matrix rows: numpy.sum(matrix, axis=1)

numpy arrays also have the function argmax, which leads to the same result.

wolff
  • 426
  • 4
  • 11
0

You can do it using numpy argmax as the following:

a=np.array([[ 0,  0,  1],[ 0,  0,  1], [ 0,  0,  1],[ 0,  1,  0],[ 0,  1,  0],[ 0,  1,  0],[ 1,0,0]])
a.argmax(1)
array([2, 2, 2, 1, 1, 1, 0])
Walid Da.
  • 948
  • 1
  • 7
  • 15