Find nearest value in numpy array

后端 未结 16 1637
被撕碎了的回忆
被撕碎了的回忆 2020-11-22 10:18

Is there a numpy-thonic way, e.g. function, to find the nearest value in an array?

Example:

np.find_nearest( array, value )
16条回答
  •  忘了有多久
    2020-11-22 10:50

    import numpy as np
    def find_nearest(array, value):
        array = np.array(array)
        z=np.abs(array-value)
        y= np.where(z == z.min())
        m=np.array(y)
        x=m[0,0]
        y=m[1,0]
        near_value=array[x,y]
    
        return near_value
    
    array =np.array([[60,200,30],[3,30,50],[20,1,-50],[20,-500,11]])
    print(array)
    value = 0
    print(find_nearest(array, value))
    

提交回复
热议问题