ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

如何找到有意义的单词来表示从word2vec向量派生的每个k-means集群?

2019-06-27 16:56:08  阅读:249  来源: 互联网

标签:python k-means gensim word2vec


我使用Python中的gensim包加载预先训练的Google word2vec数据集.然后,我想使用k-means在我的单词向量上找到有意义的聚类,并找到每个聚类的代表性单词.我正在考虑使用其对应的向量最接近群集质心的单词来表示该群集,但不知道这是否是一个好主意,因为我的实验没有给我带来好结果.

我的示例代码如下所示:

import gensim
import numpy as np
import pandas as pd
from sklearn.cluster import MiniBatchKMeans
from sklearn.metrics import pairwise_distances_argmin_min

model = gensim.models.KeyedVectors.load_word2vec_format('/home/Desktop/GoogleNews-vectors-negative300.bin', binary=True)  

K=3

words = ["ship", "car", "truck", "bus", "vehicle", "bike", "tractor", "boat",
       "apple", "banana", "fruit", "pear", "orange", "pineapple", "watermelon",
       "dog", "pig", "animal", "cat", "monkey", "snake", "tiger", "rat", "duck", "rabbit", "fox"]
NumOfWords = len(words)

# construct the n-dimentional array for input data, each row is a word vector
x = np.zeros((NumOfWords, model.vector_size))
for i in range(0, NumOfWords):
    x[i,]=model[words[i]] 

# train the k-means model
classifier = MiniBatchKMeans(n_clusters=K, random_state=1, max_iter=100)
classifier.fit(x)

# check whether the words are clustered correctly
print(classifier.predict(x))

# find the index and the distance of the closest points from x to each class centroid
close = pairwise_distances_argmin_min(classifier.cluster_centers_, x, metric='euclidean')
index_closest_points = close[0]
distance_closest_points = close[1]

for i in range(0, K):
    print("The closest word to the centroid of class {0} is {1}, the distance is {2}".format(i, words[index_closest_points[i]], distance_closest_points[i]))

输出如下:

[2 2 2 2 2 2 2 2 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0 0 0 0]
The closest word to the centroid of class 0 is rabbit, the distance is 1.578625818679259
The closest word to the centroid of class 1 is fruit, the distance is 1.8351978219013796
The closest word to the centroid of class 2 is car, the distance is 1.6586030662247868

在代码中我有3类词:车辆,水果和动物.从输出中我们可以看出k-means正确地聚类了所有3个类别的单词,但使用质心方法得出的代表性单词不是很好,因为对于0类我想看“动物”但它给出了“兔子” ,对于2级,我想看到“车辆”,但它返回“汽车”.

任何帮助或建议为每个群集找到良好的代表性词语将受到高度赞赏.

解决方法:

听起来你希望能够通过一个自动化过程找到集群中单词的通用术语 – 一种上位词,并且希望质心就是那个术语.

不幸的是,我没有看到任何声称word2vec结束这样安排的话.单词往往接近可以为其填充的其他单词 – 但实际上没有任何保证共享类型的单词比其他类型的单词更接近彼此,或者下位词往往是等距的他们的下位,等等. (当然,鉴于word2vec在类比解决中的成功,上位词往往会在各个类别的模糊相似方向上偏离它们的下位词.也就是说,大概是’大众汽车'(‘动物’ – ‘狗’)〜’汽车’ – 虽然我没有检查过.)

有时人们会对有关字向量的观察结果进行有趣的观察:对于具有更多弥散意义的词语 – 例如多重感官 – 的字词向量往往比原始形式的字母向量低于其他字词向量更奇异的意义.通常最相似的计算忽略了幅度,只是比较原始方向,但搜索更通用的术语可能希望偏向较低幅度的矢量.但这也只是猜测我没有检查过.

你可以查看有关自动上位词/下位词发现的工作,并且有可能word2vec向量可能是这种发现过程的一个促成因素 – 要么以正常方式训练,要么有一些新的皱纹试图强制所需的安排. (但是,开箱即用的gensim通常不支持这种专业化.)

通常会有一些文章改进word2vec培训过程,以使矢量更好地用于特定目的. Facebook Research最近发表的一篇似乎相关的论文是“Poincaré Embeddings for Learning Hierarchical Representations” – 它报告了更好的层次结构建模,特别是对WordNet的名词hypernym图的测试.

标签:python,k-means,gensim,word2vec
来源: https://codeday.me/bug/20190627/1306882.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有