5.4.2 · HinglishScientific Computing (Python)

Array creation — np.zeros, np.ones, np.linspace, np.arange, np.random

1,587 words7 min readRead in English

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.


Figure — Array creation — np.zeros, np.ones, np.linspace, np.arange, np.random

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?
Zeros ka ek 2×3 float array (shape tuple ke roop mein pass hoti 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?
Haan (endpoint=True).
Kya np.arange stop value include karta hai?
Nahi, stop exclusive hai (jaise Python range).
np.arange(a,b,s) mein elements ki sankhya?
.
np.arange mein float steps se kyun bachna chahiye?
Floating-point rounding count/last value ko unreliable bana deta hai; linspace use karo.
np.linspace(0,1,5) kitne points aur kya step deta hai?
5 points, step 0.25 → [0,0.25,0.5,0.75,1].
np.empty kya deta hai jo np.zeros nahi deta?
Uninitialized (garbage) values — faster, sirf tab safe hai jab sab overwrite karo.
Random generator banane ka modern tarika?
rng = np.random.default_rng(seed).
RNG ko seed kyun pass karte hain?
Reproducibility — same seed se same sequence milta hai.
rng.random((2,2)) ka range kya hai?
mein uniform floats.
Bahut saare rng.random() samples ka expected mean?
0.5.
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

Concept Map

why

too slow, motivates

blank slate

blank slate

even samples

stepped range

randomness

uses

uses

enables

derived from

Need typed number container

Python list is slow and heavy

NumPy array: one typed buffer + shape

np.zeros -> filled 0

np.ones -> filled 1

np.linspace: N points, ends included

np.arange: fixed step, stop excluded

np.random: random numbers

dtype default float64

Preallocate then fill pattern

gap = b-a / num-1