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_r288140555
 
 

 ##########
 File path: 
mllib/src/main/scala/org/apache/spark/mllib/evaluation/MultilabelMetrics.scala
 ##########
 @@ -38,85 +40,65 @@ class MultilabelMetrics @Since("1.2.0") 
(predictionAndLabels: RDD[(Array[Double]
       (r.getSeq[Double](0).toArray, r.getSeq[Double](1).toArray)
     })
 
-  private lazy val numDocs: Long = predictionAndLabels.count()
+  /**
+   * Use MultilabelSummarizer to calculate all summary statistics of 
predictions
+   * and labels on one pass.
+   */
+  private val summary: MultilabelSummarizer = {
+    predictionAndLabels
+      .treeAggregate(new MultilabelSummarizer)(
+        (summary, sample) => summary.add(sample._1, sample._2),
+        (sum1, sum2) => sum1.merge(sum2)
+      )
+  }
 
-  private lazy val numLabels: Long = predictionAndLabels.flatMap { case (_, 
labels) =>
-    labels}.distinct().count()
 
   /**
    * Returns subset accuracy
    * (for equal sets of labels)
    */
   @Since("1.2.0")
-  lazy val subsetAccuracy: Double = predictionAndLabels.filter { case 
(predictions, labels) =>
-    predictions.deep == labels.deep
-  }.count().toDouble / numDocs
+  lazy val subsetAccuracy: Double = summary.subsetAccuracy
 
 Review comment:
   These shouldn't be lazy now. They just delegate to a non-lazy object that's 
already initialized.

----------------------------------------------------------------
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