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_r287793291
########## File path: mllib/src/main/scala/org/apache/spark/mllib/evaluation/MultilabelMetrics.scala ########## @@ -179,3 +167,160 @@ class MultilabelMetrics @Since("1.2.0") (predictionAndLabels: RDD[(Array[Double] @Since("1.2.0") lazy val labels: Array[Double] = tpPerClass.keys.toArray.sorted } + + +/** + * Trait for statistical summary for multi-label metrics. + */ +private[evaluation] trait MultilabelSummary { + + def numDocs: Long + + def numLabels: Long + + def subsetAccuracy: Double + + def accuracy: Double + + def hammingLoss: Double + + def precision: Double + + def recall: Double + + def f1Measure: Double + + def tpPerClass: Map[Double, Long] + + def fpPerClass: Map[Double, Long] + + def fnPerClass: Map[Double, Long] +} + + +private[evaluation] class MultilabelSummarizer extends MultilabelSummary with Serializable { + + private var docCnt = 0L + private val labelSet = mutable.Set.empty[Double] + private var subsetAccuracyCnt = 0L + private var accuracySum = 0.0 + private var hammingLossSum = 0L + private var precisionSum = 0.0 + private var recallSum = 0.0 + private var f1MeasureSum = 0.0 + private val tpPerClass_ = mutable.Map.empty[Double, Long] + private val fpPerClass_ = mutable.Map.empty[Double, Long] + private val fnPerClass_ = mutable.Map.empty[Double, Long] + + /** + * Add a new sample (predictions and labels) to this summarizer, and update + * the statistical summary. + * + * @return This MultilabelSummarizer object. + */ + def add(predictions: Array[Double], labels: Array[Double]): this.type = { + val intersection = predictions.intersect(labels) + + docCnt += 1L + + labels.foreach(labelSet.add) + + if (predictions.deep == labels.deep) { Review comment: I'd switch this to `Arrays.equals()` for slightly better performance, as I suppose that's the goal here. ---------------------------------------------------------------- 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