Member-only story
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…