Borja
Borja

Reputation: 204

Flink linearRegression: how to load data (Scala)

I'm starting to train a Multiple Linear Regression algorithm in Flink. I'm following the awesome official documentation and quickstart. I am using Zeppelin to develop this code.

If I load the data from a CSV file:

//Read the file:
val data = benv.readCsvFile[(Int, Double, Double, Double)]("/.../quake.csv")
val mapped = data.map {x => new org.apache.flink.ml.common.LabeledVector (x._4, org.apache.flink.ml.math.DenseVector(x._1,x._2,x._3)) }

//Data created:
mapped: org.apache.flink.api.scala.DataSet[org.apache.flink.ml.common.LabeledVector] = org.apache.flink.api.scala.DataSet@7cb37ad3
    LabeledVector(6.7, DenseVector(33.0, -52.26, 28.3))
    LabeledVector(5.8, DenseVector(36.0, 45.53, 150.93))
    LabeledVector(5.8, DenseVector(57.0, 41.85, 142.78))

//Predict with the model created:
Predict with the model createdval predictions:DataSet[org.apache.flink.ml.common.LabeledVector] = mlr.predict(mapped)

If I load the data from LIBSVM file:

val testingDS: DataSet[(Vector, Double)] = MLUtils.readLibSVM(benv, "/home/borja/Desktop/bbb/quake.libsvm").map(x => (x.vector,   x.label))

But I got this ERROR:

->CSV:

   res13: org.apache.flink.api.scala.DataSet[org.apache.flink.ml.common.LabeledVector] = org.apache.flink.api.scala.DataSet@7cb37ad3
    <console>:89: error: type mismatch;
     found   : org.apache.flink.api.scala.DataSet[Any]
     required: org.apache.flink.api.scala.DataSet[org.apache.flink.ml.common.LabeledVector]
    Note: Any >: org.apache.flink.ml.common.LabeledVector, but class DataSet is invariant in type T.
    You may wish to define T as -T instead. (SLS 4.5)
    Error occurred in an application involving default arguments.
           val predictions:DataSet[org.apache.flink.ml.common.LabeledVector] = mlr.predict(mapped)

->LIBSVM:

<console>:111: error: type Vector takes type parameters
       val testingDS: DataSet[(Vector, Double)] = MLUtils.readLibSVM(benv, "/home/borja/Desktop/bbb/quake.libsvm").map(x => (x.vector,   x.label))

Ok, so I wrote:

New Code:

val testingDS: DataSet[(Vector[org.apache.flink.ml.math.Vector], Double)] = MLUtils.readLibSVM(benv, "/home/borja/Desktop/bbb/quake.libsvm").map(x => (x.vector,   x.label))

New Error:

<console>:111: error: type mismatch;
 found   : org.apache.flink.ml.math.Vector
 required: scala.collection.immutable.Vector[org.apache.flink.ml.math.Vector]
       val testingDS: DataSet[(Vector[org.apache.flink.ml.math.Vector], Double)] = MLUtils.readLibSVM(benv, "/home/borja/Desktop/bbb/quake.libsvm").map(x => (x.vector,   x.label))

I would really appreciate your help! :)

Upvotes: 0

Views: 274

Answers (1)

twalthr
twalthr

Reputation: 2654

You should not import and use the Scala Vector class. Flink ML is shipped with its own Vector. This should work:

val testingDS: DataSet[(org.apache.flink.ml.math.Vector, Double)] = MLUtils.readLibSVM(benv, "/home/borja/Desktop/bbb/quake.libsvm").map(x => (x.vector,   x.label))

Upvotes: 1

Related Questions