Github user mengxr commented on a diff in the pull request:
https://github.com/apache/spark/pull/5967#discussion_r29953076
--- Diff:
mllib/src/main/scala/org/apache/spark/ml/classification/LogisticRegression.scala
---
@@ -193,3 +308,246 @@ class LogisticRegressionModel private[ml] (
copyValues(new LogisticRegressionModel(parent, weights, intercept),
extra)
}
}
+
+/**
+ * MultiClassSummarizer computes the number of distinct labels and
corresponding counts,
+ * and validates the data to see if the labels used for k class
multi-label classification
+ * are in the range of {0, 1, ..., k - 1} in a online fashion.
+ *
+ * Two MultilabelSummarizer can be merged together to have a statistical
summary of the
+ * corresponding joint dataset.
+ */
+class MultiClassSummarizer private[ml] extends Serializable {
+ private val distinctMap = new mutable.HashMap[Int, Long]
+ private var totalInvalidCnt: Long = 0L
+
+ /**
+ * Add a new label into this MultilabelSummarizer, and update the
distinct map.
+ * @param label The label for this data point.
+ * @return This MultilabelSummarizer
+ */
+ def add(label: Double): this.type = {
+ if (label - label.toInt != 0.0 || label < 0) {
+ totalInvalidCnt += 1
+ this
+ }
+ else {
+ val counts: Long = distinctMap.getOrElse(label.toInt, 0L)
+ distinctMap.put(label.toInt, counts + 1)
+ this
+ }
+ }
+
+ /**
+ * Merge another MultilabelSummarizer, and update the distinct map.
+ * (Note that it will merge the smaller distinct map into the larger one
using in-place
+ * merging, so either `this` or `other` object will be modified and
returned.)
+ *
+ * @param other The other MultilabelSummarizer to be merged.
+ * @return Merged MultilabelSummarizer object.
+ */
+ def merge(other: MultiClassSummarizer): MultiClassSummarizer = {
+ val (largeMap, smallMap) = if (this.distinctMap.size >
other.distinctMap.size) {
+ (this, other)
+ } else {
+ (other, this)
+ }
+ smallMap.distinctMap.foreach {
+ case (key, value) =>
+ val counts = largeMap.distinctMap.getOrElse(key, 0L)
+ largeMap.distinctMap.put(key, counts + value)
+ }
+ largeMap.totalInvalidCnt += smallMap.totalInvalidCnt
+ largeMap
+ }
+
+ def countInvalid: Long = totalInvalidCnt
+
+ def numClasses: Int = distinctMap.keySet.max + 1
+
+ def histogram: Array[Long] = {
+ val result = Array.ofDim[Long](numClasses)
+ var i = 0
+ while (i < result.length) {
+ result(i) = distinctMap.getOrElse(i, 0L)
+ i += 1
+ }
+ result
+ }
+}
+
+/**
+ * :: DeveloperApi ::
--- End diff --
Remove `DeveloperApi` since this is private.
---
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 [email protected] or file a JIRA ticket
with INFRA.
---
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]