pyspark Word2Vec

from pyspark.ml.feature import Word2Vec

from pyspark.sql import SparkSession

spark= SparkSession\
                .builder \
                .appName("dataFrame") \
                .getOrCreate()

# Input data: Each row is a bag of words from a sentence or document.
documentDF = spark.createDataFrame([
    ("Hi I heard about Spark".split(" "), ),
    ("I wish Java could use case classes".split(" "), ),
    ("Logistic regression models are neat".split(" "), )
], ["text"])

# Learn a mapping from words to Vectors.
word2Vec = Word2Vec(vectorSize=3, minCount=0, inputCol="text", outputCol="result")
model = word2Vec.fit(documentDF)

result = model.transform(documentDF)
for row in result.collect():
    text, vector = row
    print("Text: [%s] => \nVector: %s\n" % (", ".join(text), str(vector)))
Text: [Hi, I, heard, about, Spark] => 
Vector: [-0.05760560743510723,-0.03687768429517746,0.053699607402086263]

Text: [I, wish, Java, could, use, case, classes] => 
Vector: [-0.06942265214664595,-0.07444838913423674,0.029864686142121042]

Text: [Logistic, regression, models, are, neat] => 
Vector: [0.025776204053545373,0.06013465970754624,-0.0191410340834409]

Word2Vec数学原理

posted @ 2022-08-19 22:58  luoganttcc  阅读(5)  评论(0编辑  收藏  举报