NumPy (pronounced NUM-py) is a library for the Python programming language that adds support for large, multi-dimensional arrays and matrices, along with a collection of high-level mathematical functions to operate on these arrays. It is open-source software with many contributors and is fiscally sponsored by NumFOCUS. NumPy is a core component of the scientific Python ecosystem, underpinning libraries such as SciPy and Matplotlib, and is widely used in fields ranging from data analysis to [[machine-learning] and Deep learning.
The predecessor of NumPy, Numeric, was originally created by Jim Hugunin with contributions from several other developers. In 2005, Travis Oliphant created NumPy by incorporating features of the competing Numarray into Numeric, with extensive modifications. The first release, NumPy 1.0, came in 2006. Since then, NumPy has evolved to support Python 3 (starting with version 1.5.0 in 2011) and has become a standard tool for numerical computing in Python.
History
The Python programming language was not originally designed for numerical computing, but it attracted the attention of the scientific and engineering community early on. In 1995, the special interest group matrix-sig was founded with the aim of defining an array computing package. Among its members was Python designer and maintainer Guido van Rossum, who extended Python's syntax, particularly the indexing syntax, to make array computing easier.
An implementation of a matrix package was completed by Jim Fulton and then expanded to support multi-dimensional arrays by Jim Hugunin, who called it Numeric (also known as the "Numerical Python extensions" or "NumPy"). Hugunin, a graduate student at the Massachusetts Institute of Technology (MIT), joined the Corporation for National Research Initiatives (CNRI) in 1997 to work on JPython, leaving Paul Dubois of Lawrence Livermore National Laboratory (LLNL) to take over as maintainer. Other early contributors included David Ascher, Konrad Hinsen, and Travis Oliphant.
A new package called Numarray was written as a more flexible replacement for Numeric. It had faster operations for large arrays but was slower on small ones, so for a time both packages were used in parallel for different use cases. The last version of Numeric (v24.2) was released on 11 November 2005, while the last version of numarray (v1.5.2) was released on 24 August 2006. There was a desire to get Numeric into the Python standard library, but Guido van Rossum decided that the code was not maintainable in its state then.
In early 2005, Travis Oliphant wanted to unify the community around a single array package. He ported Numarray's features to Numeric and released the result as NumPy 1.0 in 2006. This new project was part of SciPy, but to avoid installing the large SciPy package just to get an array object, it was separated and called NumPy. In 2011, PyPy started development on an implementation of the NumPy API for PyPy; as of 2023, it is not yet fully compatible with NumPy.
Features
NumPy targets the CPython reference implementation of Python, which is a non-optimizing bytecode interpreter. Mathematical algorithms written for this version of Python often run much slower than compiled equivalents due to the absence of compiler optimization. NumPy addresses this slowness partly by providing multidimensional arrays and functions and operators that operate efficiently on arrays. Using NumPy requires rewriting some code, mostly inner loops, to use arrays.
Using NumPy in Python gives functionality comparable to MATLAB, since both are interpreted and both allow the user to write fast programs as long as most operations work on arrays or matrices instead of scalars. MATLAB boasts a large number of additional toolboxes, notably Simulink, whereas NumPy is intrinsically integrated with Python, a more modern and complete programming language. Complementary Python packages are available: SciPy adds more MATLAB-like functionality, and Matplotlib provides MATLAB-like plotting. Although MATLAB can perform sparse matrix operations, NumPy alone cannot and requires the use of the scipy.sparse library. Internally, both MATLAB and NumPy rely on BLAS and LAPACK for efficient linear algebra computations.
Python bindings of the widely used computer vision library OpenCV utilize NumPy arrays to store and operate on data. Since images with multiple channels are simply represented as three-dimensional arrays, indexing, slicing, or masking with other arrays are efficient ways to access specific pixels. The NumPy array as a universal data structure in OpenCV for images, extracted feature points, filter kernels, and more simplifies programming workflow and debugging. Importantly, many NumPy operations release the global interpreter lock, which allows for multithreaded processing. NumPy also provides a C API, allowing Python code to interoperate with external libraries written in low-level languages.
The ndarray Data Structure
The core functionality of NumPy is its "ndarray" (n-dimensional array) data structure. These arrays are strided views on memory. In contrast to Python's built-in list data structure, arrays are homogeneously typed: all elements of a single array must be of the same type. Such arrays can also be views into memory buffers allocated by C/C++, Python, and Fortran extensions to the CPython interpreter without the need to copy data, giving a degree of compatibility with existing numerical libraries. This functionality is exploited by the SciPy package, which wraps a number of such libraries (notably BLAS and LAPACK). NumPy has built-in support for memory-mapped ndarrays.
Limitations
Inserting or appending entries to an array is not as trivially possible as it is with Python's lists. The np.pad routine to extend arrays actually creates new arrays of the desired shape and padding values, copies the given array into the new one, and returns it. Similarly, np.concatenate([a1, a2]) does not link the two arrays but returns a new one filled with the entries from both. Reshaping the dimensionality of an array with np.reshape is only possible as long as the number of elements does not change. These circumstances originate from the fact that NumPy's arrays must be views on contiguous memory buffers.
Algorithms that are not expressible as a vectorized operation will typically run slowly because they must be implemented in "pure Python", while vectorization may increase memory complexity of some operations from constant to linear, because temporary arrays must be created that are as large as the inputs. Runtime compilation of numerical code has been implemented by several groups to avoid these problems; open-source solutions that interoperate with NumPy include numexpr and Numba. Cython and Pythran are static-compilation options that can also accelerate NumPy-based code.
Applications and Impact
NumPy is foundational to many scientific and data-driven fields. It provides the array operations that power Machine learning frameworks and libraries, such as TensorFlow and PyTorch, which are used to build Neural network models and Transformer (architecture) architectures. In Artificial intelligence research, NumPy arrays are often used for data preprocessing, feature extraction, and implementing algorithms from scratch. The library's efficiency and flexibility have made it a standard tool in academic and industrial settings, from MIT and Stanford to companies like Google and Amazon.
NumPy's influence extends beyond traditional scientific computing. It is used in computer vision for image processing, in natural language processing for handling word embeddings, and in reinforcement learning for managing state spaces. Its integration with OpenCV and other libraries has streamlined workflows in robotics and autonomous systems, such as those developed by Waymo and Tesla. As of 2023, NumPy remains one of the most downloaded Python packages, reflecting its central role in the modern computational ecosystem.