This text provides an overview of 45 essential NumPy methods for data manipulation and analysis, categorized into array creation, manipulation, mathematical operations, matrix and vector operations, sorting, searching, and statistical methods.
Abstract
The text is a comprehensive guide for users looking to master NumPy, a fundamental library for data science and machine learning in Python. It covers 45 essential methods divided into seven categories: array creation, manipulation, mathematical operations, matrix and vector operations, sorting, searching, and statistical methods. The methods are explained with examples and code snippets, making it easier for users to understand and implement them. The guide is based on the author's three years of experience using NumPy and aims to help users become proficient in using this powerful library.
Bullet points
Importing the NumPy library and setting the alias as np
Creating NumPy arrays from Python lists, zeros, ones, identity matrices, and equally spaced values
Generating random NumPy arrays with integers and floats
Converting Pandas series to NumPy arrays
Determining the shape of a NumPy array
Reshaping a NumPy array without changing its data
Transposing a NumPy array
Concatenating multiple NumPy arrays into one
Flattening a NumPy array into a single dimension
Finding unique elements in a NumPy array
Squeezing a NumPy array to remove axes of length one
Transforming a NumPy array into a Python list
Performing trigonometric functions, rounding functions, exponents, and logarithms on NumPy arrays
Calculating the sum, product, and square root of array elements
Calculating the dot product and matrix product of two NumPy arrays
Finding the vector norm of a NumPy array
Sorting a NumPy array in-place and finding the order of indices in a sorted NumPy array
Finding the indices corresponding to maximum and minimum values in a NumPy array
Searching for elements based on a condition and finding the indices of non-zero elements
Computing standard statistics such as mean, median, and standard deviation on NumPy arrays.
The Only 45 Methods You Should Master To Become A NumPy Pro
After using NumPy for over three years, here are the 45 methods I have used almost all the time
NumPy (or Numeric Python) sits at the core of every data science and machine learning project.
The whole data-driven ecosystem is in some way or the other dependent upon NumPy and its core functions. This makes it one of the most important and game-changing libraries ever built in Python.
Given that NumPy holds wide applicability in industry and academia due to its unparalleled potential, a firm acquaintance with its methods and syntax is an utmost necessity for python programmers.
However, if you are a newbie and trying to get a firm hold on the NumPy library, things can appear very daunting and overwhelming at first if you start with NumPy’s official documentation.
Having been there myself, this blog is intended to assist you in getting started with NumPy.
In other words, in this blog, I will reflect on my 3+ years of experience using NumPy and share those 45 specific methods that I have used almost all the time.
If you want to collapse the entire NumPy array into a single dimension, you can use the ndarray.flatten() method as shown below:
#19) Unique Elements of a NumPy Array
To determine the unique elements of a NumPy array, use the np.unique() method as demonstrated below:
#20) Squeeze a NumPy Array
If you want to remove axes of length one from your NumPy array, use the np.squeeze() method. This is illustrated below:
#21) Transform NumPy Array to Python List
To obtain a python list from a NumPy array, use the ndarry.tolist() method as shown below:
22–33) Mathematical Operations on NumPy Arrays
NumPy offers a wide variety of element-wise mathematical functions you can apply to NumPy arrays. You can read all the mathematical operations available here. Below, let’s discuss some of the most commonly used ones.
#22–24) Trigonometric Functions
#25–28) Rounding Functions
Return the element-wise floor using the np.floor() method.
Return the element-wise ceiling using the np.ceil() method.
Round to the nearest integer using the np.rint() method.
Round to a given number of decimals using the np.round_() method:
#29–30) Exponents and logarithms
Calculate the element-wise exponential using the np.exp() method.
Calculate the element-wise natural logarithm using the np.log() method.
#31–32) Sum and Product
Use the np.sum() method to calculate the sum of array elements:
Use the np.prod() method to calculate the product of array elements:
#33) Square Root
Use the np.sqrt() method to compute the square root of array elements:
34–36) Matrix and Vector Operations
#34) Dot Product
If you want to calculate the dot product of two NumPy arrays, use the np.dot() method:
#35) Matrix Product
To calculate the matrix product of two NumPy arrays, use thenp.matmul() or the @ operator in Python:
Note: The output of np.matmul() and np.dot() are the same in this case but they can differ significantly. You can read their differences here.
#36) Vector Norm
Vector norms represent a set of functions used to measure a vector’s length. I already have a post on vector norm, which you can read below: