def to_codebook(X, part):
    """
    Calculates centroids according to flat cluster assignment
    Parameters
    ----------
    X : array, (n, d)
        The n original observations with d features
    part : array, (n)
        Partition vector. p[n]=c is the cluster assigned to observation n
    Returns
    -------
    codebook : array, (k, d)
        Returns a k x d codebook with k centroids
    """
codebook = []
for i in range(part.min(), part.max() + 1):
    codebook.append(X[part == i].mean(0))
return np.vstack(codebook)
Как получить центроиды из иерархической агломерационной кластеризации SciPy?