NumPy

NumPy from arrays, dtypes and indexing to views, masks, broadcasting, reductions, reshaping, sorting, statistics, linear algebra, random numbers and images.

Level
Intermediate
Time
About 26 hours
Modules
5
Lessons
15
Exercises
45

NumPy is the foundation of scientific Python: pandas, scikit-learn, PyTorch and nearly every data tool store their numbers in NumPy arrays. This course teaches how arrays work, why they are fast, and how to use them without falling into the traps of views, dtypes and broadcasting.

NumPy itself cannot run in this site's Python runner, so every lesson shows real NumPy code with its output as text, and every runnable example and exercise is a plain-Python model of what NumPy does. You learn the exact rules by implementing them, then use the real library with confidence.

What you will learn

  • Create arrays and predict their shape and dtype
  • Index, slice and mask arrays and know when you get a view or a copy
  • Write vectorised code with broadcasting and reductions along axes
  • Reshape, stack, sort and search arrays
  • Compute statistics, solve linear systems and run seeded simulations
  • Apply NumPy to real data such as images

Skills you will gain

  • Array creation and dtypes
  • Indexing, views and masks
  • Broadcasting
  • Reductions and statistics
  • Linear algebra
  • Random simulation

Before you start

  • The Python course: lists, loops, functions and slicing
  • Basic algebra and comfort with tables of numbers
  • Nothing to install for the course; install NumPy with pip later to use it on your own computer

Where it is used

  • Data analysis with pandas
  • Machine learning and deep learning
  • Scientific computing
  • Image processing

Modules

Module 1: Arrays and types

Why NumPy is fast, the many ways to create arrays, and how dtypes decide what an array can hold and how arithmetic behaves.

  1. 1

    Why NumPy Intermediate

    What NumPy is, how its arrays differ from Python lists, why whole-array operations are fast, and how this course models NumPy in plain Python so every exercise runs in your browser.

  2. 2

    Creating arrays Intermediate

    The ways to make arrays: from lists with np.array, filled with zeros, ones or a value, evenly spaced with arange and linspace, identity matrices with eye, and choosing the shape and dtype as you create them.

  3. 3

    Data types: dtype Intermediate

    Why every array has one dtype, the common types (bool, integers, floats), how NumPy chooses the dtype of a result, converting with astype, and the surprises of fixed-size integers and floats.

Module practice and quiz →

Module 2: Indexing and memory

Indexing and slicing in one and two dimensions, views versus copies, and boolean masks for filtering and updating.

  1. 1

    Indexing and slicing Intermediate

    Reading elements and sub-arrays: one index per axis separated by commas, negative indices, slices with start, stop and step on each axis, and how an integer index drops a dimension while a slice keeps it.

  2. 2

    Views and copies Intermediate

    Why slicing a NumPy array does not copy it, how writes through a view change the original, which operations copy (fancy indexing, masks, copy, astype), and how to tell them apart.

  3. 3

    Boolean masks and where Intermediate

    Comparisons that give arrays of True and False, selecting with a mask, combining conditions with &, | and ~, counting matches, np.where, and assigning through a mask.

Module practice and quiz →

Module 3: Vectorised computing

Element-wise arithmetic and ufuncs, broadcasting rules for arrays of different shapes, and reductions along axes.

  1. 1

    Vectorised arithmetic and ufuncs Intermediate

    Arithmetic operators that work element by element, operations with a single number, universal functions such as sqrt and exp, division by zero producing inf and nan, and why vectorised code replaces loops.

  2. 2

    Broadcasting Advanced

    How NumPy combines arrays of different shapes: lining shapes up from the right, stretching dimensions of size 1, the error when shapes do not fit, and common uses such as centring columns and building tables.

  3. 3

    Reductions along axes Intermediate

    Summarising arrays with sum, mean, min, max, std, argmin and argmax, choosing an axis to reduce along, keeping dimensions with keepdims, and running totals with cumsum.

Module practice and quiz →

Module 4: Shaping, ordering and statistics

Reshaping and joining arrays, sorting, unique values and binary search, and descriptive statistics with percentiles and histograms.

  1. 1

    Reshaping and stacking Intermediate

    Changing an array's shape without changing its data: reshape and the -1 shortcut, flatten and ravel, transposing with .T, and joining arrays with concatenate, vstack, hstack and stack.

  2. 2

    Sorting, unique values and searching Intermediate

    Ordering and finding data with NumPy: np.sort and the in-place sort, argsort for the order of positions, unique values with counts, and fast binary search in sorted arrays with searchsorted.

  3. 3

    Statistics with NumPy Intermediate

    Describing data with NumPy: mean, median and std (and the ddof trap), percentiles with linear interpolation, histograms with np.histogram, correlation with np.corrcoef, and standardising columns.

Module practice and quiz →

Module 5: NumPy in practice

Linear algebra with @ and np.linalg, random numbers and simulation with seeded generators, and images as arrays.

  1. 1

    Linear algebra with NumPy Advanced

    Vectors and matrices in NumPy: dot products, the @ operator for matrix multiplication and its shape rule, the identity, the determinant and inverse, and solving systems of equations with np.linalg.solve.

  2. 2

    Random numbers and simulation Intermediate

    NumPy's random generator: default_rng and seeds for reproducible results, integers, random, normal and choice, shuffling and permutations, how a pseudo-random generator works, and Monte Carlo estimates with their error.

  3. 3

    NumPy in practice: images as arrays Advanced

    Putting NumPy to work on images: pixels as uint8 arrays with shape (height, width) or (height, width, 3), cropping and flipping with slices, brightness changes with clipping and the uint8 overflow trap, masks and simple filters.

Module practice and quiz →

Projects

Final assessment →

What to learn next