Find the nearest value and the index of NumPy Array Last Updated : 30 Sep, 2022 Comments Improve Suggest changes Like Article Like Report In this article, let's discuss finding the nearest value and the index in an array with Numpy. We will make use of two of the functions provided by the NumPy library to calculate the nearest value and the index in the array. Those two functions are numpy.abs() and numpy.argmin(). Example Input Array: [12 40 65 78 10 99 30] Nearest value is to be found: 85 Nearest values: 78 Index of nearest value: 3Approach to Find the nearest value and the index of NumPy ArrayTake an array, say, arr[] and an element, say x to which we have to find the nearest value.Call the numpy.abs(d) function, with d as the difference between the elements of array and x, and store the values in a different array, say difference_array[].The element, providing minimum difference will be the nearest to the specified value.Use numpy.argmin(), to obtain the index of the smallest element in difference_array[]. In the case of multiple minimum values, the first occurrence will be returned.Print the nearest element, and its index from the given array.Example 1: To find the nearest element to the specified value 85. We subtract the given value from each element of the array and store the absolute value in a different array. The minimum absolute difference will correspond to the nearest value to the given number. Thus, the index of minimum absolute difference is 3 and the element from the original array at index 3 is 78. Python3 import numpy as np # array arr = np.array([12, 40, 65, 78, 10, 99, 30]) print("Array is : ", arr) # element to which nearest value is to be found x = 85 print("Value to which nearest element is to be found: ", x) # calculate the difference array difference_array = np.absolute(arr-x) # find the index of minimum element from the array index = difference_array.argmin() print("Nearest element to the given values is : ", arr[index]) print("Index of nearest value is : ", index) Output: Example 2: In this example, The minimum absolute difference will correspond to the nearest value to the given number. Thus, the index of minimum absolute difference is 2 and the element from the original array at index 2 is 1. Thus, 1 is nearest to the given number i.e 2. Python3 import numpy as np # array arr = np.array([8, 7, 1, 5, 3, 4]) print("Array is : ", arr) # element to which nearest value is to be found x = 2 print("Value to which nearest element is to be found: ", x) # calculate the difference array difference_array = np.absolute(arr-x) # find the index of minimum element from the array index = difference_array.argmin() print("Nearest element to the given values is : ", arr[index]) print("Index of nearest value is : ", index) Output: Comment More infoAdvertise with us Next Article Find the nearest value and the index of NumPy Array R rohanchopra96 Follow Improve Article Tags : Python Python-numpy Python numpy-Sorting Searching Practice Tags : python Similar Reads How to find the Index of value in Numpy Array ? In this article, we are going to find the index of the elements present in a Numpy array.Using where() Methodwhere() method is used to specify the index of a particular element specified in the condition.Syntax: numpy.where(condition[, x, y])Example 1: Get index positions of a given valueHere, we fi 5 min read Finding the k smallest values of a NumPy array In this article, let us see how to find the k number of the smallest values from a NumPy array. Examples: Input: [1,3,5,2,4,6] k = 3 Output: [1,2,3] Method 1: Using np.sort() . Approach: Create a NumPy array.Determine the value of k.Sort the array in ascending order using the sort() method.Print th 2 min read How to get the n-largest values of an array using NumPy? Let's see the program for how to get the n-largest values of an array using NumPy library. For getting n-largest values from a NumPy array we have to first sort the NumPy array using numpy.argsort() function of NumPy then applying slicing concept with negative indexing. Syntax: numpy.argsort(arr, ax 2 min read How to round elements of the NumPy array to the nearest integer? Prerequisites: Python NumPy In this article, let's discuss how to round elements of the NumPy array to the nearest integer. numpy.rint() function of Python that can convert the elements of an array to the nearest integer. Syntax: numpy.rint(x, /, out=None, *, where=True, casting='same_kind', order=' 1 min read Numpy: Index 3D array with index of last axis stored in 2D array NumPy, or Numerical Python, is a powerful library for efficient numerical computation in Python. One of the key features of NumPy is its ability to perform advanced indexing, which allows to access and manipulate specific elements of an array based on complex conditions. In this article, we will exp 5 min read Like