5.4.1 · HinglishScientific Computing (Python)

NumPy — ndarray structure, dtype, shape, strides

1,698 words8 min readRead in English

5.4.1 · Coding › Scientific Computing (Python)


ndarray ASLIYAT mein kya hai?

YE DESIGN KYU? Kyunki ek 2-D table ko real 2-D structure (list of lists / pointers) mein store karna slow aur memory-scattered hota hai. Iske bajaye NumPy sab kuch EK cache-friendly block mein rakhta hai aur dimensions ko fake karne ke liye math (strides) use karta hai. Yahi NumPy ko fast banata hai.

Figure — NumPy — ndarray structure, dtype, shape, strides

dtype — har element KYA hai

Ek ndarray mein har element ka same dtype hota hai — isliye arrays homogeneous hote hain (Python lists ke unlike). Homogeneity exactly yahi allow karti hai ki NumPy har baar ek fixed number of bytes step kar sake.


shape — grid KIS SIZE ka hai


strides — NumPy memory mein KAISE chalta hai (asli baat)

Pehle principles se derivation

Ek 2-D array ko row by row lay out karo (C/"row-major" order). Shape (R, C) aur itemsize = s bytes ke liye, A[i, j] ki flat position (elements count karke) hai:

Kyun? Row i tak skip karne ke liye i poori rows cross karni padti hain, har ek length C ki; phir row ke andar j move karo.

s se multiply karke bytes mein convert karo:

Isse general stride formula se match karo:

Comparison se, strides hain:


Views vs copies (asli faayda)


Worked example: offset haath se predict karo


Common mistakes


Recall Feynman: 12-saal ke bachche ko samjhao

Socho ek lambi single shelf par numbered books hain — yahi memory hai. Tum pretend karte ho ki yeh rows aur columns wala bookcase hai. "Row 2, column 1" dhundhne ke liye tum actually bookcase mein nahi chalte; tum quick maths karte ho: "2 rows ki books skip karo, phir 1 aur." Har direction ke liye "skip-amount" stride hai. Shape batata hai ki tum kitni rows/columns hone ka pretend kar rahe ho, aur dtype batata hai ki har book kitni moti hai. Bookcase ko sideways palatna (transpose) ek bhi book move nahi karta — tum sirf swap karte ho ki kaun sa skip-number "row" matlab rakhta hai aur kaun sa "column."


Flashcards

Ek ndarray ke flat buffer ko interpret karne wale teen metadata pieces kaunse hain?
dtype (element type/size), shape (har axis par size), strides (har axis par step karne ke bytes).
strides ki units kya hain?
Bytes (elements nahi).
i_k indices wale element ka byte offset formula kya hai?
offset = Σ_k i_k · stride_k.
Ek C-contiguous (3,4) int64 array ke strides kya hain?
(32, 8): last axis 8 = itemsize, first axis 4×8 = 32.
Kya reshape zyaadatar data copy karta hai?
Nahi — yeh ek view return karta hai, sirf metadata change karta hai (jab tak layout incompatible na ho).
.T (transpose) memory ke saath kya karta hai?
Buffer ke saath kuch nahi; yeh strides (aur shape) swap karta hai. Ek C-array F-contiguous ban jaata hai.
NumPy arrays homogeneous (sabka ek dtype) kyun hote hain?
Taaki har element ek fixed byte size ka ho, strides ko constant amount step karne deta hai → fast indexing.
Slice se real independent array kaise milti hai?
.copy() use karo; sharing .np.shares_memory se check karo.
Axis k ke liye C-contiguous stride formula kya hai?
stride_k = itemsize · k ke baad wale axes ke shape sizes ka product.
.size aur .nbytes mein kya difference hai?
size = elements ki sankhya (shape ka product); nbytes = size × itemsize.

Connections

  • NumPy — broadcasting (axes ko "stretch" karne ke liye 0-stride tricks use karta hai)
  • NumPy — views vs copies & np.shares_memory
  • NumPy — vectorization & performance
  • CPU cache & memory locality
  • Row-major vs column-major (C vs Fortran order)
  • Python lists vs arrays

Concept Map

contains

contains

includes

includes

includes

fixes itemsize + enforces

enables fixed step

sizes axes, product = size

converts index to bytes

multiplied by strides

locates element in

one cache-friendly block

reshape changes metadata only

ndarray

Flat byte buffer

Metadata bookkeeping

dtype

shape

strides

Homogeneous elements

Index i,j

Byte offset

NumPy speed