5.4.2 · D3Scientific Computing (Python)

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

3,018 words14 min readBack to topic

Before anything: three tiny words we will reuse constantly, each anchored to a picture.

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

The scenario matrix

Every row is a case class — a genuinely different situation these functions can face. Every worked example below is tagged with the cell(s) it covers, so you can check nothing is missing.

# Case class What is tricky Covered by
C1 Blank grid, 2-D, custom dtype shape is ONE tuple; dtype slot Ex 1
C2 Preallocate-then-fill the fast scientific pattern Ex 2
C3 linspace, endpoint included divide by num−1 Ex 3
C4 linspace, endpoint excluded divide by num Ex 3
C5 linspace degenerate: num=1 and num=0 limiting/empty behaviour Ex 4
C6 arange normal, exclusive stop fencepost count Ex 5
C7 arange with float step drift rounding surprise Ex 6
C8 arange negative / backwards step sign of step Ex 7
C9 arange empty (start ≥ stop, positive step) zero-length array Ex 7
C10 random reproducible + statistics of large sample mean → 0.5, mean → loc Ex 8
C11 Real-world word problem (plotting x-axis) choose the right tool Ex 9
C12 Exam twist: linspace vs arange same-looking call endpoint trap Ex 10

Prerequisite links you may want open: NumPy arrays — shape, dtype, ndim, Floating-point representation and rounding errors, Vectorization vs Python loops.


Example 1 — C1: a 2-D blank grid with a chosen dtype


Example 2 — C2: preallocate, then fill


Example 3 — C3 & C4: linspace with endpoint IN vs OUT

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

Example 4 — C5: degenerate linspace (num = 1 and num = 0)


Example 5 — C6: arange, ordinary case


Example 6 — C7: the float-step drift trap

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

Example 7 — C8 & C9: negative step, and the empty array

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

Example 8 — C10: reproducible random + statistics of a big sample


Example 9 — C11: real-world word problem


Example 10 — C12: the exam twist (endpoint trap)


Recall Rapid self-test

Length of np.arange(2,11,3)? ::: 3 → [2,5,8] (stop 11 excluded). np.linspace(0,10,5) step? ::: , includes 10. np.linspace(0,10,5,endpoint=False) step? ::: , excludes 10. np.linspace(3,9,1) returns? ::: [3.] — just the start (no gaps). np.linspace(3,9,0) returns? ::: [], shape (0,). np.arange(5,5,1) returns? ::: empty array []. np.arange(5,2,1) returns? ::: empty (positive step but start>stop). np.arange(10,0,-2) returns? ::: [10,8,6,4,2]. Mean of many rng.random() draws? ::: 0.5. Right tool for "100 points on , ends included"? ::: np.linspace.


Connections

  • Parent: 5.4.02 Array creation — np.zeros, np.ones, np.linspace, np.arange, np.random (Hinglish)
  • NumPy arrays — shape, dtype, ndim
  • Vectorization vs Python loops
  • Array indexing and slicing
  • Reshaping — reshape, ravel, newaxis
  • Plotting functions with Matplotlib
  • Random sampling and distributions
  • Floating-point representation and rounding errors