【发布时间】:2018-07-11 16:37:59
【问题描述】:
我的原始模式包含许多我想在 ML 模型中使用的映射类型,因此我需要将它们转换为 SparkML 稀疏向量。
root
|-- colA: map (nullable = true)
| |-- key: string
| |-- value: double (valueContainsNull = true)
|-- colB: map (nullable = true)
| |-- key: string
| |-- value: string (valueContainsNull = true)
|-- colC: map (nullable = true)
| |-- key: string
| |-- value: string (valueContainsNull = true)
上下文: SparkML 模型要求将数据形成为特征向量。有 一些用于生成特征向量的实用程序,但没有一个支持 maptype 类型。 例如 SparkML VectorAssembler 允许组合多个列(所有数字类型、布尔类型或向量类型)。
编辑:
到目前为止,我的解决方案是将地图单独分解为列,然后使用VectorAssembler:
val listkeysColA = df.select(explode($"colA"))
.select($"key").as[Int].distinct.collect.sorted
val exploded= df.select(listkeysColA.map(x =>
$"colA".getItem(x).alias(x.toString)): _*).na.fill(0)
val columnNames = exploded.columns
val assembler = new VectorAssembler().setInputCols(columnNames).setOutputCol("features")
编辑2:
我应该补充一点,我的地图中的数据非常稀疏,并且事先没有已知的键集。这就是为什么在我当前的解决方案中,我首先传递给数据以收集和排序键。然后我使用 getItem(keyName) 访问这些值。
【问题讨论】:
标签: scala apache-spark apache-spark-mllib apache-spark-ml