I have the given dataset:
data = np.array([
[1, 2, 1, 3, 1, 2, 1],
[3, 4, 1, 5, 2, 7, 2],
[2, 1, 2, 1, 1, 4, 5],
[6, 1, 2 ,3, 1, 3, 1]])
cols_idx = np.array([0, 0, 1, 0, 1, 0, 0])
I want to return columns from data where cols_idx == 1. For that I used:
data[:, np.nonzero(cols_idx)]
But it returns a 3D instead a 2D array:
data[:, np.nonzero(cols_idx)]
array([[[1, 1]],
[[1, 2]],
[[2, 1]],
[[2, 1]]])
data[:, np.nonzero(cols_idx)].shape
(4, 1, 2)
I would like the output to be:
data[:, np.nonzero(cols_idx)]
array([[1, 1],
[1, 2],
[2, 1],
[2, 1]])
data[:, np.nonzero(cols_idx)].shape
(4, 2)
How can I achieve that?
np.nonzero(cols_idx)by itself.np.flatnonzero()that fix the "issue"flatnonzeroextracts the array from thenonzerotuple, with a[0]indexing. Look at its code! You want to index with the array nonzero produces, not the tuple it is wrapped in. That tuple if great for indexing by itself.