scikit-learn - 限制 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]之间变化,以此类推。
解决方案
您需要使用名为 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
推荐阅读
- c++ - 从基本数据类型到用户定义类型的类型转换
- selenium - Chrome80 和 WDIO:Chrome 选项 --lang 在 ubuntu 上不起作用
- javascript - ReactJs mapStateToProps
- javascript - 为什么我的填充数组不能正常工作?
- reactjs - 窗口事件即使被删除也仍然存在
- php - 在 php 中的 if 语句中与赋值一起使用时通过或失败的返回类型的规则是什么?
- android - 自定义活动中的 Android Studio 操作栏
- google-apps-script - 为什么 Google 脚本对于每行的范围与列中的范围的运行方式不同
- java - 如何使用扫描仪将对象添加到数组列表?
- python-3.x - 一些数据没有在 Python Selenium 中被抓取,网络抓取?