从FlinkML多元线性回归中提取权重

时间:2015-10-07 19:07:18

标签: scala machine-learning linear-regression apache-flink flinkml

我正在为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.

  }
}

1 个答案:

答案 0 :(得分:5)

问题在于您只构建了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)