Question
Please help understand the design decision or rational of the Numpy indexing with tuple (i, j) into a ndarray.
Background
When the index is a single tuple (4, 2), then (i=row, j=column).
shape = (6, 7)
X = np.zeros(shape, dtype=int)
X[(4, 2)] = 1
X[(5, 3)] = 1
print("X is :\n{}\n".format(X))
---
X is :
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 1 0 0 0 0] <--- (4, 2)
[0 0 0 1 0 0 0]] <--- (5, 3)
However, when the indices are multiple tuples (4, 2), (5, 3), then (i=row, j=row) for (4, 2) and (i=column, j=column) for (5, 3).
shape = (6, 7)
Y = np.zeros(shape, dtype=int)
Y[(4, 2), (5, 3)] = 1
print("Y is :\n{}\n".format(Y))
---
Y is :
[[0 0 0 0 0 0 0]
[0 0 0 0 0 0 0]
[0 0 0 1 0 0 0] <--- (2, 3)
[0 0 0 0 0 0 0]
[0 0 0 0 0 1 0] <--- (4, 5)
[0 0 0 0 0 0 0]]
It means you are constructing a 2d array R, such that
R=A[B, C]
. This means that the value for rij=abijcij.So it means that the item located at
R[0,0]
is the item inA
with as row indexB[0,0]
and as column indexC[0,0]
. The itemR[0,1]
is the item inA
with row indexB[0,1]
and as column indexC[0,1]
, etc.
multi_index: A tuple of integer arrays, one array for each dimension.
Why not always (i=row, j=column)? What will happen if it is always (i=row, j=column)?
Updated
With the answers from Akshay and @DaniMesejo, understood:
X[
(4), # dimension 2 indices with only 1 element
(2) # dimension 1 indices with only 1 element
] = 1
Y[
(4, 2, ...), # dimension 2 indices
(5, 3, ...) # dimension 1 indices (dimension 0 is e.g. np.array(3) whose shape is (), in my understanding)
] = 1