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>You could partition the space so that you could ignore partitions wholly outside the radius for the point being tested.</p>

<p>You could also order the points by f, so you don't need to scan those with smaller values.</p>

python numpy scipy cluster-analysis