5.4.3Scientific Computing (Python)

Indexing and slicing — basic, boolean masking, fancy indexing

1,679 words8 min readdifficulty · medium5 backlinks

1. Basic slicing — start:stop:step

WHAT it returns: a view — a new array object that shares the same data buffer. Mutating the slice mutates the original.

WHY a view (HOW it works): A slice just changes the offset and strides of the recipe; no data is copied. That's why it's instant even for huge arrays.


2. Boolean masking — select by a condition

WHY a copy: the selected elements are scattered irregularly in memory; you can't describe them with a single stride, so NumPy must gather them into fresh contiguous memory.


3. Fancy (integer-array) indexing — select by position list

Figure — Indexing and slicing — basic, boolean masking, fancy indexing


Recall Feynman: explain to a 12-year-old

Imagine a long row of numbered lockers full of toys.

  • Slicing = "open lockers 2 through 8, skipping every other one." Fast, and you're looking at the real lockers — move a toy and it's really moved.
  • Boolean masking = you have a checklist: tick the lockers that hold a red toy, then copy just those toys into a basket.
  • Fancy indexing = you hand a list "lockers 5, 1, 1, 3 please" and get those toys in that order, even repeats. Masking and the list make a basket (copy); the range just peeks at the real lockers (view).

Flashcards

What three ways can you index a NumPy array?
Basic slicing (start:stop:step), boolean masking, fancy (integer-array) indexing.
Does basic slicing return a view or a copy?
A view — it shares the original data buffer (only offset/strides change).
Do boolean masking and fancy indexing return a view or a copy?
A copy (gathered into fresh memory).
Why must you use &, |, ~ instead of and, or, not in a mask?
They operate elementwise over the array; and/or need a single boolean and raise an ambiguity error.
Formula for the length of a[start:stop:step] (step>0)?
(stopstart)/step\lceil (stop-start)/step \rceil, clamped to ≥0.
What does A[[0,2],[1,3]] return for a 2-D A?
The pointwise pairs A[0,1] and A[2,3] — NOT a 2×2 block.
How do you select the actual block of rows {0,2} × cols {1,3}?
A[np.ix_([0,2],[1,3])].
What shape does a[idx] have when idx is a 2×2 integer array?
Same shape as idx (2×2); fancy indexing follows the index array's shape.
What does a boolean mask do to the dimensionality of a 2-D array?
Flattens the selection to 1-D.
How to safely edit the first row of A without changing A?
b = A[0].copy().

Connections

  • NumPy arrays — shape, strides, dtype (why views are cheap)
  • Broadcasting (masks/index arrays broadcast to array shape)
  • Views vs Copies — memory model
  • np.where and conditional selection
  • Vectorization — replacing Python loops
  • Pandas .loc / .iloc indexing (same ideas at DataFrame level)

Concept Map

described by recipe

way 1

way 2

way 3

returns

returns

returns

changes offset/strides only

gathers scattered elements

built from

element count

replaces

Array = data + shape/strides

Indexing: pick positions

Basic slicing start:stop:step

Boolean masking

Fancy indexing

View: shares memory

Copy: fresh memory

Condition with & | ~

n = ceil((stop-start)/step)

Vectorized selection

Hinglish (regional understanding)

Intuition Hinglish mein samjho

Dekho, NumPy array bas memory me numbers ki ek line hai, aur ek "recipe" (shape + strides) jo batati hai kaunsa element kahan rakha hai. Element(s) maangne ke teen tareeke hain. Pehla basic slicinga[start:stop:step], yaani ek range. Ye view deta hai, matlab original memory hi share karta hai — agar slice me kuch change karoge to original array bhi badal jayega. Isliye fast hai, copy nahi banati.

Doosra boolean masking — ek True/False array banao (jaise a > 0), aur a[mask] sirf wahi elements deta hai jahan True hai. Ye ek copy hai aur hamesha 1-D ho jaata hai. Yaad rakho conditions jodne ke liye &, |, ~ use karo, and/or nahi — kyunki yahan har element pe alag-alag operation chahiye, aur har condition ko bracket me daalna padta hai.

Teesra fancy indexing — seedha positions ki list do, a[[3,0,0,2]] — jo order maango wahi milega, repeat bhi chalega. Ye bhi copy hai aur result ka shape index array ke shape jaisa banta hai. Bada trap: A[[0,2],[1,3]] 2×2 block nahi deta — wo (0,1) aur (2,3) ko pair karke deta hai. Block chahiye to np.ix_ use karo.

Kyun important hai? Kyunki ye sab vectorized hai — Python ka slow for loop hatao, ek line me lakhon elements filter/reorder karo. 80/20 funda: bas "view vs copy" aur "fancy index pointwise zip hota hai" ye do cheezein pakad lo, baaki sab samajh aa jayega.

Go deeper — visual, from zero

Test yourself — Scientific Computing (Python)

Connections