-
-
Notifications
You must be signed in to change notification settings - Fork 10.9k
DOC,DISCUSS: A How-To guide for ndarray indexing #19586
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi Mukulikaa, |
Hi @AnjaTRPES, I would love to use your help! I'm curious to know if you referred to any specific resources or used special tricks to learn how to index arrays efficiently. I will make a draft PR outlining the doc soon (to make it easier to collaborate) and I'd appreciate your inputs for best practices there. |
Nice @Mukulikaa - it might be useful to ask in the mailing list as well and see if there are any extra suggestions there. |
I can instantly think of two different patterns that are hard to figure out on your own, and confusing when you see them for the first time, but fairly easy to understand and learn to use if you've seen them before (and an "indexing howto" might be a potential place for this!). The first pattern that can confound new users while being a fairly basic use case for broadcasting and fancy indexing is
(Note sure if this is included in your "Indexing with index arrays of shapes different from the ndarray" bullet point.) The solution being a range in the remaining leading dimension(s): arr = np.arange(3*4).reshape(3, 4)
column_indices = [[1, 3], [0, 2], [2, 2]]
print(arr[np.arange(arr.shape[0])[:,None], column_indices]) More generally you'd need an The second pattern is only a partial match for the "indexing" topic, because there are multiple ways to solve the problem. This is
I.e. if you have a = np.arange(20, 10, -1)
rng = np.random.default_rng()
inds = rng.permutation(a.size)
b = a[inds]
# solution 1: argsort the indices
inverse_inds = inds.argsort()
# solution 2: index into the left-hand side
inverse_inds2 = np.empty_like(inds)
inverse_inds2[inds] = np.arange(inds.size)
print(np.array_equal(inverse_inds, inverse_inds2)) # True
print(np.array_equal(a, b[inverse_inds])) # True |
@Mukulikaa your suggested list of topics in the OP seems like a great start for a how-to. Keep in mind that the document doesn't have to be completely comprehensive, so I would recommend not to worry about getting all of the bullets in the first version of the document. I think this will be a very nice addition to the docs! |
I believe there's nothing else to do here so closing. Thanks everyone! |
Hi, all! I'm planning to write a how-to doc for ndarray indexing as per discussion in #19407. I would love to know which use-cases you'd like to see in the doc. These are a few I have in mind (with inspiration from StackOverflow):
start
andstop
relative to each otherAs a new user of NumPy, I'm not sure if some of these cases are trivial or might be out of scope for a doc focus on indexing; I would appreciate any opinions regarding this.
cc: @melissawm @rossbar
The text was updated successfully, but these errors were encountered: