Artificial Intelligence / Python Libraries / NumPy

Advanced NumPy topics

Go beyond the basics with tools for selecting complex data, solving math problems, and working with larger arrays.

Advanced indexing

You already know basic indexing such as data[0] and slicing such as data[1:3]. Advanced indexing lets you use an array or list of indexes to select several specific values, rows, or positions at once.

Select and reorder rows

Pass a list of row indexes to select rows in that order. You can also repeat an index:

Ready to run in your browser.
Output will appear here.

Select specific positions

Two index arrays can select paired row and column positions. Here, NumPy selects row 0 with column 2, then row 2 with column 1:

Ready to run in your browser.
Output will appear here.

These are paired coordinates. They select two individual values, rather than every combination of the listed rows and columns.

Use a Boolean mask

A Boolean condition can select rows that match a rule. In data[:, 1], the colon means “all rows” and 1 selects the second column (indexes start at 0). This example keeps rows whose middle value is at least 20:

Ready to run in your browser.
Output will appear here.

Advanced indexing returns a copy

Unlike a basic slice, selecting with an index list creates a separate array. Changing that selection does not change the original:

Ready to run in your browser.
Output will appear here.

NumPy guide to indexing

Memory layout and performance

A NumPy array has two important parts: a data buffer containing the values, and metadata describing how to interpret them. The metadata includes the data type, dimensions, and strides.

Strides describe steps through memory

A stride tells NumPy how many bytes to move to reach the next value along an axis. In a standard row-major 2D array, values from each row are stored next to each other in one block. For a 2 × 3 array of 32-bit integers, each value uses 4 bytes, so the strides are 12 bytes to the next row and 4 bytes to the next column.

A transpose can be a view

Transposing an array with .T changes how its metadata maps the values into rows and columns. NumPy can often do this without moving the data buffer; the transposed array is then a view with different strides. It may no longer be C-contiguous, which can affect how some operations perform.

Run this example to inspect the array’s memory details and compare it with its transpose:

Ready to run in your browser.
Output will appear here.

NumPy guide to copies and views · NumPy guide to array memory layout

Linear algebra

NumPy’s linalg module provides tools for working with vectors and matrices. Here are three useful ideas: solving equations, measuring vector length, and understanding how a matrix transforms special directions.

Solve a system of equations

A set of linear equations can be written as A @ x = b. Use np.linalg.solve(A, b) to find the unknown values in x. For example, it can solve 3x + y = 9 and x + 2y = 8. The coefficient matrix must be square and have a unique solution.

Ready to run in your browser.
Output will appear here.

Measure a vector with a norm

A vector norm is one way to measure a vector’s length. The default np.linalg.norm() uses the Euclidean length. For [3, 4], the length is 5, from the right triangle calculation √(3² + 4²).

Ready to run in your browser.
Output will appear here.

Eigenvalues and eigenvectors

An eigenvector is a direction that a matrix keeps on the same line after transforming it; its eigenvalue tells how much that direction is scaled, including whether it is reversed. In this diagonal example, the two coordinate directions are scaled by 2 and 3.

Ready to run in your browser.
Output will appear here.

NumPy linear algebra reference

Missing and invalid data

Real datasets can have blank entries or values that cannot be used in a calculation. NumPy has tools to identify these values and choose how to handle them.

Represent missing numbers with NaN

np.nan (“Not a Number”) is a floating-point value used to mark a missing numerical value. Use np.isnan() to find it. A regular mean propagates NaN, while np.nanmean() calculates the mean while ignoring NaN entries.

Ready to run in your browser.
Output will appear here.

Check for invalid numbers

Some calculations can produce infinity as well as NaN. np.isfinite() returns True only for finite values, so it can identify both NaN and positive or negative infinity.

Ready to run in your browser.
Output will appear here.

Read missing entries from CSV

np.genfromtxt() can read a CSV file with blank numerical fields. By default, it represents missing values with NaN for floating-point data.

Ready to run in your browser.
Output will appear here.

NumPy isnan reference · NumPy nanmean reference · NumPy guide to reading and writing files

Working with large arrays

Large arrays can use more memory than a computer has available. A calculation that creates another full-size array can use even more. Two useful approaches are to read a saved array through a memory map and to process data in chunks.

Read a saved array with a memory map

A memory map lets NumPy access an array stored in a .npy file as if it were an array, while the file stays on disk. The operating system brings file regions into memory as they are accessed, so you can work with a slice without loading the whole array at once. With mmap_mode="r", the mapped array is read-only.

This example assumes that large_data.npy already exists in your working directory. It calculates the mean a chunk of rows at a time:

import numpy as np

large_data = np.load("large_data.npy", mmap_mode="r")
rows_per_chunk = 100_000
total = 0.0
count = 0

for start in range(0, large_data.shape[0], rows_per_chunk):
    chunk = large_data[start:start + rows_per_chunk]
    total += chunk.sum()
    count += chunk.size

print("Mean:", total / count)

This assumes the file contains numeric data with at least one value. The chunk loop also works with an ordinary in-memory array, but memory mapping is what avoids loading the entire saved array up front.

Practice processing in chunks

This small example uses the same chunking pattern on an in-memory array. It prints each chunk and then calculates one mean across all the values:

Ready to run in your browser.
Output will appear here.

NumPy load and memory-map reference · NumPy guide to large arrays

Finding and selecting values

NumPy can find values that match a condition, and return the position of the smallest or largest value. These tools are useful when you need the value itself and where it appears in an array.

Select values with np.where()

Give np.where() a condition to get the indexes where it is true. You can also use it to choose between two values for each position. Here, values below 70 are replaced with 70:

Ready to run in your browser.
Output will appear here.

Find the largest and smallest values

np.argmax() returns the index of the largest value; np.argmin() returns the index of the smallest. Array indexes start at 0. If a value appears more than once, these functions return the first matching index.

Ready to run in your browser.
Output will appear here.

For a 2D array, pass axis=0 to find an index within each column, or axis=1 to find one within each row:

Ready to run in your browser.
Output will appear here.

NumPy where reference · NumPy argmax reference · NumPy argmin reference

NumPy: putting it all together

You’ve now covered the core NumPy skills: creating arrays, understanding dimensions and data types, selecting and changing values, calculating with axes, reshaping, and working with copies and views. You’ve also explored advanced indexing, memory layout, linear algebra, missing data, large arrays, and ways to find important values.

The central idea is to represent numerical data as arrays and let NumPy apply operations across those arrays. When working with real data, check its shape and data type, choose the right axis, and keep memory use in mind.

← Review the complete NumPy guide