srowen commented on a change in pull request #24648: [SPARK-27777][ML] 
Eliminate uncessary sliding job in AreaUnderCurve
URL: https://github.com/apache/spark/pull/24648#discussion_r286966881
 
 

 ##########
 File path: 
mllib/src/main/scala/org/apache/spark/mllib/evaluation/AreaUnderCurve.scala
 ##########
 @@ -42,10 +41,40 @@ private[evaluation] object AreaUnderCurve {
    * @param curve an RDD of ordered 2D points stored in pairs representing a 
curve
    */
   def of(curve: RDD[(Double, Double)]): Double = {
-    curve.sliding(2).aggregate(0.0)(
-      seqOp = (auc: Double, points: Array[(Double, Double)]) => auc + 
trapezoid(points),
-      combOp = _ + _
-    )
+    val localAreas = curve.mapPartitions { iter =>
+      var localArea = 0.0
+      var firstPoint = Option.empty[(Double, Double)]
+      var lastPoint = Option.empty[(Double, Double)]
+
+      iter.sliding(2).foreach { points =>
+        if (firstPoint.isEmpty) {
+          firstPoint = Some(points.head)
+        }
+        lastPoint = Some(points.last)
+
+        if (points.length == 2) {
+          localArea += trapezoid(points)
+        }
+      }
+
+      if (firstPoint.nonEmpty) {
+        require(lastPoint.nonEmpty)
+        Iterator.single((localArea, (firstPoint.get, lastPoint.get)))
+      } else {
+        require(lastPoint.isEmpty)
+        Iterator.empty
+      }
+    }.collect()
+
+    var area = localAreas.map(_._1).sum
+    localAreas.iterator.map(_._2)
+      .sliding(2).withPartial(false)
+      .foreach { pointPairs =>
+        require(pointPairs.length == 2)
+        area += trapezoid(Seq(pointPairs.head._2, pointPairs.last._1))
 
 Review comment:
   Can you just sum these and make area the sum of the two?

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