Academy
ProgrammingPython AdvancedNumPy Basics

NumPy Basics

Why NumPy Exists

Python lists are flexible — they can hold any mix of types, grow dynamically, and handle arbitrary data. That flexibility has a cost: every operation on a list is slow compared to what a computer can do natively with numbers.

Scientific computing requires speed. Working with a million measurements, a matrix of pixel values, or the weights of a neural network — Python lists are orders of magnitude too slow.

NumPy solves this with the ndarray — an N-dimensional array of a single fixed type, stored in a contiguous block of memory. Operations on NumPy arrays are executed in compiled C code, not interpreted Python. The difference in speed is typically 100x or more.

NumPy is the foundation of the entire scientific Python ecosystem — SciPy, Pandas, Matplotlib, scikit-learn, and PyTorch all build on it.

Creating Arrays

np.array() creates an array from a Python list. dtype is the data type of the elements — int64 here. shape is a tuple describing the dimensions — (5,) means one dimension with 5 elements.

Built-in constructors

  • zeros / ones — arrays of zeros or ones, given a shape
  • arange(start, stop, step) — like Python's range, but returns an array
  • linspace(start, stop, n)n evenly spaced values between start and stop (inclusive)

linspace is particularly useful in scientific work — generating the x-axis for a plot, sampling a function at regular intervals, and so on.

The Critical Difference: Vectorised Operations

With Python lists, to add 10 to every element you write a loop or comprehension. With NumPy, you operate on the whole array at once:

Every operation applies to the entire array simultaneously. No loops. No comprehensions. The computation runs in compiled code. For large arrays, this is not a stylistic preference — it's a practical necessity.

2D Arrays — Matrices

NumPy arrays can have any number of dimensions. A 2D array is a matrix:

shape is (3, 3) — 3 rows, 3 columns. ndim is 2. Rows come first, columns second — consistent with mathematical convention.

Indexing and Slicing

1D indexing works exactly like lists:

2D indexing uses a comma: array[row, col]

m[:, 1] — all rows, column 1. The : means "all of this dimension". This notation is compact and expressive once you're used to it.

Boolean Indexing — Filtering by Condition

A comparison produces a boolean arrayTrue where the condition holds, False elsewhere. Using that array as an index selects only the elements where it's True. This is the NumPy equivalent of a filter — no loops, no comprehensions.

Aggregate Functions

argmax and argmin return the position of the extreme value — useful when you need to know which element, not just what the value is.

For 2D arrays, most aggregate functions accept an axis argument:

axis=0 operates along rows (down the columns). axis=1 operates along columns (across the rows). Visualise folding the array along that axis.

Broadcasting

Broadcasting is NumPy's rule for operating on arrays of different shapes. When shapes are compatible, NumPy stretches the smaller array to match the larger one — without copying data.

row has shape (3,). matrix has shape (3, 3). NumPy broadcasts row across all three rows of matrix — adding [10, 20, 30] to each row.

Every combination of col and row values is computed — a full outer addition. Broadcasting eliminates a large category of explicit loops in numerical code.

A Practical Example: Normalising Data

Normalising data to zero mean and unit standard deviation — one of the most common preprocessing steps in data science and machine learning — expressed as two arithmetic lines. No loops. No intermediate lists. The entire array is transformed in place by broadcasting.

What You Have Learned

NumPy is the engine behind scientific Python — its array model and vectorised operations are the foundation everything else builds on.

The key ideas:

  • np.array() creates an ndarray — fast, fixed-type, stored in contiguous memory
  • Vectorised operations apply to entire arrays simultaneously — no loops needed
  • shape, dtype, ndim, size describe an array's structure
  • arange and linspace generate numeric sequences
  • 2D indexing: a[row, col], a[:, col], a[row, :]
  • Boolean indexing: a[a > threshold] selects elements by condition
  • Aggregate functions: sum, mean, std, min, max, argmax — with optional axis
  • Broadcasting: NumPy stretches smaller arrays to match larger ones for element-wise operations

In the next lesson, you'll learn Pandas — the library that brings NumPy's power to labelled, structured data, making data analysis natural and expressive.