我正在构建一个培训集,使用两个代表文档和标签的文本文件。
Documents.txt
hello world
hello marsLabels.txt
0
1我已经读取了这些文件,并将我的文档数据转换为一个tf-idf加权term-document matrix,它表示为RDD[Vector]。我还为我的标签读取并创建了一个RDD[Vector]:
val docs: RDD[Seq[String]] = sc.textFile("Documents.txt").map(_.split(" ").toSeq)
val labs: RDD[Vector] = sc.textFile("Labels.txt")
.map(s => Vectors.dense(s.split(',').map(_.toDouble)))
val hashingTF = new HashingTF()
val tf: RDD[Vector] = hashingTF.transform(docs)
tf.cache()
val idf = new IDF(minDocFreq = 3).fit(tf)
val tfidf: RDD[Vector] = idf.transform(tf)我想使用tfidf和labs来创建一个RDD[LabeledPoint],但是我不知道如何使用两个不同的RDDs来应用映射。这是可能的/有效的吗?还是我需要重新考虑我的方法?
发布于 2016-03-11 22:10:22
处理这一问题的一种方法是基于索引进行join:
import org.apache.spark.RangePartitioner
// Add indices
val idfIndexed = idf.zipWithIndex.map(_.swap)
val labelsIndexed = labels.zipWithIndex.map(_.swap)
// Create range partitioner on larger RDD
val partitioner = new RangePartitioner(idfIndexed.partitions.size, idfIndexed)
// Join with custom partitioner
labelsIndexed.join(idfIndexed, partitioner).valueshttps://stackoverflow.com/questions/35950374
复制相似问题