Get element closest to cluster centroid
<p>After clustering a distance matrix with <code>scipy.cluster.hierarchy.linkage</code>, and assigning each sample to a cluster using <code>scipy.cluster.hierarchy.cut_tree</code>, I would like to extract one element out of each cluster, which is the closest to that cluster's centroid.</p>

<ul>
<li>I would be the happiest if an off-the-shelf function existed for this, but in the lack thereof:</li>
<li>some suggestions were already proposed <a href="http://stackoverflow.com/questions/9362304/how-to-get-centroids-from-scipys-hierarchical-agglomerative-clustering">here</a> for extracting the centroids themselves, but not the closest-to-centroid elements.</li>
<li>Note that this is not to be confused with the <code>centroid</code> linkage rule in <code>scipy.cluster.hierarchy.linkage</code>. I have already carried out the clustering itself, just want to access the closest-to-centroid elements.</li>
</ul>

<p>Nearest neighbours are most efficiently computed using KD-Trees. E.g.:</p>

<pre><code>from scipy.spatial import cKDTree

def find_k_closest(centroids, data, k=1, distance_norm=2):
    """
    Arguments:
    ----------
        centroids: (M, d) ndarray
            M - number of clusters
            d - number of data dimensions
        data: (N, d) ndarray
            N - number of data points
        k: int (default 1)
            nearest neighbour to get
        distance_norm: int (default 2)
            1: Hamming distance (x+y)
            2: Euclidean distance (sqrt(x^2 + y^2))
            np.inf: maximum distance in any dimension (max((x,y)))

    Returns:
    -------
        indices: (M,) ndarray
        values: (M, d) ndarray
    """

    kdtree = cKDTree(data, leafsize=leafsize)
    distances, indices = kdtree.query(centroids, k, p=distance_norm)
    if k &gt; 1:
        indices = indices[:,-1]
    values = data[indices]
    return indices, values

indices, values = find_k_closest(centroids, data)
</code></pre>

python numpy scipy scikit-learn