O'Reilly logo

Machine Learning with Spark - Second Edition by Nick Pentreath, Manpreet Singh Ghotra, Rajdeep Dua

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Word2Vec with Spark MLlib on the 20 Newsgroups dataset

Training a Word2Vec model in Spark is relatively simple. We will pass in an RDD where each element is a sequence of terms. We can use the RDD of tokenized documents we have already created as input to the model.

object Word2VecMllib {  def main(args: Array[String]) {  val sc = new SparkContext("local[2]", "Word2Vector App")  val path = "./data/20news-bydate-train/alt.atheism/*"  val rdd = sc.wholeTextFiles(path)  val text = rdd.map { case (file, text) => text }  val newsgroups = rdd.map { case (file, text) =>                 file.split("/").takeRight(2).head }  val newsgroupsMap =           newsgroups.distinct.collect().zipWithIndex.toMap  val dim = math.pow(2, 18).toInt var tokens = text.map(doc => TFIDFExtraction.tokenize(doc)) ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required