closest value to a given value in a 1d numpy array

Numpy – Find the Closest Value in the Array

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?

closest value to a given value in a 1d 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 –

  1. Find the absolute difference of each value in the array ar with the given value k using the numpy.abs() function.
  2. The value in the array with the smallest absolute difference with k is the closest value you’re looking for. Use the numpy.ndarray.argmin() function to get the index of the value with the minimum absolute difference.
  3. 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.

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.

📚 Data Science Programs By Skill Level

Introductory

Intermediate ⭐⭐⭐

Advanced ⭐⭐⭐⭐⭐

🔎 Find Data Science Programs 👨‍💻 111,889 already enrolled

Disclaimer: Data Science Parichay is reader supported. When you purchase a course through a link on this site, we may earn a small commission at no additional cost to you. Earned commissions help support this website and its team of writers.

# 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 –


Subscribe to our newsletter for more informative guides and tutorials.
We do not spam and you can opt out any time.


Author

  • Piyush Raj

    Piyush is a data professional passionate about using data to understand things better and make informed decisions. He has experience working as a Data Scientist in the consulting domain and holds an engineering degree from IIT Roorkee. His hobbies include watching cricket, reading, and working on side projects.

Scroll to Top