KMeans聚类后的聚类点(scikit学习) [英] cluster points after KMeans clustering (scikit learn)

查看:259
本文介绍了KMeans聚类后的聚类点(scikit学习)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经使用sklearn使用Kmeans进行了聚类.尽管它有一种打印质心的方法,但我发现scikit-learn没有一种方法可以打印出每个聚类的聚类点(或者到目前为止我还没有看到),这真是令人惊讶.是否有一种巧妙的方法来获取每个群集的群集点?

I have done clustering using Kmeans using sklearn. While it has a method to print the centroids, I am finding it rather bizzare that scikit-learn doesn't have a method to print out the cluster-points of each cluster (or that I have not seen it so far). Is there a neat way to get the cluster-points of each cluster?

我目前有一个相当笨拙的代码来做,其中V是数据集:

I currently have this rather cludgy code to do it, where V is the dataset:

def getClusterPoints(V, labels):
    clusters = {}
    for l in range(0, max(labels)+1):
        data_points = []
        indices = [i for i, x in enumerate(labels) if x == l]
        for idx in indices:
            data_points.append(V[idx])
        clusters[l] = data_points
    return clusters

建议/链接非常感谢.

谢谢! PD.

推荐答案

例如

import numpy as np
from sklearn.cluster import KMeans
from sklearn import datasets

iris = datasets.load_iris()
X = iris.data
y = iris.target

estimator = KMeans(n_clusters=3)
estimator.fit(X)

您可以通过以下方式获得每个点的聚类

You can get clusters of each point by

estimator.labels_

出局:

array([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
   0, 0, 0, 0, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
   1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1, 1,
   2, 2, 2, 2, 1, 2, 1, 2, 1, 2, 2, 1, 1, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2,
   1, 2, 2, 2, 1, 2, 2, 2, 1, 2, 2, 1], dtype=int32)

然后获取每个聚类的点的索引

Then get the indices of points for each cluster

{i: np.where(estimator.labels_ == i)[0] for i in range(estimator.n_clusters)}

出局:

{0: array([ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
        17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33,
        34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49]),
 1: array([ 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]),
 2: array([ 52,  77, 100, 102, 103, 104, 105, 107, 108, 109, 110, 111, 112,
        115, 116, 117, 118, 120, 122, 124, 125, 128, 129, 130, 131, 132,
        134, 135, 136, 137, 139, 140, 141, 143, 144, 145, 147, 148])}

修改

如果要将X中的点数组用作值而不是索引数组:

If you want to use array of points in X as values rather than the array of indices:

{i: X[np.where(estimator.labels_ == i)] for i in range(estimator.n_clusters)}

这篇关于KMeans聚类后的聚类点(scikit学习)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆