Find the index of value in Numpy Array

In this tutorial, we are going to learn how to find the index of the value in Numpy Array in Python using two methods. We will initially apply the numpy.where() method on a 1D array and later on a 2D array. The output will result in finding all indices as well as the first index of the value in the Numpy array. Then we will use the argsort() method to find the first index of multiple values.

Find all indices of the value in the 1D Numpy Array

To find all indices of the value for a 1-dimensional array we will make use of the numpy.where() function. The numpy.where() function, in our case, takes the condition as a parameter. The function applies the condition to every element. It will return the indexes of all the elements whose value is equal to 2. The output is a ndarray representing the indices of all elements whose value is equal to 2.

import numpy as np

arr = np.array([2,1,3,2,4,5,4,3,2,9,10])

print("All indexes of value is equal to 2: ", np.where(arr == 2))

Output:

All indexes of value is equal to 2: (array([0, 3, 8]),)

Find the first index of the value in the 1D Numpy Array

To find the first index of the value in the 1-dimensional NumPy array, we will again use the numpy.where() function as mentioned above. The only difference is that we will apply indexing on the result of the where() function. We will choose the first array of the ndarray. Further, from the first array, we will choose the first index value. This will result in the first index of the value in the array.

import numpy as np

arr = np.array([2,1,3,2,4,5,4,3,2,9,10])

print("First index position of value is equal to 2: ",np.where(arr==2)[0][0])

Output:

First index position of value is equal to 2: 0

Find all the index of the value in 2D Numpy Array

For a 2-dimensional array, we will initially use the numpy.where() function to find all the indices of elements in the array whose value is equal to 8. The output will be 2 ndarrays, each representing the index of value 8 in each dimension. That is the first array will represent indices of value 8 in the horizontal dimension and the second array for indices in the vertical dimension.

Next, we will use the zip function to map pairs of values from each of the two arrays. This will give us a list of pairs of coordinates. We will iterate over this list using for loop to print each pair of coordinates giving the index position of value 8 in the 2D NumPy array.

import numpy as np
arr = np.array([[1, 2, 3],
                [8, 5, 6],
                [7, 8, 9],
                [10, 11, 8]])

res = np.where(arr == 8)

coordinates= list(zip(res[0], res[1]))

for pair in coordinates:
    print(pair)

Output:

(1, 0)
(2, 1)
(3, 2)

Find the first index position of multiple values in Numpy

To find the first index position of multiple values in Python, we use the numpy.argsort() function. This function will take the array to be sorted as an input. It will return the indices of the sorted array. Then we will pass the np.searchsorted() function inside the sorter array. The np.searchsorted() function takes the array, values, and sorter array as input. It will find the index value where the new elements should be inserted in the sorted array such that the order of the array is preserved. Thus, we will get the first index position of multiple values.

import numpy as np

arr = np.array([2,1,3,2,5,5,4,3,2,9,10])

values = np.array([3,5,4])

sorter = np.argsort(arr)
print(f"First index of 3,5, and 4 are:{sorter[np.searchsorted(arr, values, sorter=sorter)]}")

Output:

First index of 3,5, and 4 are:[2 4 6]

To learn more about Numpy array in Python click the following link: Basic NumPy array dimension visualization in Python

Leave a Reply

Your email address will not be published. Required fields are marked *