首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >星火MLib -从RDD[Vector]特性和RDD[Vector]标签创建RDD[Vector]

星火MLib -从RDD[Vector]特性和RDD[Vector]标签创建RDD[Vector]
EN

Stack Overflow用户
提问于 2016-03-11 21:36:00
回答 1查看 306关注 0票数 0

我正在构建一个培训集,使用两个代表文档和标签的文本文件。

Documents.txt

代码语言:javascript
复制
hello world
hello mars

Labels.txt

代码语言:javascript
复制
0
1

我已经读取了这些文件,并将我的文档数据转换为一个tf-idf加权term-document matrix,它表示为RDD[Vector]。我还为我的标签读取并创建了一个RDD[Vector]

代码语言:javascript
复制
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)

我想使用tfidflabs来创建一个RDD[LabeledPoint],但是我不知道如何使用两个不同的RDDs来应用映射。这是可能的/有效的吗?还是我需要重新考虑我的方法?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-03-11 22:10:22

处理这一问题的一种方法是基于索引进行join

代码语言:javascript
复制
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).values
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/35950374

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档