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

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

##### Data Science Project - Instacart Market Basket Analysis
Data Science Project - Build a recommendation engine which will predict the products to be purchased by an Instacart consumer again.

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

##### Identifying Product Bundles from Sales Data Using R Language
In this data science project in R, we are going to talk about subjective segmentation which is a clustering technique to find out product bundles in sales data.

##### Human Activity Recognition Using Multiclass Classification in Python
In this human activity recognition project, we use multiclass classification machine learning techniques to analyse fitness dataset from a smartphone tracker.

##### Predict Churn for a Telecom company using Logistic Regression
Machine Learning Project in R- Predict the customer churn of telecom sector and find out the key drivers that lead to churn. Learn how the logistic regression model using R can be used to identify the customer churn in telecom dataset.

##### Ecommerce product reviews - Pairwise ranking and sentiment analysis
This project analyzes a dataset containing ecommerce product reviews. The goal is to use machine learning models to perform sentiment analysis on product reviews and rank them based on relevance. Reviews play a key role in product recommendation systems.

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

##### German Credit Dataset Analysis to Classify Loan Applications
In this data science project, you will work with German credit dataset using classification techniques like Decision Tree, Neural Networks etc to classify loan applications using R.

##### Perform Time series modelling using Facebook Prophet
In this project, we are going to talk about Time Series Forecasting to predict the electricity requirement for a particular house using Prophet.

##### Solving Multiple Classification use cases Using H2O
In this project, we are going to talk about H2O and functionality in terms of building Machine Learning models.