Cannot assign values to numpy array using 3D masking and indexing Cannot assign values to numpy array using 3D masking and indexing numpy numpy

Cannot assign values to numpy array using 3D masking and indexing


When you index an array with a boolean mask, the elements are extracted and placed into a 1-D array. This pretty much had to be the case, since the selected elements of the mask are not evenly space across our within any dimension. The expression results[mask] = value is equivalent to results.__setitem__(mask, value): clearly an in-place modification on result. However results[mask][index_keep] = value is equivalent to result.__getitem__(mask).__setitem__(index_keep, value). The in-place operation happens on a temporary array that is completely discarded.

The solution is to play with the index to get a single call to __setitem__ on the object you want. One way to do that is to apply index_keep to mask. You would first have to convert mask to linear indices, e.g. with np.flatnonzero:

result.ravel()[np.flatnonzero(mask)[index_keep]] = value

This will work as long as ravel returns a view, which it should in most cases. If result is a contiguous array, this will work all the time. It wont work if result is already a subset of a larger array.

This approach has the advantage that it uses only a single index array, and it works for any number of dimensions. Using np.where could be adapted to do the same, but would require more temporary storage. The disadvantage is of course that this approach is limited to contiguous arrays.

P.S. You almost certainly don't need to copy value. Its elements won't be modified, and the assignment will already make the copy into the appropriate locations of result. Making a copy just creates a needless temporary array that will be discarded immediately.


You can use numpy.where on your mask, which will allow you to get a view of your results array to index.

x, y, z = np.where(mask)results[x[index_keep], y[index_keep], z[index_keep]] = values