Python中文本聚类后的相同聚类

时间:2017-08-08 15:24:54

标签: python scikit-learn nlp nltk tf-idf

我在Python中对一组textdata执行文本聚类。基本上,我使用tf idf得分然后将结果矩阵应用到kmeans算法中:

vect = TfidfVectorizer(min_df=100,stop_words=sw) 

dtm = vect.fit_transform(df) 
l=vect.get_feature_names()

k = 15
model = MiniBatchKMeans(n_clusters=k)
model.fit(dtm)

order_centroids = model.cluster_centers_.argsort()[:, ::-1]
terms = vect.get_feature_names()
for i in range(k):
       print("Cluster %d:" % i, end='')
       for ind in order_centroids[i, :100]:
           print(' %s' % l[ind], end='')
       print()

然后,在执行以下操作后,我得到15个相同的群集(其中的词语几乎完全相同)。我也尝试使用LSA方法进行规范化,但它几乎相同。

我做错了什么以及如何解决?

1 个答案:

答案 0 :(得分:1)

我的猜测是你的功能没有标准化,这意味着dtm中的某些列包含的中心分布比其他分布更高。因此,用于提取与群集相关的功能的排序将错误地支持这些功能。

避免此类问题的常见做法是将zero meanunit variance的功能standardize改为:{/ p>

dtm_standardized = (dtm - dtm.mean(axis=0)) / dtm.std(axis=0)

或者像这样:

dtm_standardized = sklearn.preprocessing.scale(dtm)