Follow the transformation
Create an ndarray and inspect shape, ndim and dtype.
Select values with indexing, slicing or boolean masks.
Apply vectorised arithmetic; broadcasting aligns compatible dimensions.
Vectorised Computation is part of NumPy's array model.
Vectorised Computation is part of NumPy's array model. An ndarray stores homogeneous values in an n-dimensional shape and enables vectorised operations that act over many elements without writing an explicit Python loop for each value.
Vectorised Computation matters because numerical arrays and labelled tables are the working representations for most data-science pipelines. Types, shape, alignment, missingness and reusable code determine whether later statistics and models receive the intended data.
Treat this as a sequence of observable decisions rather than one opaque command. Stage 1: Create an ndarray and inspect shape, ndim and dtype. Stage 2: Select values with indexing, slicing or boolean masks. Stage 3: Apply vectorised arithmetic; broadcasting aligns compatible dimensions. Final checkpoint: Treat NaN deliberately because many aggregations either propagate it or require nan-aware functions.
Create an ndarray and inspect shape, ndim and dtype.
Select values with indexing, slicing or boolean masks.
Apply vectorised arithmetic; broadcasting aligns compatible dimensions.
Create an ndarray and inspect shape, ndim and dtype. For Vectorised Computation, make this checkpoint explicit by recording the evidence inspected, the expected result, and the condition that would make you reject the current result.
# Step 1 — Import the module so its functions/classes are available to the rest of this example.
import numpy as np
# Step 2 — Construct `X` as an array so vectorised numerical operations can be applied consistently.
X = np.array([[1., 2., 3.], [4., 5., 6.]])
# Step 3 — Compute the right-hand expression and store its result in `centre` for the next step.
centre = X.mean(axis=0)
# Step 4 — Compute the right-hand expression and store its result in `Xc` for the next step.
Xc = X - centre # broadcasting over rows
# Step 5 — Display the current value explicitly so the result/state can be inspected during execution.
print("shape:", X.shape)
# Step 6 — Display the current value explicitly so the result/state can be inspected during execution.
print("column means:", centre)
# Step 7 — Display the current value explicitly so the result/state can be inspected during execution.
print("centred:\n", Xc)shape: (2, 3) column means: [2.5 3.5 4.5] centred: [[-1.5 -1.5 -1.5] [ 1.5 1.5 1.5]]
For Vectorised Computation, trace representative source rows/columns into the result and reconcile row counts, dtypes, keys or missing values that the operation could change.
Python listGeneral-purpose heterogeneous container.NumPy ndarrayHomogeneous n-D numerical array with vectorised operations.BroadcastingImplicitly expands size-1/missing dimensions when shapes are compatible.ReshapeChanges the array view/shape without changing element count.Use Vectorised Computation when the data are naturally tabular and row grain, column meaning, keys and dtypes can be stated explicitly.
Reconsider the operation if row identity/grain is unclear, join keys are not validated, chained transformations hide state, or the task is better expressed with a simpler table operation.
Build a tiny, inspectable example of Vectorised Computation. First create an ndarray and inspect shape, ndim and dtype. Then select values with indexing, slicing or boolean masks. Write the expected result before running it, and explain one condition that would make the result misleading or invalid.
Before trusting a result from Vectorised Computation, which check provides the strongest evidence that you understand and applied it correctly?
Step 1Create an ndarray and inspect shape, ndim and dtype.Step 2Select values with indexing, slicing or boolean masks.Step 3Apply vectorised arithmetic; broadcasting aligns compatible dimensions.Step 4Reshape only when the number of elements remains consistent.