KMeans聚类后的聚类点(scikit learn)
cluster points after KMeans clustering (scikit learn)
我已经使用 sklearn 使用 Kmeans 完成了聚类。虽然它有一种打印质心的方法,但我发现 scikit-learn 没有一种方法可以打印出每个集群的集群点(或者我到目前为止还没有看到它),这很奇怪。有没有一种巧妙的方法来获取每个集群的集群点?
我目前使用这个相当笨拙的代码来执行此操作,其中 V 是数据集:
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
Suggestions/links 非常感谢。
谢谢!
PD.
如果您阅读 documentation,您会发现 kmeans 具有 labels_
属性。此属性提供集群。
查看下面的完整示例:
import matplotlib.pyplot as plt
from sklearn.cluster import MiniBatchKMeans, KMeans
from sklearn.metrics.pairwise import pairwise_distances_argmin
from sklearn.datasets.samples_generator import make_blobs
import numpy as np
##############################################################################
# Generate sample data
np.random.seed(0)
batch_size = 45
centers = [[1, 1], [-1, -1], [1, -1]]
n_clusters = len(centers)
X, labels_true = make_blobs(n_samples=3000, centers=centers, cluster_std=0.7)
##############################################################################
# Compute clustering with Means
k_means = KMeans(init='k-means++', n_clusters=3, n_init=10)
k_means.fit(X)
##############################################################################
# Plot the results
for i in set(k_means.labels_):
index = k_means.labels_ == i
plt.plot(X[index,0], X[index,1], 'o')
plt.show()
例如
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)
你可以通过
得到每个点的聚类
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)
然后获取每个簇的点索引
{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
中的点数组而不是索引数组作为值:
{i: X[np.where(estimator.labels_ == i)] for i in range(estimator.n_clusters)}
我已经使用 sklearn 使用 Kmeans 完成了聚类。虽然它有一种打印质心的方法,但我发现 scikit-learn 没有一种方法可以打印出每个集群的集群点(或者我到目前为止还没有看到它),这很奇怪。有没有一种巧妙的方法来获取每个集群的集群点?
我目前使用这个相当笨拙的代码来执行此操作,其中 V 是数据集:
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
Suggestions/links 非常感谢。
谢谢! PD.
如果您阅读 documentation,您会发现 kmeans 具有 labels_
属性。此属性提供集群。
查看下面的完整示例:
import matplotlib.pyplot as plt
from sklearn.cluster import MiniBatchKMeans, KMeans
from sklearn.metrics.pairwise import pairwise_distances_argmin
from sklearn.datasets.samples_generator import make_blobs
import numpy as np
##############################################################################
# Generate sample data
np.random.seed(0)
batch_size = 45
centers = [[1, 1], [-1, -1], [1, -1]]
n_clusters = len(centers)
X, labels_true = make_blobs(n_samples=3000, centers=centers, cluster_std=0.7)
##############################################################################
# Compute clustering with Means
k_means = KMeans(init='k-means++', n_clusters=3, n_init=10)
k_means.fit(X)
##############################################################################
# Plot the results
for i in set(k_means.labels_):
index = k_means.labels_ == i
plt.plot(X[index,0], X[index,1], 'o')
plt.show()
例如
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)
你可以通过
得到每个点的聚类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)
然后获取每个簇的点索引
{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
中的点数组而不是索引数组作为值:
{i: X[np.where(estimator.labels_ == i)] for i in range(estimator.n_clusters)}