I'm struggling to select the specific columns per row of a NumPy matrix.
Suppose I have the following matrix which I would call X
:
[1, 2, 3]
[4, 5, 6]
[7, 8, 9]
I also have a list
of column indexes per every row which I would call Y
:
[1, 0, 2]
I need to get the values:
[2]
[4]
[9]
Instead of a list
with indexes Y
, I can also produce a matrix with the same shape as X
where every column is a bool
/ int
in the range 0-1 value, indicating whether this is the required column.
[0, 1, 0]
[1, 0, 0]
[0, 0, 1]
I know this can be done with iterating over the array and selecting the column values I need. However, this will be executed frequently on big arrays of data and that's why it has to run as fast as it can.
I was thus wondering if there is a better solution?