Friday, December 27, 2024
Google search engine
HomeLanguagesPython | Numpy ndarray.item()

Python | Numpy ndarray.item()

With the help of numpy.ndarray.item() method, we can fetch the data elements that is found at the given index on numpy array. Remember we can give index as one dimensional parameter or can be two dimensional.

Parameters:
*args : Arguments (variable number and type)
-> none: This argument only works when size of an array is 1.
-> int_type: This argument is interpreted as a flat index into the array, specifying which element to return.
-> tuple of int_types: This argument is interpreted as a two dimensional array, by specifying which element to return.

Returns: Copy of an Item

Example #1 :
In this example we can see that by specifying the argument in ndarray.item() method, we can have the element if it existed on this index.




# import the important module in python
import numpy as np
       
# make an array with numpy
gfg = np.array([1, 2, 3, 4, 5])
       
# applying ndarray.item() method
print(gfg.item(2))


Output:

3

Example #2 :




# import the important module in python
import numpy as np
       
# make an array with numpy
gfg = np.array([[1, 2, 3, 4, 5],
                [6, 5, 4, 3, 2]])
       
# applying ndarray.item() method
print(gfg.item((1, 2)))


Output:

4
RELATED ARTICLES

Most Popular

Recent Comments