【问题标题】:How to perform clustering on Word2Vec如何在 Word2Vec 上执行聚类
【发布时间】:2019-02-02 14:15:14
【问题描述】:

我有一个半结构化数据集,每一行都属于一个用户:

id, skills
0,"java, python, sql"
1,"java, python, spark, html"
2, "business management, communication"

为什么半结构化是因为以下技能只能从 580 个唯一值的列表中选择。

我的目标是聚集用户,或者根据相似的技能组合找到相似的用户。我曾尝试使用 Word2Vec 模型,这给了我很好的结果来识别相似的技能集 - 例如。

model.most_similar(["Data Science"])

给我 -

[('Data Mining', 0.9249375462532043),
 ('Data Visualization', 0.9111810922622681),
 ('Big Data', 0.8253220319747925),...

这为我提供了一个很好的模型来识别个人技能,而不是一组技能。如何利用 Word2Vec 模型提供的向量成功聚类相似用户组?

【问题讨论】:

标签: python nlp cluster-analysis data-mining word2vec


【解决方案1】:

您需要使用 Word2Vec 模型对字符串进行矢量化处理。 您可以这样实现:

model = KeyedVectors.load("path/to/your/model") 
w2v_vectors = model.wv.vectors # here you load vectors for each word in your model
w2v_indices = {word: model.wv.vocab[word].index for word in model.wv.vocab} # here you load indices - with whom you can find an index of the particular word in your model 

那么就可以这样使用is:

def vectorize(line): 
    words = []
    for word in line: # line - iterable, for example list of tokens 
        try:
            w2v_idx = w2v_indices[word]
        except KeyError: # if you does not have a vector for this word in your w2v model, continue 
            continue
        words.append(w2v_vectors[w2v_idx])
        if words: 
            words = np.asarray(words)
            min_vec = words.min(axis=0)
            max_vec = words.max(axis=0)
            return np.concatenate((min_vec, max_vec))
        if not words:
            return None 

然后您会收到一个向量,它代表您的行(文档等)。

收到每行的所有向量后,您需要进行聚类,您可以使用 sklearn 的 DBSCAN 进行聚类。

from sklearn.cluster import DBSCAN
dbscan = DBSCAN(metric='cosine', eps=0.07, min_samples=3) # you can change these parameters, given just for example 
cluster_labels = dbscan.fit_predict(X) # where X - is your matrix, where each row corresponds to one document (line) from the docs, you need to cluster 

祝你好运!

【讨论】:

  • 我真的很喜欢这种方法,它适用于使用有限的词汇集来聚类文档(这里:用户技能标签)。对未来的读者来说,请注意:对于由整个句子组成的文档,它对我来说根本不起作用:-(
  • 方法问题:是否需要向量化字符串/Word2Vec?你能根据它对技能和集群进行热编码吗?
  • @Dasha 获取min_vecmax_vec 并将它们连接起来的原因是什么?
  • @JeffParker TFIDF 也可以,但我相信矢量化可以让您使用相似的词而不是确切的词。
  • 我认为这里的vectorise(..) 存在错误,我们不应该检查if len(words): 而不是if words 并且在我们完成对所有word in line 的迭代之后吗?
猜你喜欢
  • 2019-01-06
  • 2017-10-02
  • 2019-09-18
  • 2021-12-13
  • 2018-01-07
  • 1970-01-01
  • 2019-09-04
  • 2014-04-12
  • 1970-01-01
相关资源
最近更新 更多