Reputation: 14399
An answer I gave earlier raised a question for me: Is it possible to reference a view or slice of a numpy array without repeating a bunch of brackets?
For instance, in the answer I used s=np.argsort(u)
and then did all my calculations on a 'virtually' sorted u[s]
. I've had situations where I then needed a boolean mask of that array, giving something akin to u[s][mask]
. For bigger data I might have a mask of a mask of a mask . . . until things start to look like the end of an episode of Scooby Doo.
But if I assign that array to a variable b=a[s][mask]
and change b
, a
does not change, so I end up carrying a pile of brackets through my calculations. I've tried various arrangements of uv=u.view()[s]
but it seems .view()
only makes a view of the whole array. Is there another method I am missing?
Upvotes: 4
Views: 943
Reputation: 249462
You may not be able to solve the simple case of u[s]
but in more complex cases like u[s][mask]
you can:
t = s[mask]
u[t] # same as u[s][mask]
That is, you can simplify your mask to a single variable, but you may not be able to get rid of it completely, unless perhaps you want to write your own wrapper class with __getitem__
and __setitem__
.
Upvotes: 2