Unlocking NumPy's Potential: 5 Underrated Functions to Explore
Written on
Introduction
NumPy is an essential library in Python for scientific computing, offering robust support for multidimensional arrays, mathematical computations, and linear algebra. While it enjoys widespread use, some of its lesser-known features remain underappreciated. This article will highlight five underrated NumPy functions that are worth trying out.
1. Using np.clip for Value Limitation
The clip() function is a handy tool for constraining the values within an array to a defined minimum and maximum. It requires three parameters: the target array, a minimum threshold, and a maximum threshold. Any values falling below the minimum are replaced with the minimum value, and those exceeding the maximum are capped at the maximum value.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.clip(arr, 3, 7)
Output:
array([3, 3, 3, 4, 5, 6, 7, 7, 7])
2. Finding Indices with np.where
The where() function is useful for identifying the indices of elements in an array that satisfy a specific condition. It takes a boolean condition as input and can return different values based on whether that condition is met or not. If the alternative values are omitted, it simply returns a tuple of arrays containing the indices of the elements that meet the condition.
import numpy as np
arr = np.array([1, 2, 3, 4, 5, 6, 7, 8, 9])
np.where(arr > 5)
Output:
(array([5, 6, 7, 8]),)
3. Extracting Unique Elements with np.unique
The unique() function retrieves the distinct elements from an array. It accepts a single array as an argument and returns the unique values sorted in ascending order.
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
np.unique(arr)
Output:
array([1, 2, 3, 4])
4. Counting Occurrences with np.bincount
The bincount() function counts how many times each integer appears in an array of non-negative integers. It takes one input: an array consisting of non-negative integers. The output is an array that enumerates the occurrences of each integer from the input.
import numpy as np
arr = np.array([1, 2, 2, 3, 3, 3, 4, 4, 4, 4])
np.bincount(arr)
Output:
array([0, 1, 2, 3, 4])
5. Applying Functions with np.vectorize
The vectorize() function allows you to apply a specific function to every element in an array. You need to provide a function that accepts a scalar input and returns a scalar output. The resulting function can then be used to apply the initial function to each element of the target array.
import numpy as np
def add_five(x):
return x + 5
arr = np.array([1, 2, 3, 4, 5])
np.vectorize(add_five)(arr)
Output:
array([ 6, 7, 8, 9, 10])
Learn More with These Videos
To deepen your understanding of these NumPy functions and more, check out the following videos:
Discover innovative techniques with NumPy to enhance your data analysis skills.
The ultimate guide to learning NumPy in 2024, perfect for beginners.
Connect with Moez Ali
Moez Ali is a visionary in technology and innovation. Transitioning from a data scientist to a product manager, he is committed to developing cutting-edge data products and fostering vibrant open-source communities. As the creator of PyCaret, he has over 100 publications with more than 500 citations and is recognized globally for his contributions to Python.
Stay connected with Moez through:
- Medium
- YouTube
For insights into his open-source initiatives, explore PyCaret on GitHub or follow the official PyCaret LinkedIn page. Don’t miss his presentation on Time Series Forecasting with PyCaret at the DATA+AI SUMMIT 2022 by Databricks.