Search code examples
scalaapache-sparkapache-spark-mllibapache-spark-ml

org.apache.spark.ml.linalg.DenseVector cannot be cast to java.lang.Double


Can somebody please help me I am trying to convert column values of vector to Double and getting the following error: org.apache.spark.ml.linalg.DenseVector cannot be cast to java.lang.Double

I have basically normalized my values between (0,1) range and for that range I had to convert my simple values of double type into dense Vector.

Following is the code

        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)

Now on converting the values back to Double I am getting this error of type conversion.


Solution

  • You are trying to access the vector from a row, modify- From

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

    To

    var listofScaledCol: List[Double] = ScaledCol.map(_.getAs[Vector]("vScaled")(0))