【问题标题】:Reconstructing k-means using pre-computed cluster centres使用预先计算的聚类中心重建 k-means
【发布时间】:2018-04-23 13:37:11
【问题描述】:

我使用 k-means 进行聚类,聚类数为 60。由于某些聚类的意义较小,因此我已从聚类中心数组 (count = 8) 中删除了这些聚类中心并保存在clean_cluster_array

这一次,我用init = clean_cluster_centers 重新拟合k-means 模型。和n_clusters = 52max_iter = 1 因为我想尽可能避免重新安装。

基本思想是用 clean_cluster_centers 重新创建新模型。这里的问题是,我们正在删除大量集群;即使使用n_iter = 1,该模型也会快速配置到更稳定的中心。有没有办法重新创建 k-means 模型?

【问题讨论】:

  • 你能展示一下 clean_cluster_array 的样子和 clusters_centers_ 的样子吗?
  • 我无法在此处显示,因为中心是形状 (52, 2E5)。他们以不同的方式回答您的问题。
  • @ValentinCalomme 我认为您的建议是在拟合模型后将clean_cluster_array 喂给clusters_centers_,这应该重写中心。虽然,我想完全绕过这个模型拟合。我会试试这个并发布结果。

标签: python-3.x scikit-learn k-means


【解决方案1】:

如果您安装了一个 KMeans 对象,它有一个 cluster_centers_ 属性。您可以通过执行以下操作直接更新它:

cls.cluster_centers_ = new_cluster_centers

因此,如果您想要一个具有干净集群中心的新对象,只需执行以下操作:

cls = KMeans().fit(X)
cls2 = cls.copy()
cls2.cluster_centers_ = new_cluster_centers

现在,由于 predict 函数只检查您的对象是否具有名为 cluster_centers_ 的非空属性,因此您可以使用 predict 函数

def predict(self, X):
    """Predict the closest cluster each sample in X belongs to.

    In the vector quantization literature, `cluster_centers_` is called
    the code book and each value returned by `predict` is the index of
    the closest code in the code book.

    Parameters
    ----------
    X : {array-like, sparse matrix}, shape = [n_samples, n_features]
        New data to predict.

    Returns
    -------
    labels : array, shape [n_samples,]
        Index of the cluster each sample belongs to.
    """
    check_is_fitted(self, 'cluster_centers_')

    X = self._check_test_data(X)
    x_squared_norms = row_norms(X, squared=True)
    return _labels_inertia(X, x_squared_norms, self.cluster_centers_)[0]

【讨论】:

    猜你喜欢
    • 2018-05-09
    • 2012-01-01
    • 2015-06-04
    • 2020-02-21
    • 2016-05-29
    • 1970-01-01
    • 2020-12-16
    • 1970-01-01
    • 2016-05-29
    相关资源
    最近更新 更多