Reputation: 405
I would like to know how can I assign a matrix to a slice of a slice of a NumPy array. Following I have toy data and a 1D mask to illustrates what I need to do:
data = np.array([[1, 2, 11, 21], [2, 4, 12, 23], [3, 6, 13, 25], [4, 8, 14, 27], [5, 10, 15, 29]])
m = np.array([False, False, True, True, False])
Consequently, I have:
data[m] -> array([[ 3, 6, 13, 25], [ 4, 8, 14, 27]])
and
data[m][:, 2:] -> array([[13, 25], [14, 27]])
I would like to assign a matrix to data[m][:, 2:]. Something like:
data[m][:, 2:] = np.array([[2,2], [2,2]])
and end up with the data like:
np.array([[1, 2, 11, 21], [2, 4, 12, 23], [3, 6, 2, 2], [4, 8, 2, 2], [5, 10, 15, 29]])
My use case is for a huge dataset where I cannot go cell by cell assigning values. Also, I know I can duplicate the mask to the number of columns and then make every value in those columns, but the ones I want to assign, into False and use that final mask over the data, but I am searching for a better solution.
Upvotes: 1
Views: 609
Reputation: 231385
Because data[m]
uses boolean indexing (selecting rows) the result is a copy
, not a view
. The subsequent assignment modifies that, not data
. You need to combine the indexing into one.
I suggested using the indices of the True
values in m
:
In [205]: data[[2,3],2:]
Out[205]:
array([[13, 25],
[14, 27]])
In [208]: m.nonzero()
Out[208]: (array([2, 3]),)
In [209]: data[m.nonzero(),2:]
Out[209]:
array([[[13, 25],
[14, 27]]])
But m
can be used directly, since it is just selecting rows:
In [210]: data[m,2:]
Out[210]:
array([[13, 25],
[14, 27]])
It's a little trickier to use boolean indexing with others (list or slices), so that's why I started with the list [2,3]
.
Upvotes: 1