srowen commented on a change in pull request #24717: [SPARK-27847][ML] One-Pass 
MultilabelMetrics & MulticlassMetrics
URL: https://github.com/apache/spark/pull/24717#discussion_r288139122
 
 

 ##########
 File path: 
mllib/src/main/scala/org/apache/spark/mllib/evaluation/MulticlassMetrics.scala
 ##########
 @@ -55,31 +48,59 @@ class MulticlassMetrics @Since("1.1.0") 
(predictionAndLabels: RDD[_ <: Product])
         throw new IllegalArgumentException(s"Expected Row of tuples, got 
$other")
     })
 
-  private lazy val labelCountByClass: Map[Double, Double] =
-    predLabelsWeight.map {
-      case (_: Double, label: Double, weight: Double) =>
-        (label, weight)
-    }.reduceByKey(_ + _)
+
+  private val confusions = predictionAndLabels.map {
+    case (prediction: Double, label: Double, weight: Double) =>
+      (prediction, label, weight)
+    case (prediction: Double, label: Double) =>
+      (prediction, label, 1.0)
+    case other =>
+      throw new IllegalArgumentException(s"Expected tuples, got $other")
+  }.map { case (prediction: Double, label: Double, weight: Double) =>
+    ((label, prediction), weight)
+  }.reduceByKey(_ + _)
     .collectAsMap()
+
+  private lazy val labelCountByClass: Map[Double, Double] = {
+    val labelCountByClass = mutable.Map.empty[Double, Double]
+    confusions.iterator.foreach {
+      case ((label, _), weight) =>
+        val w = labelCountByClass.getOrElse(label, 0.0)
+        labelCountByClass.update(label, w + weight)
+    }
+    labelCountByClass.toMap
+  }
+
   private lazy val labelCount: Double = labelCountByClass.values.sum
-  private lazy val tpByClass: Map[Double, Double] = predLabelsWeight
-    .map {
-      case (prediction: Double, label: Double, weight: Double) =>
-        (label, if (label == prediction) weight else 0.0)
-    }.reduceByKey(_ + _)
-    .collectAsMap()
-  private lazy val fpByClass: Map[Double, Double] = predLabelsWeight
-    .map {
-      case (prediction: Double, label: Double, weight: Double) =>
-        (prediction, if (prediction != label) weight else 0.0)
-    }.reduceByKey(_ + _)
-    .collectAsMap()
-  private lazy val confusions = predLabelsWeight
-    .map {
-      case (prediction: Double, label: Double, weight: Double) =>
-        ((label, prediction), weight)
-    }.reduceByKey(_ + _)
-    .collectAsMap()
+
+  private lazy val tpByClass: Map[Double, Double] = {
+    val tpByClass = mutable.Map.empty[Double, Double]
+    confusions.iterator.foreach {
+      case ((label, prediction), weight) =>
+        val w = tpByClass.getOrElse(label, 0.0)
+        if (label == prediction) {
+          tpByClass.update(label, w + weight)
+        } else {
+          tpByClass.update(label, w)
 
 Review comment:
   This would be a no-op right unless w == 0? what about:
   ```
   } else if (w == 0.0) {
     tpByClass.put(label, 0.0)
   }
   ```
   Same below.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to