Python | Numpy np.digitize() method (original) (raw)
Last Updated : 02 Sep, 2024
With the help of np.digitize()
method, we can get the indices of the bins to which each value belongs in an array.
**Syntax :
np.digitize(Array, Bin, Right)
**Return : Return an array of indices of the bins.
**Example #1 :
In this example we can see that by using np.digitize()
method, we are able to get the array of indices of the bin of each value which belongs to an array by using this method.
Python3 1=1 `
import numpy
import numpy as np
a = np.array([1.2, 2.4, 3.6, 4.8]) bins = np.array([1.0, 1.3, 2.5, 4.0, 10.0])
using np.digitize() method
gfg = np.digitize(a, bins)
print(gfg)
`
**Output :
[1 2 3 4]
**Example #2 :
Python3 1=1 `
import numpy
import numpy as np
a = np.array([[10.2, 21.4, 3.6, 14.8], [1.0, 5.0, 10.0, 15.0]]) bins = np.array([1.0, 1.3, 2.5, 4.0, 10.0])
using np.digitize() method
gfg = np.digitize(a, bins)
print(gfg)
`
**Output :
[[5 5 3 5] [1 4 5 5]]
Similar Reads
- Python | numpy.fill_diagonal() method With the help of numpy.fill_diagonal() method, we can get filled the diagonals of numpy array with the value passed as the parameter in numpy.fill_diagonal() method. Syntax : numpy.fill_diagonal(array, value) Return : Return the filled value in the diagonal of an array. Example #1 : In this example 1 min read
- Pgmagick edge() method - Python The edge() function is an inbuilt function in the Pgmagick library which is used to apply a convolution filter to detect edges. Syntax: edge(radius) Parameters: This function accepts single parameter as mentioned above and defined below: radius: This parameter stores the aperture of detection filter 1 min read
- numpy.eye() in Python numpy.eye() is a function in the NumPy library that creates a 2D array with ones on the diagonal and zeros elsewhere. This function is often used to generate identity matrices with ones along the diagonal and zeros in all other positions. Let's understand with the help of an example: [GFGTABS] Pytho 2 min read
- Python | Numpy ndarray.__imod__() With the help of Numpy ndarray.__imod__(), every element in an array is operated on binary operator i.e mod(%). Remember we can use any type of values in an array and value for mod is applied as the parameter in ndarray.__imod__(). Syntax: ndarray.__imod__($self, value, /) Return: self%=value Exampl 1 min read
- numpy.atleast_2d() in Python numpy.atleast_2d() function is used when we want to Convert inputs to arrays with at least two dimension. Scalar and 1-dimensional inputs are converted to 2-dimensional arrays, whilst higher-dimensional inputs are preserved. Syntax : numpy.atleast_2d(*arrays) Parameters : arrays1, arrays2, ... : [ar 2 min read
- Python PIL | Image.draft() method PIL is the Python Imaging Library which provides the python interpreter with image editing capabilities. The Image module provides a class with the same name which is used to represent a PIL image. The module also provides a number of factory functions, including functions to load images from files, 2 min read
- numpy.atleast_1d() in Python numpy.atleast_1d()function is used when we want to Convert inputs to arrays with at least one dimension. Scalar inputs are converted to 1-dimensional arrays, whilst higher-dimensional inputs are preserved. Syntax : numpy.atleast_1d(*arrays) Parameters : arrays1, arrays2, ... : [array_like] One or mo 2 min read
- numpy.fromiter() function – Python NumPy's fromiter() function is a handy tool for creating a NumPy array from an iterable object. This iterable can be any Python object that provides elements one at a time. The function is especially useful when you need to convert data from a custom data source, like a file or generator, into a Num 2 min read
- Pgmagick equalize() method - Python The equalize() function is an inbuilt function in the Pgmagick library which is used to perform histogram equalization to the image. Syntax: equalize() Parameters: This function does not accept any parameters.Return Value: This function returns the Pgmagick object with image added. Input Image: Exam 1 min read
- numpy.multiply() in Python The numpy.multiply() is a numpy function in Python which is used to find element-wise multiplication of two arrays or scalar (single value). It returns the product of two input array element by element. Syntax: numpy.multiply(arr1, arr2, out=None, where=True, casting='same_kind', order='K', dtype=No 3 min read