Skip to main content
Albero Academy

NumPy in Practice

Bring it together — images and signals as arrays, a normalisation pipeline, common gotchas, and where NumPy hands off to the rest of the stack.

Chapter 16NumPy15 min readBeginner friendly
  • #NumPy
  • #Practice
  • #Capstone

You now have the whole toolkit. This closing chapter shows it working on real data shapes — images and signals — pulls the pieces into one pipeline, and collects the gotchas worth remembering. Then it points to where NumPy hands off to pandas, scikit-learn and the rest.

Images are arrays

A grayscale image is a 2-D array of pixel intensities; a colour image is 3-D (height, width, 3). Everything you've learned applies directly — cropping is slicing, brightening is arithmetic, flipping is indexing:

image operations = array operations
img = np.random.randint(0, 256, (128, 128, 3), dtype=np.uint8)
img[:64, :, :] # crop the top half (slicing)
img[:, ::-1, :] # mirror horizontally (reverse the column axis)
gray = img.mean(axis=2) # average channels → grayscale
bright = np.clip(img * 1.2, 0, 255).astype(np.uint8) # brighten & clamp

Signals & series

A 1-D array is a time series or signal. A moving average smooths it, and differences reveal change:

smoothing & differencing
signal = rng.normal(0, 1, 100).cumsum() # a random walk
# 5-point moving average via convolution
kernel = np.ones(5) / 5
smooth = np.convolve(signal, kernel, mode='valid')
np.diff(signal) # consecutive differences (velocity)

A normalisation pipeline

A tiny end-to-end example combining broadcasting, aggregation and nan-handling — standardising a feature matrix column-wise:

standardise features (z-score)
X = rng.normal(50, 10, (100, 4)) # 100 samples, 4 features
mu = np.nanmean(X, axis=0) # per-feature mean
sigma = np.nanstd(X, axis=0) # per-feature std
Xz = (X - mu) / sigma # broadcasting does every column at once
Xz.mean(axis=0).round(6) # ≈ 0 for each feature
Xz.std(axis=0).round(6) # ≈ 1 for each feature

Common gotchas

  • Integer arrays truncate — np.array([1,2,3]) / 2 is fine, but in-place //= on ints floors silently.
  • A slice is a view; mutating it changes the original (use .copy()).
  • Use &/| with parentheses for array conditions, not and/or.
  • np.nan breaks == and poisons sums — use np.isnan and the nan* functions.

Practice

Load or synthesise a small image array and crop, mirror and grayscale it. Smooth a noisy 1-D signal with a moving average. Finally, write a z-score normalisation of a feature matrix in three lines using broadcasting and confirm each column ends with mean ≈ 0 and std ≈ 1. You've completed NumPy — pandas builds directly on everything here.

Key takeaways

  1. 01Images are 2-D/3-D arrays: cropping is slicing, mirroring is indexing, grayscale is a mean over the channel axis.
  2. 021-D arrays are signals/series — smooth with np.convolve, find change with np.diff.
  3. 03A z-score pipeline combines per-column aggregation, broadcasting and nan-aware functions in a few lines.
  4. 04Remember the gotchas: integer truncation, views vs copies, &/| for masks, and NaN behaviour.
  5. 05NumPy is the foundation — pandas, scikit-learn, SciPy and deep-learning libraries all build on the ndarray.