arrays - Find the indices of the lowest closest neighbors between two lists in python -


given 2 numpy arrays of unequal size: (a presorted dataset) , b (a list of query values). want find closest "lower" neighbor in array each element of array b. example code below:

import numpy np  = np.array([0.456, 2.0, 2.948, 3.0, 7.0, 12.132]) #pre-sorted dataset b = np.array([1.1, 1.9, 2.1, 5.0, 7.0]) #query values, not sorted print a.searchsorted(b) # result:  [1 1 2 4 4] # desired: [0 0 1 3 4] 

in example, b[0]'s closest neighbors a[0] , a[1]. closest a[1], why searchsorted returns index 1 match, want lower neighbor @ index 0. same b[1:4], , b[4] should matched a[4] because both values identical.

i clunky this:

desired = [] b in b:     id = -1     in a:         if > b:             if id == -1:                 desired.append(0)             else:                 desired.append(id)             break          id+=1  print desired # result: [0, 0, 1, 3, 4] 

but there's gotta prettier more concise way write numpy. i'd keep solution in numpy because i'm dealing large data sets, i'm open other options.

you can introduce optional argument side , set 'right' mentioned in docs. then, subtract final indices result 1 desired output, -

a.searchsorted(b,side='right')-1 

sample run -

in [63]: out[63]: array([  0.456,   2.   ,   2.948,   3.   ,   7.   ,  12.132])  in [64]: b out[64]: array([ 1.1,  1.9,  2.1,  5. ,  7. ])  in [65]: a.searchsorted(b,side='right')-1 out[65]: array([0, 0, 1, 3, 4])  in [66]: a.searchsorted(a,side='right')-1 # out[66]: array([0, 1, 2, 3, 4, 5]) 

Comments

Popular posts from this blog

sql - VB.NET Operand type clash: date is incompatible with int error -

SVG stroke-linecap doesn't work for circles in Firefox? -

python - TypeError: Scalar value for argument 'color' is not numeric in openCV -