【问题标题】:SparkML - Getting several metrics at the same time with RegressionEvaluator()SparkML - 使用 RegressionEvaluator() 同时获取多个指标
【发布时间】:2019-02-21 01:01:39
【问题描述】:

我正在尝试使用 RegressionEvaluator() 评估梯度提升树回归模型。我想比较这个evaluator 的四个可能指标:

  • rmse
  • 毫秒
  • r2

这就是我目前处理任务的方式。

//PREDICTION AND METRICS FOR GBT
val predictions = cvGBTModel.transform(test)

//Root Mean Squared Error
val evaluatorRMSE = new RegressionEvaluator()
  .setLabelCol("label")
  .setPredictionCol("prediction")
  .setMetricName("rmse");
val rmse = evaluatorRMSE.evaluate(predictions);

//Mean Squared Error
val evaluatorMSE = new RegressionEvaluator()
  .setLabelCol("label")
  .setPredictionCol("prediction")
  .setMetricName("mse");
val mse = evaluatorMSE.evaluate(predictions);

//Regression through the origin
val evaluatorR2 = new RegressionEvaluator()
  .setLabelCol("label")
  .setPredictionCol("prediction")
  .setMetricName("r2");
val r2 = evaluatorR2.evaluate(predictions);

//Mean absolute error
val evaluatorMAE = new RegressionEvaluator()
  .setLabelCol("label")
  .setPredictionCol("prediction")
  .setMetricName("mae");
val mae = evaluatorMAE.evaluate(predictions);


print("Root Mean Squared Error (RMSE) on test data = " + rmse);
print("Mean squared error (MSE) on test data = " + mse);
print("Regression through the origin(R2) on test data = " + r2);
print("Mean absolute error (MAE) on test data = " + mae);

是否可以在不运行 4 个不同的评估器的情况下同时获得四个指标?

顺便说一句,我发现了一个类似的question,用户发现 RegressionEvaluator 是使用 RegressionMetrics 实现的,它应该已经包含我正在寻找的四个指标。但我不清楚如何从评估者那里访问这些指标。

【问题讨论】:

  • Evaluator 确实使用了 MLlib RegressionMetrics,但如果您检查 the source,您会将实例设置为闭包的本地实例,然后将其丢弃。因此,没有任何技巧可用于提取多个指标。如果你真的需要这个,直接使用 MLLib 回归指标。

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


【解决方案1】:

正如 zero323 所提到的,没有任何技巧可用于提取多个指标

但您可以简化代码。 这里有两种方法可以做到这一点:

val evaluator = new RegressionEvaluator()
    .setLabelCol("label")
    .setPredictionCol("prediction")

val rmse = evaluator.setMetricName("rmse").evaluate(predictions)
println(s"rmse = $rmse")

val mae = evaluator.setMetricName("mae").evaluate(predictions)
println(s"mae = $mae")

val r2 = evaluator.setMetricName("r2").evaluate(predictions)
println(s"r2 = $r2")

或者

import org.apache.spark.ml.param.ParamMap

val evaluator = new RegressionEvaluator()
        .setLabelCol("label")
        .setPredictionCol("prediction")

val rmse = evaluator.evaluate(predictions, ParamMap(evaluator.metricName -> "rmse"))
println(s"rmse = $rmse")

val mae = evaluator.evaluate(predictions, ParamMap(evaluator.metricName -> "mae"))
println(s"mae = $mae")

val r2 = evaluator.evaluate(predictions, ParamMap(evaluator.metricName -> "r2"))
println(s"r2 = $r2")

资源: https://spark.apache.org/docs/latest/api/scala/org/apache/spark/ml/evaluation/RegressionEvaluator.html(见 evaluate 方法) https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.ml.evaluation.RegressionEvaluator.html(参见示例部分)

【讨论】:

    猜你喜欢
    • 2023-03-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-13
    • 2021-07-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多