Array creation — np.zeros, np.ones, np.linspace, np.arange, np.random
5.4.2· Coding › Scientific Computing (Python)
WHY dedicated creators ki zaroorat hai?
Ek Python list [0]*1000000 slow aur memory-heavy hoti hai: har element ek full Python object hota hai jisme ek pointer hota hai. NumPy iske bajaye ek typed buffer allocate karta hai. Isliye np.zeros(10**6) fast, compact, aur vectorized math ke liye ready hota hai. Creators aapko ek output preallocate karke use fill karne bhi dete hain — scientific code mein ye standard fast pattern hai.
np.zeros aur np.ones
WHAT ye dete hain: ek fully-initialized array.
HOW ye kaam karte hain: buffer allocate karo → har byte set karo (zeros literally memset to 0 karta hai; ones chunke gaye dtype mein value 1 likhta hai).
WHY default dtype float64 hai: scientific math mein floats chahiye hote hain; agar integers chahiye toh dtype=int pass karo.
Related: np.full(shape, 7) → 7 se bhara hota hai; np.empty(shape) → uninitialized (garbage values, sabse fast, sirf tab safe hai jab aap sab kuch overwrite karo).
np.linspace — N points, endpoints included
Spacing ki derivation (scratch se)
Hum chahte hain num points jisme , , equally spaced hon. num points ke beech gaps hote hain. Toh har gap hai
np.arange — fixed step, range jaisa
Count ki derivation
Values hain se pehle rukti hain. Elements ki sankhya hai
np.random — random arrays
WHY seed? Reproducible experiments: same seed → same numbers, taaki ek teammate (ya future mein aap khud) identical output paa sake.
rng.random() samples ka mean → ( par uniform ka mean hota hai). rng.normal(loc, scale, ...) ka mean → loc, std → scale, large samples ke liye.

Recall Feynman: 12-saal ke bacche ko samjhao
Ek khaali egg carton imagine karo. np.zeros ek aisi carton hai jisme har cup mein "0" egg hai; np.ones mein "1" eggs hain. np.linspace matlab "mujhe exactly 5 marks chahiye jo ek ruler par 0 se 1 tak evenly painted hon, bilkul dono siron sameet." np.arange matlab "0 se chalna shuru karo aur 0.25-size ke steps lo, 1 tak pahunchne se pehle ruk jao." np.random matlab cups ko surprise numbers se bharne ke liye dice roll karna — aur ek seed ek magic word hai jo dice ko hamesha ek tarah land karata hai taaki tum game replay kar sako.
Common mistakes (steel-manned)
Flashcards
np.zeros((2,3)) kya return karta hai?
np.zeros / np.ones ka default dtype kya hai?
float64.np.linspace(a,b,num) mein endpoint=True ke saath spacing formula kya hai?
Kya np.linspace default mein stop include karta hai?
Kya np.arange stop value include karta hai?
range).np.arange(a,b,s) mein elements ki sankhya?
np.arange mein float steps se kyun bachna chahiye?
linspace use karo.np.linspace(0,1,5) kitne points aur kya step deta hai?
np.empty kya deta hai jo np.zeros nahi deta?
Random generator banane ka modern tarika?
rng = np.random.default_rng(seed).RNG ko seed kyun pass karte hain?
rng.random((2,2)) ka range kya hai?
Bahut saare rng.random() samples ka expected mean?
4×4 array jo saara 7 ke barabar ho, kaise banayein?
np.full((4,4), 7).Connections
- NumPy arrays — shape, dtype, ndim
- Vectorization vs Python loops
- Array indexing and slicing
- Reshaping — reshape, ravel, newaxis
- Plotting functions with Matplotlib (linspace x-axis ko feed karta hai)
- Random sampling and distributions
- Floating-point representation and rounding errors