How to get centroids from SciPy's hierarchical agglomerative clustering?

前端 未结 2 1355
我在风中等你
我在风中等你 2021-02-13 04:15

I am using SciPy\'s hierarchical agglomerative clustering methods to cluster a m x n matrix of features, but after the clustering is complete, I can\'t seem to figure out how to

2条回答
  •  甜味超标
    2021-02-13 05:10

    You can do something like this (D=number of dimensions):

    # Sum the vectors in each cluster
    lens = {}      # will contain the lengths for each cluster
    centroids = {} # will contain the centroids of each cluster
    for idx,clno in enumerate(T):
        centroids.setdefault(clno,np.zeros(D)) 
        centroids[clno] += features[idx,:]
        lens.setdefault(clno,0)
        lens[clno] += 1
    # Divide by number of observations in each cluster to get the centroid
    for clno in centroids:
        centroids[clno] /= float(lens[clno])
    

    This will give you a dictionary with cluster number as the key and the centroid of the specific cluster as the value.

提交回复
热议问题