Clustering points based on their function values and proximity
<p>I have many points <code>X</code> and their function values <code>f</code> stored in <code>numpy</code> arrays. I want to find <strong>all points</strong> in <code>X</code> that don't have a better point (smaller <code>f</code> value) within a distance <code>r</code>.</p>

<p><code>X</code> is hundreds of thousands of points, so I can't precompute <code>sp.spatial.distance.pdist(X)</code> but resort to the following:</p>

<pre><code>def cluster(X,f,r):
    pts,n = np.shape(X)
    centers = []
    for i in range(0,pts):
        pdist = sp.spatial.distance.cdist(X,[X[i]])
        if not np.any(np.logical_and(pdist &lt;= r, f &lt; f[i])):
            centers.append(i)
    return centers
</code></pre>

<p>This takes minutes. Is there a way to quickly cluster based on proximity and another metric?</p>

<p>I think one can sum this up as:</p>

<p>Use k-nearest neighbor to build a kdtree. Query the tree for points close to your query point with radius, check their function values.</p>

<pre><code>x=scipy.random.rand(10000,2) # sample data
f = exp(-x[:,0]**2) # sample function values
K=scipy.spatial.KDTree(x) # generate kdtree of data set
ix = K.query_point_ball(x[0],0.1) # query indices of points within 0.1 of x[0] in euclidean norm
# check f[ix] for your function criterion
</code></pre>

<p>You can query for all points at once if you are interested in that</p>

<pre><code>ix = K.query_point_ball(x,0.04)
</code></pre>

python numpy scipy cluster-analysis