The Hidden Gems of NumPy: 5 Lesser Known Functions You Need to Try
Discover the NumPy functions that will make your data analysis tasks a breeze
Introduction
NumPy is a fundamental package in Python for scientific computing that provides support for multidimensional arrays, mathematical operations, and linear algebra. Although NumPy is a popular package, there are a few hidden gems that are not commonly used. In this blog, we will explore five lesser-known functions of NumPy that you need to try.
1. np.clip
The clip() function is used to limit the values of an array between a specified minimum and maximum. It takes three arguments - an array, a minimum value, and a maximum value. The function replaces all values in the array that are less than the minimum with the minimum value and all values that are greater than the maximum with 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. np.where
- The
where()function returns the indices of elements in an array where a specified condition is true. It takes two arguments - a boolean condition and the values to return for the true and false elements. If the second and third arguments are not provided, it returns a tuple of arrays that contain the indices of true elements.
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. np.unique
The unique() function returns the unique elements in an array. It takes one argument - an array. The returned elements are 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. np.bincount
The bincount() function counts the occurrences of each integer in an array of non-negative integers. It takes one argument - an array of non-negative integers. The returned array contains the count of occurrences of each integer in the input array.
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. np.vectorize
The vectorize() function is used to apply a function to each element of an array. It takes one argument - a function that takes a scalar input and returns a scalar output. The returned function can be used to apply the input function to each element of an 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])Liked the blog? Connect with Moez Ali
Moez Ali is an innovator and technologist. A data scientist turned product manager dedicated to creating modern and cutting-edge data products and growing vibrant open-source communities around them.
Creator of PyCaret, 100+ publications with 500+ citations, keynote speaker and globally recognized for open-source contributions in Python.
Let’s be friends! connect with me:
👉 LinkedIn 👉 Twitter 👉 Medium 👉 YouTube
🔥 Check out my brand new personal website: https://www.moez.ai.
To learn more about my open-source work: PyCaret, you can check out this GitHub repo or you can follow PyCaret’s Official LinkedIn page.
Listen to my talk on Time Series Forecasting with PyCaret in DATA+AI SUMMIT 2022 by Databricks.






