From 80c6681c6c649b6f05473c4e787756fb7c89e682 Mon Sep 17 00:00:00 2001 From: Xiangrui Meng Date: Fri, 30 Jan 2015 11:43:16 -0800 Subject: [PATCH] update IRModel --- .../mllib/regression/IsotonicRegression.scala | 28 +++++++++---------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/mllib/src/main/scala/org/apache/spark/mllib/regression/IsotonicRegression.scala b/mllib/src/main/scala/org/apache/spark/mllib/regression/IsotonicRegression.scala index 4fd7904d026c6..779fa8d4c1453 100644 --- a/mllib/src/main/scala/org/apache/spark/mllib/regression/IsotonicRegression.scala +++ b/mllib/src/main/scala/org/apache/spark/mllib/regression/IsotonicRegression.scala @@ -31,31 +31,29 @@ import org.apache.spark.rdd.RDD * Boundaries must be sorted in increasing order. * @param predictions Array of predictions associated to the boundaries at the same index. * Results of isotonic regression and therefore monotone. + * @param isotonic indicates whether this is isotonic or antitonic. */ class IsotonicRegressionModel ( - boundaries: Array[Double], + val boundaries: Array[Double], val predictions: Array[Double], - isotonic: Boolean) - extends Serializable { + val isotonic: Boolean) extends Serializable { - private def isSorted(xs: Array[Double]): Boolean = { + private val predictionOrd = if (isotonic) Ordering[Double] else Ordering[Double].reverse + + assert(boundaries.length == predictions.length) + assertOrdered(boundaries) + assertOrdered(predictions)(predictionOrd) + + /** Asserts the input array is monotone with the given ordering. */ + private def assertOrdered(xs: Array[Double])(implicit ord: Ordering[Double]): Unit = { var i = 1 while (i < xs.length) { - if (xs(i) < xs(i - 1)) false + assert(ord.compare(xs(i - 1), xs(i)) <= 0, + s"Elements (${xs(i - 1)}, ${xs(i)}) are not ordered.") i += 1 } - true } - if (isotonic) { - assert(isSorted(predictions)) - } else { - assert(isSorted(predictions.map(-_))) - } - - assert(isSorted(boundaries)) - assert(boundaries.length == predictions.length) - /** * Predict labels for provided features. * Using a piecewise linear function.