The Numpy library in Python comes with a number of useful methods and features to help wrangle data when working with arrays. In this tutorial, we will look at how to get the closest value in a Numpy array to a given value with the help of some examples.
How to get the closest value to a given value in a Numpy array?

You can use a combination of the Numpy’s abs()
and the argmin()
functions to find the closest value to a given value in a Numpy array.
Let’s say you want to find the closest value in the array ar
to a given value k
. You can use the following steps –
- Find the absolute difference of each value in the array
ar
with the given valuek
using thenumpy.abs()
function. - The value in the array with the smallest absolute difference with
k
is the closest value you’re looking for. Use thenumpy.ndarray.argmin()
function to get the index of the value with the minimum absolute difference. - Use the index from step 2 to find the closest value in the array
ar
.
Example
Let’s now look at a step-by-step example of using the above syntax.
First, we will create an input array ar
and a value k
that we will use throughout this tutorial.
Highlighted programs for you
Flatiron School
Flatiron School
University of Maryland Global Campus
University of Maryland Global Campus
Creighton University
Creighton University
import numpy as np # scaler value k = 4.3 # create numpy array ar = np.arange(1, 11) # display the array ar
Output:
array([ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10])
Here, we used the numpy.arange()
function to create a 1d array of integers from 1 to 10. We also created a variable k
that is set to 4.3 (an arbitrary value) for which we want to find the closest value in the array ar
.
Step 1 – Find the absolute difference with each value
First, we will compute the absolute difference of each value in the array with our given value k
. For this, we will use the numpy.abs()
function.
# get absolute diffence array ar_diff = np.abs(ar - k) # display the resulting array ar_diff
Output:
array([3.3, 2.3, 1.3, 0.3, 0.7, 1.7, 2.7, 3.7, 4.7, 5.7])
We get a Numpy array containing the absolute difference of each value in the above array with k
.
Step 2 – Get the index of the smallest absolute difference
Use the numpy.ndarray.argmin()
function to get the index of the value with the smallest absolute difference. We’ll use this index in step 3.
# find index of min value in ar_diff index = ar_diff.argmin() # display the resulting index index
Output:
3
We get the index of the smallest absolute difference.
Step 3 – Find the closest value in the array using the index from step 2
The index of step 2 represents the index of the value having the smallest absolute difference with k
. We can use this index to identify the value in the array ar
that is closest to k
.
# closest value in ar to k ar[index]
Output:
4
We get the closest value to 4.3 in the array ar
as 4.
Summary
In this tutorial, we looked at the steps to find the closest value in a Numpy array to a given value. The steps (and code) mentioned in this tutorial can be summarized by the following block of code.
# index of value closest to k in ar index = np.abs(ar - k).argmin() # closest value closest_val = ar[index] print(closest_val)
Output:
4
You might also be interested in –
- Numpy – Get Min Value in Array
- Numpy – Get Absolute Value of Each Element
- Calculate Manhattan Distance in Python
Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.