[SciPy-User] KDTree count_neighbors

elmar elmar@net4werling...
Sun Jul 4 12:59:27 CDT 2010

Am 02.07.2010 22:35, schrieb Keith Goodman:
> Oh, that sounds interesting. So I build one tree for the data and
> another tree for the query points? (For each query point I want to
> find the k nearest neighbors in the data tree). What's the next step?

hi all,

thanks for help. Scipts is now working, but still needs some improvement.


from numpy import array, arange, zeros
from numpy.random import uniform
from scipy.spatial import KDTree
from matplotlib.pylab import scatter, contourf

# just to get a pattern
x = uniform(0, 360, 100)
y = uniform(0, 90, 100)
pattern = zip(x.ravel(), y.ravel())
tree_pattern = KDTree(pattern)

# background grid or reference points
neighbors_grid = zeros((360, 90))

# get number of neighbors within radius of r
r = 20
for i in range(360):
     for j in range(90):
         ref=array([[float(i), float(j)]])
         neighbors_grid[i][j] =
             KDTree.count_neighbors(tree_ref,tree_pattern, r)


More information about the SciPy-User mailing list