【问题标题】:How is to implement K-Means clustering algorithm with Cosine Distance measure?如何使用余弦距离度量实现 K-Means 聚类算法?
【发布时间】:2021-03-16 16:00:31
【问题描述】:

我需要运行 K-means 聚类算法来聚类文本数据,但使用余弦距离测量而不是欧几里得距离。在 python 中有任何可靠的实现吗?

编辑:

我尝试过如下使用 NLTK:

NUM_CLUSTERS=3
kclusterer = KMeansClusterer(NUM_CLUSTERS, distance= 
            nltk.cluster.util.cosine_distance, repeats=25)

clstr = kclusterer.cluster(X, clusters=False, trace=False)
print (clstr)

但它给了我错误:

TypeError: sparse matrix length is ambiguous; use getnnz() or shape[0]

这里的 X 是一个形状为 (15, 155) 的 TF-IDF 矩阵。

【问题讨论】:

    标签: python cluster-analysis k-means cosine-similarity


    【解决方案1】:

    您可以为此使用 NLTK。 NLTK 的 K-means 允许您指定要使用的距离度量。

    nltk

    【讨论】:

      【解决方案2】:

      如果你想自己做:https://stanford.edu/~cpiech/cs221/handouts/kmeans.html

      只需更改距离测量条目。距离测量在伪代码i的for循环中。

      【讨论】:

        猜你喜欢
        • 2020-03-10
        • 2014-02-02
        • 2017-06-25
        • 2011-09-04
        • 2017-04-07
        • 2019-06-01
        • 2021-05-12
        • 2011-07-24
        • 1970-01-01
        相关资源
        最近更新 更多