Fancy indexing selects array elements by supplying an index collection for each dimension. NumPy returns the selected values as a copy rather than a view. section 4.1.2
For multidimensional NumPy arrays:
- Pair row and column index lists to select corresponding positions element by element. section 4.1.2
- Expect selected results to allocate separate storage; np.shares_memory returns False for the source and result. section 4.1.2
- Prefer views when selection can be expressed through slicing or compatible strides, because views can be more efficient than copies. section 4.1.2
Alternating matrix positions
Given a 2 × 5 matrix, row indices [0, 1, 0, 1, 0] and column indices [0, 1, 2, 3, 4] select [0, 6, 2, 8, 4]. The result does not share memory with the matrix. section 4.1.2
import numpy as np m5x2 = np.arange(10).reshape(2, 5) my_rows = [0, 1, 0, 1, 0] my_cols = [0, 1, 2, 3, 4] alternate = m5x2[my_rows, my_cols] print(m5x2) print(alternate) print(np.shares_memory(m5x2, alternate))
Run the complete NumPy example to inspect paired indexing, the selected values, and memory independence. section 4.1.2

