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

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.

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

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

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

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

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.

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.

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

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.

In this deep learning project, you will build a classification system where to precisely identify human fitness activities.

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

Data Science Project in R -Build a machine learning algorithm to predict the future sale prices of homes.

In this data science project, we will predict the credit card fraud in the transactional dataset using some of the predictive models.

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.

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.