# Spark ML 机器学习之Word2Vec

Word2Vec是一个 Estimator表示文档的单词序列并用于训练一个 Word2VecModel。该模型将每个单词映射到唯一的固定大小的向量。使用 Word2VecModel文档中所有单词的平均值将转换为向量；然后，可以将此向量用作预测，文档相似度计算等功能。

[En]

In the following code snippet, we start with a set of documents, each represented by a sequence of words. For each document, we convert it into a feature vector. The feature vector can then be passed to the learning algorithm.

import org.apache.spark.ml.feature.Word2Vec
import org.apache.spark.ml.linalg.Vector
import org.apache.spark.sql.Row

// Input data: Each row is a bag of words from a sentence or document.

val documentDF = spark.createDataFrame(Seq(
"Hi I heard about Spark".split(" "),
"I wish Java could use case classes".split(" "),
"Logistic regression models are neat".split(" ")
).map(Tuple1.apply)).toDF("text")

// Learn a mapping from words to Vectors.

val word2Vec = new Word2Vec()
.setInputCol("text")
.setOutputCol("result")
.setVectorSize(3)
.setMinCount(0)
val model = word2Vec.fit(documentDF)

val result = model.transform(documentDF)
result.collect().foreach { case Row(text: Seq[_], features: Vector) =>
println(s"Text: [${text.mkString(", ")}] => \nVector:$features\n") }


Original: https://www.cnblogs.com/yszd/p/13748359.html
Author: 云山之巅
Title: Spark ML 机器学习之Word2Vec

(0)