[GitHub] [spark] zhengruifeng commented on a change in pull request #27555: [SPARK-30802][ML] Use Summarizer instead of MultivariateOnlineSummarizer in Aggregator test suite

2020-02-17 Thread GitBox
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_r380469360
 
 

 ##
 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) {
+  totalInvalidCnt += 1
+  this
+}
+else {
+  val (counts: Long, weightSum: Double) = 
distinctMap.getOrElse(label.toInt, (0L, 0.0))
+  distinctMap.put(label.toInt, (counts + 1L, weightSum + weight))
+  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: Long, weightSum: Double) = 
largeMap.distinctMap.getOrElse(key, (0L, 0.0))
+largeMap.distinctMap.put(key, (counts + value._1, weightSum + 
value._2))
+}
+largeMap.totalInvalidCnt += smallMap.totalInvalidCnt
+largeMap
+  }
+
+  /** @return The total invalid input counts. */
+  def countInvalid: Long = totalInvalidCnt
+
+  /** @return The number of distinct labels in the input dataset. */
+  def numClasses: Int = if (distinctMap.isEmpty) 0 else distinctMap.keySet.max 
+ 1
 
 Review comment:
   nit: `distinctMap.keysIterator.max + 1`


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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] zhengruifeng commented on a change in pull request #27555: [SPARK-30802][ML] Use Summarizer instead of MultivariateOnlineSummarizer in Aggregator test suite

2020-02-17 Thread GitBox
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:
us...@infra.apache.org


With regards,
Apache Git Services

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



[GitHub] [spark] zhengruifeng commented on a change in pull request #27555: [SPARK-30802][ML] Use Summarizer instead of MultivariateOnlineSummarizer in Aggregator test suite

2020-02-13 Thread GitBox
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_r379289047
 
 

 ##
 File path: mllib/src/main/scala/org/apache/spark/ml/stat/Summarizer.scala
 ##
 @@ -202,6 +204,38 @@ object Summarizer extends Logging {
 val (metrics, computeMetrics) = getRelevantMetrics(requested)
 new SummarizerBuffer(metrics, computeMetrics)
   }
+
+  /** Get regression feature and label summarizers for provided data. */
+  private[spark] def getRegressionSummarizers(
 
 Review comment:
   `private[ml]` ?


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:
us...@infra.apache.org


With regards,
Apache Git Services

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