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]
我如何使用标签重建我的数据?我的意思是让我的数据由每个像素的标签组成。
如果你想做一维聚类,那么重塑你的数据到一个一维数组,聚类点,然后重塑你的标签:
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))