【问题标题】:How to find which text is close to the center of kmeans clusters如何找到靠近kmeans集群中心的文本
【发布时间】:2019-11-27 14:05:14
【问题描述】:

我有一个文本列表,我已经执行了tfidfkmeans 集群,如何访问最接近kmeans 集群中心的文本。

text=['this is text one','this is text two','this is text three',
     'thats are next','that are four','that are three',
     'lionel messi is footbal player','kobe bryant is basket ball player',
     'rossi is motogp racer']
Tfidf_vect = TfidfVectorizer(max_features=5000)
Tfidf_vect.fit(text)
cluster_text = Tfidf_vect.transform(text)
kmeans = KMeans(n_clusters=3, random_state=0,max_iter=600,n_init=10)
kmeans.fit(cluster_text)
labels = (kmeans.labels_)
center=kmeans.cluster_centers_

预期输出:

closest text to the center cluster 1=['this is text two','this is text three']
closest text to the center cluster 2=['that are three','that are four']
closest text to the center cluster 3=['rossi is motogp racer']

感谢您的帮助

【问题讨论】:

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


    【解决方案1】:

    您可以使用每个文本的 tfidf 表示与聚类中心之间的余弦相似度。试试这个!

    from sklearn.metrics import pairwise_distances
    
    distances = pairwise_distances(cluster_text, kmeans.cluster_centers_, 
                                   metric='cosine')
    
    ranking = np.argsort(distances, axis=0)
    
    df = pd.DataFrame({'text': text})
    for i in range(kmeans.n_clusters):
        df['cluster_{}'.format(i)] = ranking[:,i]
    
    top_n = 2
    
    for i in range(kmeans.n_clusters):
        print('top_{} closest text to the cluster {} :'.format(top_n, i))
        print(df.nsmallest(top_n,'cluster_{}'.format(i))[['text']].values)
    
    top_2 closest text to the cluster 0 :
    [['that are four']
     ['that are three']]
    top_2 closest text to the cluster 1 :
    [['thats are next']
     ['that are four']]
    top_2 closest text to the cluster 2 :
    [['this is text three']
     ['this is text two']]
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-14
      • 2011-01-20
      • 2018-10-28
      • 2019-04-04
      • 2014-11-07
      • 2013-09-10
      • 2017-06-01
      • 2020-05-26
      相关资源
      最近更新 更多