【问题标题】:Getting error no centroid defined for empty cluster as I try to create clusters当我尝试创建集群时出现错误,没有为空集群定义质心
【发布时间】:2019-03-21 17:20:56
【问题描述】:

我正在尝试使用 K-Means 对 2 个分布进行聚类,并使用余弦相似度作为定义相似度的指标。我写了以下代码。但它给出了一个错误说:

        Error: no centroid defined for empty cluster.
        Try setting argument 'avoid_empty_clusters' to True

我不明白这是为什么。我需要创建 2 个集群。

    import numpy as np
    from nltk.cluster.kmeans import KMeansClusterer
    import nltk as nltk

    np.random.seed(1)

    distr_1 = np.sin(2 * np.random.randn(100) + np.random.randn())
    distr_2 = (3 * np.random.randn(100)) + np.random.randn()

    x = list(range(0,100))

    X_train = np.concatenate((distr_1, distr_2))
    X_train = X_train.reshape(200,1)

    kclusterer = KMeansClusterer(2, distance=nltk.cluster.util.cosine_distance, repeats=100)
    assigned_clusters = kclusterer.cluster(X_train.flatten(), assign_clusters=True)

    print(assigned_clusters)

【问题讨论】:

    标签: python numpy nltk cluster-analysis


    【解决方案1】:

    由于糟糕的启动条件,可能会出现集群变空的情况。

    您的代码需要处理这种特殊情况。

    【讨论】:

      猜你喜欢
      • 2016-07-22
      • 2020-01-08
      • 2015-04-12
      • 1970-01-01
      • 1970-01-01
      • 2022-10-24
      • 1970-01-01
      • 2020-09-28
      • 2015-12-13
      相关资源
      最近更新 更多