srowen commented on a change in pull request #24656: [SPARK-27787][ML] Eliminate uncessary job to compute SSreg URL: https://github.com/apache/spark/pull/24656#discussion_r286080206
########## File path: mllib/src/main/scala/org/apache/spark/mllib/evaluation/RegressionMetrics.scala ########## @@ -60,29 +60,23 @@ class RegressionMetrics @Since("2.0.0") ( * Use MultivariateOnlineSummarizer to calculate summary statistics of observations and errors. */ private lazy val summary: MultivariateStatisticalSummary = { - val summary: MultivariateStatisticalSummary = predictionAndObservations.map { + predictionAndObservations.map { case (prediction: Double, observation: Double, weight: Double) => - (Vectors.dense(observation, observation - prediction), weight) + (Vectors.dense(observation, observation - prediction, prediction), weight) case (prediction: Double, observation: Double) => - (Vectors.dense(observation, observation - prediction), 1.0) + (Vectors.dense(observation, observation - prediction, prediction), 1.0) }.treeAggregate(new MultivariateOnlineSummarizer())( (summary, sample) => summary.add(sample._1, sample._2), (sum1, sum2) => sum1.merge(sum2) ) - summary } private lazy val SSy = math.pow(summary.normL2(0), 2) private lazy val SSerr = math.pow(summary.normL2(1), 2) private lazy val SStot = summary.variance(0) * (summary.weightSum - 1) - private lazy val SSreg = { - val yMean = summary.mean(0) - predictionAndObservations.map { - case (prediction: Double, _: Double, weight: Double) => - math.pow(prediction - yMean, 2) * weight - case (prediction: Double, _: Double) => math.pow(prediction - yMean, 2) - }.sum() - } + private lazy val SSreg = math.pow(summary.normL2(2), 2) + Review comment: It took me a minute but yes I think this is correct. It feels like this code would be more straightforward if the summarizer would just expose a sum and squaredL2Norm method, but, maybe for another time. A few comments about the computations here wouldn't hurt. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: reviews-unsubscr...@spark.apache.org For additional commands, e-mail: reviews-h...@spark.apache.org