在Sklearn中将KMeans标签转换为源数据

时间:2015-08-18 02:06:19

标签: numpy scikit-learn

import numpy as np
from sklearn.cluster import DBSCAN, MiniBatchKMeans
data = np.random.rand(5,5)
print data
km = MiniBatchKMeans(n_clusters=3, n_init=10, max_iter=5)
km.fit(data)
labels = km.labels_
print labels
[1 2 0 2 2]

如何使用标签重建数据?我的意思是让我的数据由每个像素中的标签组成。

1 个答案:

答案 0 :(得分:1)

如果您想进行1d聚类,请将数据重新整形为1d阵列,对点进行聚类,然后重新整形标签:

import numpy as np
from sklearn.cluster import MiniBatchKMeans

data = np.random.rand(5, 5)
data_to_cluster = np.reshape(data, (data.size, 1))

km = MiniBatchKMeans(n_clusters=3, n_init=10, max_iter=5)
km.fit(data_to_cluster)

labels = km.labels_
labels = np.reshape(labels, (5, 5))