从flinkml多元线性回归中提取权值的scala方法

iih3973s  于 2021-06-24  发布在  Flink
关注(0)|答案(1)|浏览(440)

我正在运行flink(0.10-snapshot)的多元线性回归示例。我不知道如何提取权重(例如斜率和截距,beta0-beta1,你想怎么称呼它们)。我在斯卡拉不是很老练,这可能是我的问题的一半。
谢谢大家的帮助。

object Job {
 def main(args: Array[String]) {
    // set up the execution environment
    val env = ExecutionEnvironment.getExecutionEnvironment

    val survival = env.readCsvFile[(String, String, String, String)]("/home/danger/IdeaProjects/quickstart/docs/haberman.data")

    val survivalLV = survival
      .map{tuple =>
      val list = tuple.productIterator.toList
      val numList = list.map(_.asInstanceOf[String].toDouble)
      LabeledVector(numList(3), DenseVector(numList.take(3).toArray))
    }

    val mlr = MultipleLinearRegression()
      .setStepsize(1.0)
      .setIterations(100)
      .setConvergenceThreshold(0.001)

    mlr.fit(survivalLV) 
    println(mlr.toString())     // This doesn't do anything productive...
    println(mlr.weightsOption)  // Neither does this.

  }
}
c86crjj0

c86crjj01#

问题是您只构建了flink作业(dag),它将计算权重,但尚未执行。触发执行的最简单方法是使用 collect 方法,该方法将检索 DataSet 回到你的客户那里。

mlr.fit(survivalLV)

val weights = mlr.weightsOption match {
  case Some(weights) => weights.collect()
  case None => throw new Exception("Could not calculate the weights.")
}

println(weights)

相关问题