How to find the closest value to a given scalar in a vector?
MACHINE LEARNING RECIPES DATA CLEANING PYTHON DATA MUNGING PANDAS CHEATSHEET     ALL TAGS

How to find the closest value to a given scalar in a vector?

How to find the closest value to a given scalar in a vector?

This recipe helps you find the closest value to a given scalar in a vector

0

Recipe Objective

Suppose we have a scaler values of array. Now any irrational number comes and we have to find the nearest value corresponding to the given value.

So this recipe is a short example on how to find the closest value (to a given scalar) in a vector. 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 - Generating a random array

x = np.arange(100)

We have generated an array having 100 values from 0 to 99.

Step 3 - Generating a random values

a = np.random.uniform(0,100) print(a)

We have a genrated a random value between 0 to 100.

Step 4 - Printing the nearest value

index = (np.abs(x-a)).argmin() print(x[index])

Now using abs and argumin function, we have found the index corresponding to nearest integer. Finally, we have printed the value from the array corresponding to our found index.

Step 5 - Let's look at our dataset now

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

66.84178287734619
67

Relevant Projects

Music Recommendation System Project using Python and R
Machine Learning Project - Work with KKBOX's Music Recommendation System dataset to build the best music recommendation engine.

Mercari Price Suggestion Challenge Data Science Project
Data Science Project in Python- Build a machine learning algorithm that automatically suggests the right product prices.

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.

Predict Macro Economic Trends using Kaggle Financial Dataset
In this machine learning project, you will uncover the predictive value in an uncertain world by using various artificial intelligence, machine learning, advanced regression and feature transformation techniques.

Predict Credit Default | Give Me Some Credit Kaggle
In this data science project, you will predict borrowers chance of defaulting on credit loans by building a credit score prediction model.

Data Science Project-TalkingData AdTracking Fraud Detection
Machine Learning Project in R-Detect fraudulent click traffic for mobile app ads using R data science programming language.

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.

Forecast Inventory demand using historical sales data in R
In this machine learning project, you will develop a machine learning model to accurately forecast inventory demand based on historical sales data.

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.

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.