【问题标题】:In Spark, how to convert DataFrame with SparseVector into RDD[Vector]?在 Spark 中,如何将带有 SparseVector 的 DataFrame 转换为 RDD[Vector]?
【发布时间】:2018-03-23 17:11:56
【问题描述】:

this example 之后,我计算了一些文档的 TF-IDF 权重。现在我想使用RowMatrix 来计算文档相似度。但是我无法将数据拟合成正确的格式。我现在拥有的是一个 DataFrame,它的行有 (String,SparseVector) 作为两列的类型。我应该将其转换为RDD[Vector],我认为这很简单:

features.map(row => row.getAs[SparseVector](1)).rdd()

但我收到此错误:

<console>:58: error: Unable to find encoder for type stored in a
Dataset.  Primitive types (Int, String, etc) and Product types (case
classes) are supported by importing spark.implicits._  Support for 
serializing other types will be added in future releases.

导入spark.implicits._ 没有区别。

那么发生了什么?我很惊讶 Spark 不知道如何编码自己的向量数据类型。

【问题讨论】:

    标签: apache-spark apache-spark-mllib


    【解决方案1】:

    只需在map 之前转换为RDD

    import org.apache.spark.ml.linalg._
    
    val df = Seq((1, Vectors.sparse(1, Array(), Array()))).toDF
    
    df.rdd.map(row => row.getAs[Vector](1))
    

    【讨论】:

      猜你喜欢
      • 2017-06-02
      • 1970-01-01
      • 1970-01-01
      • 2015-10-28
      • 2017-05-13
      • 1970-01-01
      • 2015-12-21
      • 1970-01-01
      • 2016-06-28
      相关资源
      最近更新 更多