zhengruifeng commented on a change in pull request #27555: [SPARK-30802][ML] 
Use Summarizer instead of MultivariateOnlineSummarizer in Aggregator test suite
URL: https://github.com/apache/spark/pull/27555#discussion_r380468433
 
 

 ##########
 File path: 
mllib/src/main/scala/org/apache/spark/ml/stat/MultiClassSummarizer.scala
 ##########
 @@ -0,0 +1,100 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.spark.ml.stat
+
+import scala.collection.mutable
+
+
+/**
+ * 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 an online fashion.
+ *
+ * Two MultilabelSummarizer can be merged together to have a statistical 
summary of the
+ * corresponding joint dataset.
+ */
+private[ml] class MultiClassSummarizer extends Serializable {
+  // The first element of value in distinctMap is the actually number of 
instances,
+  // and the second element of value is sum of the weights.
+  private val distinctMap = new mutable.HashMap[Int, (Long, Double)]
+  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.
+   * @param weight The weight of this instances.
+   * @return This MultilabelSummarizer
+   */
+  def add(label: Double, weight: Double = 1.0): MultiClassSummarizer = {
+    require(weight >= 0.0, s"instance weight, $weight has to be >= 0.0")
+
+    if (weight == 0.0) return this
+
+    if (label - label.toInt != 0.0 || label < 0) {
 
 Review comment:
   ```scala
       if (label - label.toInt != 0.0 || label < 0) {
         totalInvalidCnt += 1
       } else {
         val (counts: Long, weightSum: Double) = 
distinctMap.getOrElse(label.toInt, (0L, 0.0))
         distinctMap.put(label.toInt, (counts + 1L, weightSum + weight))
       }
       this
   
   
   ```

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