python - Efficient lookup table for collection of numpy arrays -


i know what's efficient way create lookup table floats (and collection of floats) in python. since both sets , dicts need keys hashable, guess can't use sort of closeness check proximity inserted, can i? have seen this answer , it's not quite i'm looking don't want give burden of creating right key user , need extend collections of floats. example, given following code:

>>> import numpy np >>> = {np.array([0.01, 0.005]): 1} traceback (most recent call last):   file "<stdin>", line 1, in <module> typeerror: unhashable type: 'numpy.ndarray' >>> = {tuple(np.array([0.01, 0.005])): 1} >>> tuple(np.array([0.0100000000000001,0.0050002])) in false 

i last statement return true. coming c++ world, create std::map , provide compare function can comparison user defined tolerance check if values have been added data structure. of course question extends naturally lookup tables of arrays (for example numpy arrays). what's efficient way accomplish i'm looking for?

since interested in 3d points, think using data-structure optimized storing spatial data, such kd-tree. available in scipy , allows lookup of point closest given coordinate. after have looked point, check see if within tolerance accept new point or not.

usage should (untested, never used myself):

from scipy.spatial import kdtree points = ... # points [nx3] tree = kdtree(points)   new_point = ... # array of length 3 distance, nearest_index = tree.query(new_point) if distance > tolerance:  # add point     points = np.vstack((points, new_point))     tree = kdtree(points)  # generate tree scratch 

note kd-tree efficient looking point in static collection of points (cost of lookup o(log(n)), not optimized repeatedly adding new points. scipy implementation lacks method add new points, have generate new tree every time insert new point. since operation o(n*log(n)), might faster brute-force calculation of distances, costs o(n). note there alternative version ckdtree, might implemented in c speed, documentation not clear on this.


Comments

Popular posts from this blog

html - Firefox flex bug applied to buttons? -

html - Missing border-right in select on Firefox -

c# - two queries in same method -