Getting a numpy array view with integer or boolean indexing
Question: is there any simple/clean way to get a writeable array view based on an integer-indexed or boolean-indexed index subset?
No.
NumPy arrays (and views) are required to have constant strides (i.e., the distance between elements in memory has to be constant). If your indexing operation would create an object that violates this limitation, you are out of luck.
See e.g. here for a discussion of a related problem:
You cannot in the numpy memory model. The numpy memory model defines an array as something that has regular strides to jump from an element to the next one.
A nice explanation to your question here:
You can create views by selecting a slice of the original array, or also by changing the dtype (or a combination of both). The rule of thumb for creating a slice view is that the viewed elements can be addressed with offsets, strides, and counts in the original array. (...)
The reason why a fancy indexing is not returning a view is that, in general, it cannot be expressed as a slice (in the sense stated above of being able to be addressed with offsets, strides, and counts).
For example, fancy indexing for could have been expressed by , but it is not possible to do the same for by means of a slice. So, this is why an object with a copy of the original data is returned instead.
So as a general rule, no, you can't.
In my opinion the most "numpy" way of working with views is by working with masks, and keeping track of these instead of assigning the views to a new variable. I'd simply do:
m = [2, 4]
x[m] = some_function(x[m]) # whatever you need to do