首页 > 解决方案 > 限制 scikit k 均值聚类中的质心运动

问题描述

我有一个用作初始质心的数组(3 个变量 A、B、C)。

X = np.array([[0.5, 0.1, 0.4],
              [0.7, 0.7, 0.3],
              [0.2,0.5,0.9]], np.float64)
clus = KMeans(n_clusters=3,init=X,n_init = 1).fit(data)
centers = clus.cluster_centers_
print centers

但是对于后续迭代,我想限制质心在一个范围之间的移动。例子:[0.5,0.1,0.4]只能在[0.4-0.6,0-0.2,0.3-0.5]之间变化,以此类推。

标签: scikit-learncluster-analysis

解决方案


您需要使用名为 COP-Kmeans 的约束 K-means 聚类(请参阅此处的论文)。

它是算法的不同实现,在 scikit-learn 上不可用。在这个 GitHub 存储库上有一个它的 python 实现。克隆 repo 后,它会以下列方式使用(取自“使用”部分):

run_ckm.py [-h] [--ofile OFILE] [--n_rep N_REP] [--m_iter M_ITER] [--tol TOL] dfile cfile k

论据如下:

dfile            data file
cfile            constraint file
k                number of clusters

还有一些与算法本身相关的可选参数:

--n_rep N_REP    number of times to repeat the algorithm
--m_iter M_ITER  maximum number of iterations of the main loop
--tol TOL        tolerance for deciding on convergence

最终,更元的论点,但也有用:

-h, --help       show this help message and exit
--ofile OFILE    file to store the output

推荐阅读