python - find 2d elements in a 3d array which are similar to 2d elements in another 3d array -


i have 2 3d arrays , want identify 2d elements in 1 array, have 1 or more similar counterparts in other array.

this works in python 3:

import numpy np import random  np.random.seed(123) = np.round(np.random.rand(25000,2,2),2) b = np.round(np.random.rand(25000,2,2),2)  a_index = np.zeros(a.shape[0])  in range(a.shape[0]):     b in range(b.shape[0]):         if np.allclose(a[a,:,:].reshape(-1, a.shape[1]), b[b,:,:].reshape(-1, b.shape[1]),                        rtol=1e-04, atol=1e-06):             a_index[a] = 1             break  np.nonzero(a_index)[0] 

but of course approach awfully slow. please tell me, there more efficient way (and is). thx.

you trying all-nearest-neighbor type query. has special o(n log n) algorithms, i'm not aware of python implementation. can use regular nearest-neighbor o(n log n) bit slower. example scipy.spatial.kdtree or ckdtree.

import numpy np import random np.random.seed(123) = np.round(np.random.rand(25000,2,2),2) b = np.round(np.random.rand(25000,2,2),2)  import scipy.spatial tree = scipy.spatial.ckdtree(a.reshape(25000, 4)) results = tree.query_ball_point(b.reshape(25000, 4), r=1e-04, p=1)  print [r r in results if r != []] # [[14252], [1972], [7108], [13369], [23171]] 

query_ball_point() not exact equivalent allclose() close enough, if don't care rtol parameter allclose(). choice of metric (p=1 city block, or p=2 euclidean).

p.s. consider using query_ball_tree() large data sets. both , b have indexed in case.

p.s. i'm not sure effect 2d-ness of elements should have; sample code gave treats them 1d , identical @ least when using city block metric.


Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -