2016-04-10 52 views
2

我想用kmeans来聚类我的单词向量,如here所述。 我使用Kmeans fit_predict with word2vec

# 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) 

的代码片段,我收到以下错误 类型错误:“浮动”对象不能被解释为一个整数

可能有人请帮助

回答

2

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

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

+0

UGGGGH!谢谢!我觉得很蠢,没有发现我自己的代码中出现错误的原因:P –

+0

没有。如果我们承认,学习并向前推进,那么错误就会发生并且没关系(不是愚蠢的):D – LGG

+1

您也可以使用wordVectors.shape [0] // 5。 //运算符强制除法结果为int – icaro56