Python, NumPy & pandas · Lesson 6

NumPy Arrays

NumPy Arrays is part of NumPy's array model.

ConceptWorked examplePracticeKnowledge check
Textbook walkthrough

What NumPy Arrays actually means

NumPy Arrays 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.

NumPy Arrays 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.

Deeper walkthrough

Read NumPy Arrays as a mechanism, not a recipe

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.

Mechanism

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.

Evidence

Know what would convince you

  • Compare row/column counts, dtypes and missing values before and after the operation.
  • Trace a few representative rows or one group manually from source values to result.
Useful distinctionPython list: General-purpose heterogeneous container.
Click a stage to inspect what happens, what changes, and what should be checked before moving on.
Stage 1

Create an ndarray and inspect shape

Create an ndarray and inspect shape, ndim and dtype. For NumPy Arrays, make this checkpoint explicit by recording the evidence inspected, the expected result, and the condition that would make you reject the current result.

Verification focus: record the evidence you inspected and the condition that would make this stage fail.
How it works

Trace the mechanism step by step

  1. Create an ndarray and inspect shape, ndim and dtype.
  2. Select values with indexing, slicing or boolean masks.
  3. Apply vectorised arithmetic; broadcasting aligns compatible dimensions.
  4. Reshape only when the number of elements remains consistent.
  5. Treat NaN deliberately because many aggregations either propagate it or require nan-aware functions.
Worked demonstration

Make the concept concrete

Demonstration

Python / NumPy example

# 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)
Expected / illustrative result
shape: (2, 3)
column means: [2.5 3.5 4.5]
centred:
 [[-1.5 -1.5 -1.5]
 [ 1.5  1.5  1.5]]
Interpret the result.

For NumPy Arrays, trace representative source rows/columns into the result and reconcile row counts, dtypes, keys or missing values that the operation could change.

Distinctions & related ideas

Know what this is — and what it is not

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 deliberately

When it is appropriate

Use NumPy Arrays when the data are naturally tabular and row grain, column meaning, keys and dtypes can be stated explicitly.

Boundary conditions

When to stop or reconsider

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.

Common mistakes

Failure modes to recognise

  • Changing row grain or row count without noticing it.
  • Joining/grouping on keys whose uniqueness or missingness was never checked.
  • Interpreting a derived column or aggregation without reconciling it to source rows and units.
Verification

How to check the result

  • Compare row/column counts, dtypes and missing values before and after the operation.
  • Trace a few representative rows or one group manually from source values to result.
  • For joins/reshapes/grouping, verify key uniqueness/cardinality and reconcile totals where totals should be preserved.
Hands-on practice

Demonstrate understanding

Try this:

Build a tiny, inspectable example of NumPy Arrays. 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.

Work with 4–8 rows that contain the exact key/category/missing-value pattern you want to understand. Trace one row or group all the way through.
Knowledge check

Check reasoning, not memorisation

Before trusting a result from NumPy Arrays, which check provides the strongest evidence that you understand and applied it correctly?

Quick reference

Keep the important distinctions visible

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.
Lesson summary

What to remember

  • NumPy Arrays 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.
  • Create an ndarray and inspect shape, ndim and dtype.
  • Changing row grain or row count without noticing it.
  • Compare row/column counts, dtypes and missing values before and after the operation.