NumPy - Introduction
Introduction to NumPy
NumPy is a Python package. It stands for ‘Numerical Python’. It is a library consisting of multidimensional array objects and a collection of routines for processing of array.
Numeric, the ancestor of NumPy, was developed by Jim Hugunin. Another package Numarray was also developed, having some additional functionalities. In 2005, Travis Oliphant created NumPy package by incorporating the features of Numarray into Numeric package. There are many contributors to this open source project.
Operations using NumPy
Using NumPy, a developer can perform the following operations −
- Mathematical and logical operations on arrays.
- Fourier transforms and routines for shape manipulation.
- Operations related to linear algebra. NumPy has in-built functions for linear algebra and random number generation.
NumPy – A Replacement for MATLAB
NumPy is often used in conjunction with packages like SciPy (Scientific Python) and Matplotlib (plotting library). This combination is widely used as a replacement for MATLAB, a popular platform for technical computing. However, Python alternative to MATLAB is now seen as a more modern and complete programming language.
One of the significant advantages of NumPy is that it is open-source, making it freely accessible to anyone.
Why is NumPy Faster Than Lists?
NumPy arrays are significantly faster than Python lists for several reasons −
Aspect | NumPy | List |
---|---|---|
Memory Storage | NumPy uses a contiguous block of memory, which improves cache efficiency and access speed. | Python lists consist of pointers to objects, leading to more memory fragmentation and slower access. |
Data Types | NumPy supports homogeneous data types (all elements are of the same type), leading to more efficient memory use. | Python lists can contain heterogeneous data types (elements can be of different types), resulting in higher memory overhead. |
Operations | NumPy uses vector operations that leverage SIMD (Single Instruction, Multiple Data) for parallel processing. | Python lists rely on loop-based operations, which are slower due to the overhead of Python’s interpreted nature. |
Efficiency | NumPy is written in C and optimized for performance, reducing the execution time of numerical operations. | Python lists are executed as Python byte-code, which is generally slower compared to compiled C code. |
Memory Usage | NumPy requires less memory due to fixed data types and contiguous storage. | Python lists use more memory because each element is a separate Python object with additional overhead. |
Broadcasting | NumPy supports broadcasting, allowing operations on arrays of different shapes without creating additional copies. | Python lists do not support broadcasting, making element-wise operations less efficient. |
Performance | Better cache utilization due to contiguous memory storage, leading to faster access and processing. | Poor cache utilization because of scattered memory allocation, slowing down access. |
Functionality | NumPy provides a rich set of mathematical functions and tools optimized for array operations. | Python lists are Limited to basic operations and lack advanced mathematical capabilities. |
Which Language is NumPy written in?
NumPy is primarily written in the following languages −
- C: The core functionality of NumPy, including the implementation of array objects and basic operations, is written in C. This provides the high performance and efficiency that NumPy is known for.
- Python: NumPy’s user interface and high-level functionalities are written in Python. This makes it easy to use and integrate with other Python libraries.
- Fortran : Some of the numerical routines in NumPy, especially those related to linear algebra (like LAPACK and BLAS), are written in Fortran. Fortran is known for its efficiency in numerical computing, which enhances NumPy’s performance for specific types of operations.