【发布时间】: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