Find the nearest value from a given value in an array?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

Find the nearest value from a given value in an array?

Find the nearest value from a given value in an array?

Find the nearest value from a given value in an array

0

Recipe Objective

While handling arrays, we might need to find the value that matches closest to any element in our given array.

So this recipe is a short example on how to find the nearest value from a given value in an array. Let's get started.

Step 1 - Import the library

import numpy as np

Let's pause and look at these imports. Numpy is generally helpful in data manipulation while working with arrays. It also helps in performing mathematical operation.

Step 2 - Defining find_nearest function

def find_nearest(array, value): idx = (np.abs(array - value)).argmin() return array[idx]

We have a defined find_nearest function. It first subtracts the given value from all the elements of array. Then, finds the argument of closest value by taking the absolute value of all elements.

Step 3 - Defining a random array and find the nearest value in our array

array = np.random.random(10) print(array) value = 0.5 print(find_nearest(array, value))

Here we define a random array and thereby find the closest value to 0.5

Step 4 - Lets look at our dataset now

Once we run the above code snippet, we will see:

[0.91099284 0.4100222  0.26885347 0.0256024  0.10999511 0.11454188
 0.79332232 0.71090965 0.3848031  0.82222392]
0.4100222047688208

Relevant Projects

Machine Learning or Predictive Models in IoT - Energy Prediction Use Case
In this machine learning and IoT project, we are going to test out the experimental data using various predictive models and train the models and break the energy usage.

Machine Learning project for Retail Price Optimization
In this machine learning pricing project, we implement a retail price optimization algorithm using regression trees. This is one of the first steps to building a dynamic pricing model.

Predict Employee Computer Access Needs in Python
Data Science Project in Python- Given his or her job role, predict employee access needs using amazon employee database.

Customer Market Basket Analysis using Apriori and Fpgrowth algorithms
In this data science project, you will learn how to perform market basket analysis with the application of Apriori and FP growth algorithms based on the concept of association rule learning.

Human Activity Recognition Using Smartphones Data Set
In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

PySpark Tutorial - Learn to use Apache Spark with Python
PySpark Project-Get a handle on using Python with Spark through this hands-on data processing spark python tutorial.

Zillow’s Home Value Prediction (Zestimate)
Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

Credit Card Fraud Detection as a Classification Problem
In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

Choosing the right Time Series Forecasting Methods
There are different time series forecasting methods to forecast stock price, demand etc. In this machine learning project, you will learn to determine which forecasting method to be used when and how to apply with time series forecasting example.

Walmart Sales Forecasting Data Science Project
Data Science Project in R-Predict the sales for each department using historical markdown data from the Walmart dataset containing data of 45 Walmart stores.