spark-reviews mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From feynmanliang <...@git.apache.org>
Subject [GitHub] spark pull request: [SPARK-6332] [MLlib] compute calibration curve...
Date Mon, 31 Aug 2015 23:19:43 GMT
Github user feynmanliang commented on a diff in the pull request:

    https://github.com/apache/spark/pull/5025#discussion_r38371291
  
    --- Diff: mllib/src/main/scala/org/apache/spark/mllib/evaluation/BinaryClassificationMetrics.scala
---
    @@ -204,4 +204,76 @@ class BinaryClassificationMetrics(
           (x(c), y(c))
         }
       }
    +
    +  /**
    +   * Returns the calibration or reliability curve,
    +   * which is an RDD of (average score in bin, fraction of positive examples in bin).
    +   * @see http://en.wikipedia.org/wiki/Calibration_%28statistics%29#In_classification
    +   *
    +   * References:
    +   *
    +   * Mahdi Pakdaman Naeini, Gregory F. Cooper, Milos Hauskrecht.
    +   * Binary Classifier Calibration: Non-parametric approach.
    +   * http://arxiv.org/abs/1401.3390
    +   *
    +   * Alexandru Niculescu-Mizil, Rich Caruana.
    +   * Predicting Good Probabilities With Supervised Learning.
    +   * Appearing in Proceedings of the 22nd International Conference on Machine Learning,
    +   * Bonn, Germany, 2005.
    +   * http://www.cs.cornell.edu/~alexn/papers/calibration.icml05.crc.rev3.pdf
    +   *
    +   * Properties and benefits of calibrated classifiers.
    +   * Ira Cohen, Moises Goldszmidt.
    +   * http://www.hpl.hp.com/techreports/2004/HPL-2004-22R1.pdf
    +   */
    +  def calibration(): RDD[((Double, Double), (Double, Long))] = {
    +    assessedCalibration
    +  }
    +  
    +  private lazy val assessedCalibration: RDD[((Double, Double), (Double, Long))] = {
    +    val distinctScoresAndLabelCounts = scoreAndLabels.combineByKey(
    +      createCombiner = (label: Double) => new BinaryLabelCounter(0L, 0L) += label,
    +      mergeValue = (c: BinaryLabelCounter, label: Double) => c += label,
    +      mergeCombiners = (c1: BinaryLabelCounter, c2: BinaryLabelCounter) => c1 += c2
    +    ).sortByKey(ascending = true)
    +  
    +    val binnedDistinctScoresAndLabelCounts =
    +      if (numBins == 0) {
    +        distinctScoresAndLabelCounts.map { pair => ((pair._1, pair._1), pair._2) }
    +      } else {
    +        val distinctScoresCount = distinctScoresAndLabelCounts.count()
    +  
    +        var groupCount =
    +          if (distinctScoresCount % numBins == 0) {
    +            distinctScoresCount / numBins
    +          } else {
    +            // prevent the last bin from being very small compared to the others
    +            distinctScoresCount / numBins + 1
    +          }
    +        
    +        if (groupCount < 2) {
    +          logInfo(s"Too few distinct scores ($distinctScoresCount) for $numBins bins
to be useful")
    +          distinctScoresAndLabelCounts.map { pair => ((pair._1, pair._1), pair._2)
}
    +        } else {
    +          if (groupCount >= Int.MaxValue) {
    --- End diff --
    
    The overflow will wrap around to be negative so this check needs to be changed (the check
on L254 should probably check `0 < groupCount < 2` to avoid catching integer overflow
case)


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org


Mime
View raw message