【问题标题】:org.apache.spark.ml.linalg.DenseVector cannot be cast to java.lang.Doubleorg.apache.spark.ml.linalg.DenseVector 不能转换为 java.lang.Double
【发布时间】:2020-10-10 11:46:30
【问题描述】:

有人可以帮我吗?我正在尝试将向量的列值转换为 Double 并收到以下错误: org.apache.spark.ml.linalg.DenseVector 不能转换为 java.lang.Double

我基本上已经在 (0,1) 范围内标准化了我的值,对于该范围,我必须将我的简单类型的双精度值转换为密集向量。

下面是代码

        val dataset =vectorUList.toDF("id")

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

        val output = assembler.transform(dataset)
        println("Assembled columns ")
        output.select("id").show(false)
        output.printSchema()

        val scaler = new MinMaxScaler()
          .setInputCol("features")
          .setOutputCol("vScaled")
          .setMax(1)
          .setMin(0)
        val ScalarModel =scaler.fit(output)
        val scalarData =ScalarModel.transform(output)

        scalarData.select("vScaled").show()

        val ScaledCol: List[Row] = scalarData.select("vScaled").collect.toList
        var listofScaledCol: List[Double] = ScaledCol.map(r => r.getDouble(0))
        print(listofScaledCol)

现在在将值转换回 Double 时,我收到了这个类型转换错误。

【问题讨论】:

    标签: scala apache-spark apache-spark-mllib apache-spark-ml


    【解决方案1】:

    您正在尝试从一行中访问向量,请修改- From

    var listofScaledCol: List[Double] = ScaledCol.map(r => r.getDouble(0))
    

    To

    var listofScaledCol: List[Double] = ScaledCol.map(_.getAs[Vector]("vScaled")(0))
    
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-09
    • 2017-07-01
    • 2023-02-01
    • 1970-01-01
    • 2012-10-02
    相关资源
    最近更新 更多