| name | numpy-2-4-4 |
| description | Fundamental package for scientific computing with Python, providing n-dimensional arrays, mathematical functions, random number generators, linear algebra, and Fourier transforms with modern Python typing and Array API standard compatibility. Use when building Python programs requiring numerical array computing, matrix operations, statistical analysis, or high-performance vectorized computation. |
NumPy 2.4.4
Overview
NumPy (Numerical Python) is the fundamental package for scientific computing in Python. It provides a powerful n-dimensional array object (ndarray), derived objects (masked arrays, matrices), and an assortment of routines for fast operations on arrays including mathematical, logical, shape manipulation, sorting, selecting, I/O, discrete Fourier transforms, basic linear algebra, basic statistical operations, random simulation, and much more.
NumPy 2.4.4 is part of the NumPy 2.x series (released June 2024 as a major breaking-change release). It supports Python 3.11 through 3.14, implements the Array API standard 2024.12 compatibility in its main namespace, and continues work on free-threaded Python support, user dtypes, and annotation improvements.
Key features of NumPy 2.x:
- New type promotion rules (NEP 50) preserving scalar precision consistently
- Cleaned Python API namespace (NEP 52) with ~100 members moved or removed
- Default integer is now 64-bit on all 64-bit systems (
np.intp equivalent)
- Array API standard compatibility in the main namespace
- C-API changes including opaque
PyArray_Descr struct and increased max dimensions to 64
- SIMD optimizations via CPU dispatch
- Multi-phase C extension initialization (PEP 489)
When to Use
- Building numerical computing applications requiring n-dimensional array operations
- Performing matrix algebra, linear system solving, eigenvalue decomposition, or SVD
- Implementing vectorized computations to replace slow Python loops
- Working with scientific data: signal processing, image analysis, statistics
- Generating random numbers from various probability distributions
- Reading/writing binary and text data in array format
- Building foundations for data science pipelines (pandas, scikit-learn, etc. depend on NumPy)
- Interfacing with GPU/distributed array libraries (CuPy, Dask, JAX use NumPy-compatible APIs)
- Implementing Array API standard-compatible code for backend-agnostic array computing
Core Concepts
The ndarray — N-dimensional Array
The ndarray is the central data structure in NumPy. It represents a homogeneous, rectangular grid of values with fixed size and uniform data type. Key attributes:
ndim — number of dimensions (axes)
shape — tuple of sizes along each axis
size — total number of elements
dtype — data type of elements
itemsize — size in bytes of each element
import numpy as np
a = np.array([[1, 2, 3], [4, 5, 6]])
a.ndim
a.shape
a.size
a.dtype
Broadcasting
Broadcasting describes how NumPy handles arrays of different shapes in arithmetic operations. The smaller array is conceptually "stretched" across the larger one. Rules: dimensions are compared from trailing to leading; they are compatible if equal or one is 1. Missing leading dimensions are treated as size 1.
a = np.array([[0, 0, 0], [10, 10, 10]])
b = np.array([1, 2, 3])
a + b
Universal Functions (ufuncs)
Ufuncs operate element-by-element on arrays, supporting broadcasting, type casting, and multiple outputs. Examples: np.add, np.multiply, np.sin, np.exp. They support keyword arguments like out (output buffer), where (boolean mask), and dtype (computation precision).
Data Types (dtype)
NumPy supports 24 fundamental scalar types organized in a hierarchy: generic → number → integer/floating/complexfloating. Common types: int8-int64, uint8-uint64, float16/float32/float64, complex64/complex128, bool_, str_, bytes_, datetime64, timedelta64. Structured dtypes allow C-like records with named fields.
Usage Examples
Array Creation
import numpy as np
a = np.array([1, 2, 3, 4])
b = np.array([[1, 2], [3, 4]])
zeros = np.zeros((3, 4))
ones = np.ones((2, 2), dtype=np.int32)
empty = np.empty((5,))
full = np.full((3, 3), 7.5)
identity = np.eye(4)
seq = np.arange(0, 10, 2)
spaced = np.linspace(0, 1, 5)
log_spaced = np.logspace(0, 2, 3)
Indexing and Slicing
a = np.arange(12).reshape(3, 4)
a[0]
a[1, 2]
a[:, ::2]
a[[0, 2], [1, 3]]
mask = a > 5
a[mask]
Linear Algebra
import numpy as np
A = np.array([[1, 2], [3, 4]])
b = np.array([5, 6])
C = A @ A.T
x = np.linalg.solve(A, b)
eigenvalues, eigenvectors = np.linalg.eig(A)
U, s, Vt = np.linalg.svd(A)
Random Number Generation
import numpy as np
rng = np.random.default_rng(seed=42)
uniform = rng.random((3, 3))
normal = rng.standard_normal(1000)
integers = rng.integers(low=0, high=10, size=5)
choice = rng.choice([10, 20, 30], size=3, replace=False)
Advanced Topics
Array Fundamentals: Deep dive into ndarray creation, indexing patterns, views vs copies, structured arrays, and memory layout → Array Fundamentals
Data Types and Type Promotion: Complete dtype hierarchy, structured dtypes, NEP 50 type promotion rules, casting modes, and NumPy 2.0 changes → Data Types and Type Promotion
Universal Functions and Broadcasting: Ufunc internals, generalized ufuncs, output buffers, where masks, broadcasting rules with examples → Universal Functions and Broadcasting
Linear Algebra and Fourier Transforms: BLAS/LAPACK-backed routines, matrix decompositions, eigenvalue problems, norm computation, DFT operations → Linear Algebra and Fourier Transforms
Random Number Generation: Generator API, bit generators (PCG64, MT19937), seeding strategies, parallel generation, distribution methods → Random Number Generation
I/O and Memory Mapping: Binary formats (.npy/.npz), text file I/O, memory-mapped arrays, string formatting → I/O and Memory Mapping
Array API Standard and Interoperability: Array API 2024.12 compliance, __array_namespace_info__, entry points, duck array protocols → Array API Standard and Interoperability
NumPy 2.x Migration Guide: NEP 50 type promotion changes, NEP 52 namespace cleanup, C-API changes, default integer on Windows, Ruff NPY201 rule → NumPy 2.x Migration Guide