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:
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 → grayscalebright = 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:
signal = rng.normal(0, 1, 100).cumsum() # a random walk# 5-point moving average via convolutionkernel = np.ones(5) / 5smooth = 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:
X = rng.normal(50, 10, (100, 4)) # 100 samples, 4 featuresmu = np.nanmean(X, axis=0) # per-feature meansigma = np.nanstd(X, axis=0) # per-feature stdXz = (X - mu) / sigma # broadcasting does every column at onceXz.mean(axis=0).round(6) # ≈ 0 for each featureXz.std(axis=0).round(6) # ≈ 1 for each feature
Common gotchas
- Integer arrays truncate —
np.array([1,2,3]) / 2is 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, notand/or. np.nanbreaks==and poisons sums — usenp.isnanand thenan*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
- 01Images are 2-D/3-D arrays: cropping is slicing, mirroring is indexing, grayscale is a mean over the channel axis.
- 021-D arrays are signals/series — smooth with np.convolve, find change with np.diff.
- 03A z-score pipeline combines per-column aggregation, broadcasting and nan-aware functions in a few lines.
- 04Remember the gotchas: integer truncation, views vs copies, &/| for masks, and NaN behaviour.
- 05NumPy is the foundation — pandas, scikit-learn, SciPy and deep-learning libraries all build on the ndarray.
