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 −

AspectNumPyList
Memory StorageNumPy 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 TypesNumPy 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.
OperationsNumPy 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.
EfficiencyNumPy 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 UsageNumPy 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.
BroadcastingNumPy 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.
PerformanceBetter cache utilization due to contiguous memory storage, leading to faster access and processing.Poor cache utilization because of scattered memory allocation, slowing down access.
FunctionalityNumPy 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.
© 2024 All rights reserved. | Made With 🤍 By The_MAK Team