【问题标题】:Kmeans fit_predict with word2vecKmeans fit_predict 与 word2vec
【发布时间】:2016-07-31 05:38:45
【问题描述】:

我正在尝试使用 kmeans 对我的词向量进行聚类,如 here 所述。 我正在使用的代码sn-p

# Set "k" (num_clusters) to be 1/5th of the vocabulary size, or an
# average of 5 words per cluster
word_vectors = model.syn0
num_clusters = word_vectors.shape[0] / 5

# Initalize a k-means object and use it to extract centroids
kmeans_clustering = KMeans( n_clusters = num_clusters )
idx = kmeans_clustering.fit_predict( word_vectors )

我收到以下错误 TypeError: 'float' 对象不能被解释为整数

有人可以帮忙

【问题讨论】:

    标签: python-3.x k-means word2vec


    【解决方案1】:

    发现错误。簇数必须是整数所以我做了以下

    num_clusters = int(word_vectors.shape[0] / 5)

    【讨论】:

    • UGGGGH!谢谢!在我自己的代码中没有发现相同的错误原因,我感到很愚蠢:P
    • 不。只要我们承认,学习并继续前进,就会发生错误,这没关系(不是愚蠢):D
    • 你也可以使用 wordVectors.shape[0] // 5。 // 运算符强制除法结果为 int
    猜你喜欢
    • 2021-06-21
    • 2019-01-06
    • 2016-09-03
    • 1970-01-01
    • 2016-09-07
    • 2019-10-04
    • 2019-11-22
    • 2021-02-03
    • 2019-01-03
    相关资源
    最近更新 更多