如何使用pyspark

时间:2018-12-11 11:52:57

标签: python-3.x pyspark word2vec

我是pyspark的新手,我想对一组文档进行聚类(20个聚类)。为此,我需要执行以下操作:

  • 找到word2vec表示形式。
  • 将每个文档映射到一个矢量,该矢量是代表文档中单词的矢量的平均值。
  • 执行聚类。

因此我创建了word2vec表示形式,但是我没有设法使用spark将每个文档映射到其对应的矢量。

我尝试了以下操作:

from pyspark.mllib.feature import Word2Vec
word2vec = Word2Vec()
model = word2vec.fit(docs)
dic = model.getVectors()

import numpy as np
def calcAverage(x, dic):
    ans = np.zeros(100)
    for word in x: 
        if word in dic:
            ans += np.array(dic[word])
    return tuple(ans / len(x))

vectors = docs.map(lambda x: calcAverage(x, dic))
vectors.collect()

但是它不起作用,代码在以下行失败:vectors = docs.map(lambda x: calcAverage(x, dic)),有什么建议吗?

我遇到以下错误:

PicklingError: Could not serialize object: Py4JError: An error occurred while calling o73192.__getstate__. Trace:
py4j.Py4JException: Method __getstate__([]) does not exist
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:318)
    at py4j.reflection.ReflectionEngine.getMethod(ReflectionEngine.java:326)
    at py4j.Gateway.invoke(Gateway.java:274)
    at py4j.commands.AbstractCommand.invokeMethod(AbstractCommand.java:132)
    at py4j.commands.CallCommand.execute(CallCommand.java:79)
    at py4j.GatewayConnection.run(GatewayConnection.java:238)
    at java.lang.Thread.run(Thread.java:748)

0 个答案:

没有答案
相关问题