【问题标题】:Spark Scala VectorAssembler IllegalArgumentException: XX does not exist. Available: numberSpark Scala VectorAssembler IllegalArgumentException:XX 不存在。可用:数量
【发布时间】:2021-04-28 14:20:38
【问题描述】:

我在 Spark 上使用 Scala,我有这样的密集矩阵:

vp

res63: org.apache.spark.ml.linalg.DenseMatrix =

-0.26035262239241047 -0.9349256695883184 0.08719326360909431 -0.06393629243008418 0.006698866707269257 0.04124873027993731 0.011979122705128064 -0.005430767154896149 0.049075485175059275 0.04810618828561001 0.001605411530424612 0.015016736357799364 0.9587151228619724 -0.2534046936998956 -0.04668498310146597 0.06015550772431999 -0.022360873382091598 -0.22147143481166376 -0.014153052584280682 -0.025947327705852636

我想使用 VectorAssembler 创建特征列,所以我转换 vp:

import org.apache.spark.ml.feature.VectorAssembler
import org.apache.spark.ml.linalg.Vectors

val c = vp.toArray.toSeq
val vp_df = c.toDF("number")
vp_df.createOrReplaceTempView("vp_df")

val vp_list = vp_df.collect.map(_.toSeq).flatten
val vp_string = vp_list.map(_.toString)

vp_string

res64:数组[字符串] =阵列(-0.26035262239241047,0.08719326360909431,0.006698866707269257,0.011979122705128064,0.049075485175059275,0.001605411530424612,0.9587151228619724,-0.04668498310146597,-0.022360873382091598,-0.014153052584280682,-0.9349256695883184,-0.06393629243008418,0.04124873027993731,-0.005430767154896149,0.04810618828561001,0.015016736357799364 , -0.2534046936998956, 0.06015550772431999, -0.22147143481166376, -0.025947327705852636)

然后我使用 VectorAssembler:

val assembler = new VectorAssembler().setInputCols(vp_string).setOutputCol("features")

val output = assembler.transform(vp_df)
output.select("features").show(false)

但我有一个错误,我不明白为什么

IllegalArgumentException:-0.26035262239241047 不存在。可用:数量

我不知道这是怎么可能的,我已经多次完成了 AssemblerVector,这是我第一次看到这个

【问题讨论】:

  • 因为你对 VectorAssemblar 说要组装的列是列表的浮点数,我怀疑是否有一个名为 -0.26035262239241047 的列

标签: scala apache-spark runtime-error illegalargumentexception


【解决方案1】:

您的“数字”列已经在一个双精度数组中,因此您只需将此列转换为密集向量。

val arrayToVectorUDF = udf((array : Seq[Double]) => {
  Vectors.dense(array.toArray)
})

vp_df.withColumn("vector", arrayToVectorUDF(col("number")))

更新:我误解了你的代码。 number 列是 DoubleType 列,因此您只需将列名传递给向量汇编器即可。

import org.apache.spark.ml.linalg.DenseMatrix
import org.apache.spark.ml.feature.VectorAssembler
import org.apache.spark.ml.linalg.Vectors

val data = (1 to 20).map(_.toDouble).toArray
val dm = new DenseMatrix(2, 10, data)
val vp_df = dm.toArray.toSeq.toDF("number")
val assembler = new VectorAssembler().setInputCols(Array("number")).setOutputCol("features")
val output = assembler.transform(vp_df)
output.select("features").show(false)
+--------+
|features|
+--------+
|[1.0]   |
|[2.0]   |
|[3.0]   |
|[4.0]   |
|[5.0]   |
|[6.0]   |
|[7.0]   |
|[8.0]   |
|[9.0]   |
|[10.0]  |
|[11.0]  |
|[12.0]  |
|[13.0]  |
|[14.0]  |
|[15.0]  |
|[16.0]  |
|[17.0]  |
|[18.0]  |
|[19.0]  |
|[20.0]  |
+--------+

【讨论】:

  • 我不明白这段代码究竟做了什么,但如果我运行它,我会得到:'AnalysisException: need an array field but got double'
猜你喜欢
  • 2016-09-28
  • 1970-01-01
  • 2018-08-31
  • 1970-01-01
  • 1970-01-01
  • 2020-03-23
  • 1970-01-01
  • 2023-03-03
  • 2017-09-16
相关资源
最近更新 更多