I have an operation that I usually do, which I call a “jagged cut” because I don’t know the real name. This is best explained with an example:
a = np.random.randn(50, 10)
entries_of_interest = np.random.randint(10, size = 50)
jagged_slice_of_a = a[np.arange(a.shape[0]), entries_of_interest]
The only problem is it’s a little cumbersome to do for indexing a[np.arange(a.shape[0]), entries_of_interest](it seems silly to build "np.arange (a.shape [0])" just for that). I would like something like an operator :for this, but :do something else. Is there a shorter way to do this operation?
The best answer:
No, there is no better way with native numpy. You can create a helper function for this if you want.
Peter source
share