ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

NumPy 101 ⚡

pip install numpy
uv add numpy
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

What is NumPy?

  • Numerical Python = arrays + fast math
  • More compact and faster than Python lists
  • Supports broadcasting, vectorization, and linear algebra

How to import numpy:

import numpy as np
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Creating arrays

a = np.array([1, 2, 3])  # vector
b = np.array([[1, 2], [3, 4]]) # 2 by 2 matrix

Common constructors:

np.zeros((2, 3))
np.ones((3,))
np.eye(3)            # identity
np.arange(0, 10, 2)
np.linspace(0, 1, 5)
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Array properties

Classical attributes of an array:

a.shape   # dimensions
a.ndim    # number of axes
a.size    # number of elements
a.dtype   # element type

NumPy uses float64/int64 by default but it can handle other dtypes:

x = np.array([1.0, 2.0, 3.0], dtype=np.float32)

💡 PyTorch/JAX use float32/int32 by default!

ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Indexing & slicing

x = np.array([[10, 20, 30], [40, 50, 60]])
x[1, 2]     # element
x[:, 1]     # column
x[0]        # row
x[0:2, 1:]  # submatrix

Boolean & fancy indexing:

x[x > 30]         
x[[0, 1], [1, 2]]
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Array operations

Element-wise:

a = np.array([1, 2, 3])
b = np.array([10, 20, 30])
a + b
a * b
a ** 2

Aggregate functions:

np.sum(a), np.mean(a), np.std(a)
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Broadcasting

Works on arrays of different shapes:

a = np.array([[1], [2], [3]])
b = np.array([10, 20, 30])
a + b

The result is:

array([[11, 21, 31],
       [12, 22, 32],
       [13, 23, 33]])

Very powerful: no loops needed.

ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Reshaping arrays

a = np.arange(6)
a.reshape((2, 3))
a.flatten()   # copy
a.ravel()     # view
a.T           # transpose

Add dimensions:

a[:, np.newaxis]
np.expand_dims(a, axis=0)
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Linear algebra basics

Create a matrix and a vector

A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])

A few possible methods:

np.dot(A, b)       # dot
A @ A              # matrix mult
np.linalg.inv(A)   # inverse
np.linalg.solve(A, b)

NumPy ≠ full linear algebra library, but covers essentials.

ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Random numbers

If you need reproductibility:

np.random.seed(0)

There a several random generation methods:

np.random.rand(2, 3)       # uniform [0,1)
np.random.randn(3)         # normal
np.random.integers(1, 10, (2, 2))
np.random.choice([1, 2, 3], size=5)
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Practical example: distance

Let and . We want to compute

a = np.array([1, 2])
b = np.array([4, 6])

dist = np.linalg.norm(a - b)
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Practical example: normalize rows

Let . We want to normalize each row of by its norm.

Pick a random matrix

X = np.array([[1, 2], [3, 4]])

Compute norm along the column axis

row_norms = np.linalg.norm(X, axis=1, keepdims=True)
X_normed = X / row_norms
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Practical example: Monte Carlo estimation of π

We sample points uniformly in the unit square .
The probability of falling inside the quarter unit circle is .

N = 10000
x = np.random.rand(N)
y = np.random.rand(N)
inside = x**2 + y**2 <= 1
pi_est = 4 * np.sum(inside) / N
ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Recap

  • Arrays: fast, compact, vectorized
  • Indexing, slicing, broadcasting
  • Reshape and transpose
  • Basic linear algebra
  • Random numbers for simulations

NumPy is the foundation of many scientific stacks in Python.

ENSAI - 2A - Programmation algorithmique en Python - 2025/2026

Resources

💡 Use the quickstart and examples to learn.

💡 The reference is best when you need details about a function.