【问题标题】:Find length of cluster (how many point associated with cluster) after KMeans clustering (scikit learn)在 KMeans 聚类(scikit learn)之后查找簇的长度(与簇关联的点数)
【发布时间】:2018-11-16 20:20:38
【问题描述】:

我已经使用 sklearn 使用 Kmeans 完成了聚类。虽然它有一种打印质心的方法,但我发现 scikit-learn 没有一种方法来找出簇长度(或者我到目前为止还没有看到它),这很奇怪。有没有一种简洁的方法来获取每个集群的集群长度或与集群相关的许多点?我目前有这个相当笨拙的代码来做它,我发现长度为一的集群,需要通过测量点之间的欧几里德距离来将其他点添加到这个集群中,并且必须更新标签

import numpy as np
from clustering.clusternew import Kmeans_clu
from evolution.generate import reproduction
from mapping.somnew import mapping, no_of_neurons, neuron_weights_init
from population_creation.population import pop_create
from New_SOL import newsol


data = genfromtxt('iris.csv', delimiter=',', skip_header=0, usecols=range(0, 4)) ##Read the input data
actual_label = genfromtxt('iris.csv', delimiter=',', dtype=str,skip_header=0, usecols=(4))
chromosome = int(input("Enter the number of chromosomes: "))  #Input the population size
max_gen = int(input("Enter the maximum number of generation: "))  #Input the maximum number of generation

for i in range(0, chromosome):
    cluster = 3#random.randint(2, max_cluster)  ##Randomly selects cluster number from 2 to root(poplation)
    K.insert(i, cluster)  ##Store the number of clusters in clu
    print('value of K is ',K)
    u, label,z1,A1= Kmeans_clu(cluster, data)
    #print("centers and labels : ", u, label)
    lab.insert(i, label)  ##Store the labels in lab
    center.insert(i, u) 
    new_center = pop_create(max_cluster, features, cluster, u)
    population.insert(i, new_center)
    print("VAlue of population in main\n" ,population)

newsol(max_gen,population,data)

对于 newsol 方法,我们从上述方法生成的代码中传递新种群,然后再次对种群进行 K-Means

def ClusterIndicesComp(clustNum, labels_array):   #list comprehension for accessing the features in iris data set 
    return np.array([i for i, x in enumerate(labels_array) if x == clustNum])

def newsol(max_gen,population,data):
    #print('VAlue of NewSol Population is',population)
    for i in range(max_gen):
        cluster1=5
        u,label,t,l=Kmeans_clu(cluster1, population)
        A1.insert(i,t)
        plab.insert(i,label)
        pcenter.insert(i,u)
        k2=Counter(l.labels_)  #Count number of elements in each cluster
        k1=[t for (t, v) in k2.items() if v == 1] #element whose length is one will be fetched 
        t1= np.array(k1) #Iterating through the cluster which have one point associated with them 
        for b in range(len(t1)):
            print("Value in NEW_SOL is of 1 length cluster\n",t1[b])
            plot1=data[ClusterIndicesComp(t1[b], l.labels_)]
            print("Values are in sol of plot1",plot1)
            for q in range(cluster1):
                plot2=data[ClusterIndicesComp(q, l.labels_)]
                print("VAlue of plot2 is for \n",q,plot2)
                for i in range(len(plot2)):#To get one element at a time from plot2
                    plotk=plot2[i]
                    if([t for (t, v) in k2.items() if v >2]):#checking if the cluster have more than 2 points than only the distance will be calculated 
                        S=np.linalg.norm(np.array(plot1) - np.array(plotk))
                        print("Distance between plot1 and plotk is",plot1,plotk,np.linalg.norm(np.array(plot1) - np.array(plotk)))#euclidian distance is calculated 
                    else:
                        print("NO distance between them\n")

我所做的Kmeans是

from sklearn.cluster import KMeans
import numpy as np 

def Kmeans_clu(K, data):

    kmeans = KMeans(n_clusters=K, init='random', max_iter=1, n_init=1).fit(data) ##Apply k-means clustering
    labels = kmeans.labels_
    clu_centres = kmeans.cluster_centers_
    z={i: np.where(kmeans.labels_ == i)[0] for i in range(kmeans.n_clusters)} #getting cluster for each label 

    return clu_centres, labels ,z,kmeans

【问题讨论】:

    标签: python machine-learning scikit-learn k-means unsupervised-learning


    【解决方案1】:

    要获取每个集群中的实例数,您可以尝试使用Counter

    from collections import Counter, defaultdict
    print(Counter(estimator.labels_))
    

    结果:

    Counter({0: 62, 1: 50, 2: 38})
    

    其中集群 0 有 62 个实例,集群 1 有 50 个实例,集群 2 有 38 个实例

    并且可能要存储每个集群的实例索引,可以使用defaultdict

    clusters_indices = defaultdict(list)
    for index, c  in enumerate(estimator.labels_):
        clusters_indices[c].append(index)
    

    现在,要查找集群 0 中实例的索引,请调用:

    print(clusters_indices[0])
    

    结果:

    [50, 51, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 
     71, 72, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92,
     93, 94, 95, 96, 97, 98, 99, 101, 106, 113, 114, 119, 121, 123, 126, 127, 133, 138, 142, 146, 149]
    

    【讨论】:

    • 您的代码是正确的,您可以帮助找到集群中所有解决方案的欧几里德距离,并且最近的解决方案将被添加到长度为 1 的集群中,这样它的长度就不是一个。提前致谢
    • 也许您可以更新问题,尝试对具有预期输出的新要求进行一些尝试。
    • 我已经编辑了更改。请查看我的整个代码并帮助我。提前致谢
    • 对不起!我不认为我完全理解它。我赞成这个问题,希望有人能给出答案。
    猜你喜欢
    • 2015-11-20
    • 2015-02-20
    • 1970-01-01
    • 2016-11-16
    • 2019-11-06
    • 1970-01-01
    • 2019-11-29
    • 2014-05-09
    • 2014-02-20
    相关资源
    最近更新 更多