Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

fnd closest element in array numpy

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

array = np.random.random(10)
print(array)
# [ 0.21069679  0.61290182  0.63425412  0.84635244  0.91599191  0.00213826
#   0.17104965  0.56874386  0.57319379  0.28719469]

value = 0.5

print(find_nearest(array, value))
# 0.568743859261
Comment

PREVIOUS NEXT
Code Example
Python :: how to change plot size in matplotlib 
Python :: matplotlib show grid for log or logit 
Python :: django app 
Python :: random numbers python 
Python :: how to make python turn a list into a text file grapper 
Python :: magic methods python 
Python :: how to ask a question in python 
Python :: python - count how many unique in a column 
Python :: python remove key from dict 
Python :: python timer decorator 
Python :: webdriver firefox install 
Python :: pil normalize image 
Python :: beautiful soup documentation 
Python :: python reverse 2d list 
Python :: tensor to int python 
Python :: django boilerplate command 
Python :: python iterate set 
Python :: http client post python 
Python :: how to send file in django response 
Python :: How do you print a integer in python 
Python :: python comment block 
Python :: how to make a distance function in python 
Python :: posted data to flask 
Python :: np one hot encoding 
Python :: python list splicing 
Python :: concat columns pandas dataframe 
Python :: python remove first substring from string 
Python :: print pretty in python 
Python :: write json pythonb 
Python :: python string: iterate string 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =