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 value`k`

using the`numpy.abs()`

function. - 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. - 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** ⭐

- Harvard University Data Science: Learn R Basics for Data Science
- Standford University Data Science: Introduction to Machine Learning
- UC Davis Data Science: Learn SQL Basics for Data Science
- IBM Data Science: Professional Certificate in Data Science
- IBM Data Analysis: Professional Certificate in Data Analytics
- Google Data Analysis: Professional Certificate in Data Analytics
- IBM Data Science: Professional Certificate in Python Data Science
- IBM Data Engineering Fundamentals: Python Basics for Data Science

**Intermediate ⭐⭐⭐**

- Harvard University Learning Python for Data Science: Introduction to Data Science with Python
- Harvard University Computer Science Courses: Using Python for Research
- IBM Python Data Science: Visualizing Data with Python
- DeepLearning.AI Data Science and Machine Learning: Deep Learning Specialization

**Advanced ⭐⭐⭐⭐⭐**

- UC San Diego Data Science: Python for Data Science
- UC San Diego Data Science: Probability and Statistics in Data Science using Python
- Google Data Analysis: Professional Certificate in Advanced Data Analytics
- MIT Statistics and Data Science: Machine Learning with Python - from Linear Models to Deep Learning
- MIT Statistics and Data Science: MicroMasters® Program in Statistics and Data Science

**🔎 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 –

- 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.**