Skip to content
Advertisement

Transform a 3D numpy array to 1D based on column value

Maybe this is a very simple task, but I have a numpy.ndarray with shape (1988,3).

JavaScript

I want to create a 1D array with shape=(1988,) that will have values corresponding to the column of my 3D array that has a value of 1.

For example,

JavaScript

How can I do this?

Advertisement

Answer

You can use numpy.nonzero:

JavaScript

Output: array([0, 1, 2, 1, 0, 2])

handling rows with no match:

JavaScript

Output: array([ 0., 1., nan, 1., 0., 2.])

User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement