Performing Math Operations in Python: NumPy

Search

Table of Contents

NumPy is an essential and widely used library in the data science area that provides data manipulation and analysis functionality. Since its initial release, various projects employ this library since it allows programmers to perform combined operations through tools like random number generators, linear algebra routines, Fourier transforms, array handling, and more.

It is accessible and practical for programmers from any background or experience level. It is accessible and practical for programmers from any background or experience level. In this way, Numpy contributes to the easy adoption and transition of those who need to become more familiar with this language. From an overall point of view, the primary use cases of NumPy are:

  • Math Operations: NumPy offers comprehensive mathematical functions, making it useful for numerical integration, differentiation, polynomial regression, and more.
  • Image Processing: NumPy allows to perform a variety of operations with images. Examples include image manipulation (cropping, resizing, rotating, and flipping), filtering, converting to a binary image, color space conversion, and more.
  • Machine Learning:  When designing Machine Learning (ML) algorithms, it is common to work with arrays for complex math operations such as multiplication, inversion, decomposition, Fourier analysis, gradient computation, and calculus. In this context, arrays store data and parameters, as they are suitable for efficiently handling large amounts of numeric data.

What Makes NumPy Unique Compared to MATLAB

NumPy and MATLAB are both featured tools for numerical computing and data manipulation. Both libraries provide functionality for working with data arrays. They use similar syntax and conventions for array operations, making it easy for users to switch between them. But despite these similarities, NumPy excels in other ways. 

For example, numpy.resize(a, new_shape) allows Devs to change the array size at runtime. You can resize an array to a new shape while preserving the existing data.

NumPy offers several advantages over MATLAB, such as being open-source, supporting a wide range of data types, and having a strong community of developers, users, and contributors. On the other hand, GNU Octave is the open-source version of MATLAB but with fewer features.

In addition, as it is built on the top of Python, NumPy runs over several environments (e.g., CONDA, web browser),  Operating Systems (e.g., Windows, Linux, and macOS) and CPU architectures. NumPy can work together with many available libraries that are constantly updated and improved.

In sum, NumPy is a powerful Python numerical computing library that provides a similar interface to other numerical software like IDL, MATLAB, or Yorick, but it also offers functionality for working with arrays of homogeneous data, making it an essential tool for data scientists and engineers.

Projects Built on Top of NumPY

NumPy’s ndarray objects provide a high-level API for working with array-structured data and a strided in-RAM strategy storage. However, this API has limitations as data sets continue to grow and new environments and architectures emerge, leading developers to execute projects built on top of the NumPy API:

  • XArray: It aims to extend the capabilities of NumPy’s ndarray to provide a more flexible and intuitive interface for working with multi-dimensional arrays, focusing on labeled arrays and the ability to work with both in-memory and out-of-memory datasets.
  • JAX: It is a popular open-source library that combines the functionality of Autograd, and XLA, which are libraries for function differentiation and for compiling and running Machine Learning (ML) operations on GPUs and TPUs.
  • Numpy.ma: This module provides a convenient way to handle missing or invalid data in arrays by treating them as separate entities rather than replacing them with a particular value such as NaN (Not a Number). It also enables more accurate data analysis and calculations.
  • Astropy.units: It is part of the Astropy project, a collection of packages for astronomy and astrophysics. The astropy.units library provides a convenient way to attach units to numbers, perform mathematical operations with them, and ensure that the units are consistent throughout the calculations.
  • SciPy: It provides functionality for tasks such as optimization, interpolation, integration, signal and image processing. It also provides interfaces to other libraries such as BLAS, LAPACK, and ARPACK, as well as for Matplotlib and SymPy.
  • Scikit Learn: It is a wide-use library for ML projects that provides classification, regression, clustering, and model selection tools. It also contains algorithms for performing linear and logistic regression, k-means clustering, decision trees, random forest, gradient boosting, and more.
  • Pandas: It is a well-known library that is built on top of NumPy. Pandas provides functionalities for reading and writing data in various formats, handling missing data, handling date-time data, merging, joining and reshaping data, grouping and aggregation, and data filtering.

Importing NumPy

The import statement is the most common way to invoke a library, but it’s not the only one. Devs also use importlib.import_module() and built-in __import__() statements. To import the NumPy library, you just have to type:

import numpy as np
The import statement for NumPy library. Image by the author.

Note: You must install this library when using Jupyter Notebook. Other IDEs commonly do not require it. To install it on Jupyter Notebook, execute the following command on your PC.

!pip install numpy

Python List vs. NumPy Arrays

A list in Python is a general-purpose data structure (It supports different data types), while a NumPy array is a specialized data structure for numerical operations and is more efficient for such operations.

my_list=[1,2,3,4]
print(my_list)
#R. [1, 2, 3, 4]

my_array=np.array([1,2,3,4])
print(my_array)
#R. [1 2 3 4]

Note in the examples above that, for arrays, the comma (,) does not separate the values, while lists do use it

For more information, check our blog post.

In sum

NumPy is a library that supports huge, multi-dimensional arrays and matrices and high-level mathematical functions that operate on these arrays. In addition, as Python is interpreted, NumPy provides functionality similar to MATLAB, allowing users to construct fast programs as long as most operations are performed on arrays or matrices rather than scalars.

Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Scroll to Top