在 Spark 中,如何将带有 SparseVector 的 DataFrame 转换为 RDD[Vector]?
In Spark, how to convert DataFrame with SparseVector into RDD[Vector]?
在 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 不知道如何编码自己的矢量数据类型。
只需在 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))
在 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 不知道如何编码自己的矢量数据类型。
只需在 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))