You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by GitBox <gi...@apache.org> on 2020/05/20 05:00:19 UTC

[GitHub] [spark] huaxingao opened a new pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

huaxingao opened a new pull request #28590:
URL: https://github.com/apache/spark/pull/28590


   ### What changes were proposed in this pull request?
   add getMetrics in Evaluators to get the corresponding Metrics instance, so users can use it to get any of the metrics scores. For example:
   
   ```
       val trainer = new LinearRegression
       val model = trainer.fit(dataset)
       val predictions = model.transform(dataset)
   
       val evaluator = new RegressionEvaluator()
   
       val metrics = evaluator.getMetrics(predictions)
       val rmse = metrics.rootMeanSquaredError
       val r2 = metrics.r2
       val mae = metrics.meanAbsoluteError
       val variance = metrics.explainedVariance
   ```
   
   
   ### Why are the changes needed?
   Currently, Evaluator.evaluate only access to one metrics, but most users may need to get multiple metrics. This PR adds getMetrics in all the Evaluators, so users can use it to get an instance of the corresponding Metrics to get any of the metrics they want. 
   
   
   ### Does this PR introduce _any_ user-facing change?
   Yes. Add getMetrics in Evaluators.
   For example:
   ```
     /**
      * Get a RegressionMetrics, which can be used to get any of the regression
      * metrics such as rootMeanSquaredError, meanSquaredError, etc.
      *
      * @param dataset a dataset that contains labels/observations and predictions.
      * @return RegressionMetrics
      */
     @Since("3.1.0")
     def getMetrics(dataset: Dataset[_]): RegressionMetrics
   ```
   
   ### How was this patch tested?
   Add new unit tests
   


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



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


[GitHub] [spark] AmplabJenkins removed a comment on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-631262591






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



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


[GitHub] [spark] mengxr commented on a change in pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
mengxr commented on a change in pull request #28590:
URL: https://github.com/apache/spark/pull/28590#discussion_r563956258



##########
File path: mllib/src/main/scala/org/apache/spark/ml/evaluation/ClusteringMetrics.scala
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.evaluation
+
+import org.apache.spark.SparkContext
+import org.apache.spark.annotation.Since
+import org.apache.spark.broadcast.Broadcast
+import org.apache.spark.ml.linalg.{BLAS, DenseVector, Vector, Vectors}
+import org.apache.spark.ml.util.MetadataUtils
+import org.apache.spark.sql.{Column, DataFrame, Dataset}
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types.DoubleType
+
+
+/**
+ * Metrics for clustering, which expects two input columns: prediction and label.
+ */
+@Since("3.1.0")
+class ClusteringMetrics private[spark](dataset: Dataset[_]) {
+
+  private var distanceMeasure: String = "squaredEuclidean"
+
+  def getDistanceMeasure: String = distanceMeasure
+
+  def setDistanceMeasure(value: String) : Unit = distanceMeasure = value
+
+  /**
+   * Returns the silhouette score
+   */
+  @Since("3.1.0")
+  lazy val silhouette: Double = {

Review comment:
       This method will trigger I/O. By convention you should have `()` in the defintion.

##########
File path: mllib/src/main/scala/org/apache/spark/ml/evaluation/ClusteringMetrics.scala
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.evaluation
+
+import org.apache.spark.SparkContext
+import org.apache.spark.annotation.Since
+import org.apache.spark.broadcast.Broadcast
+import org.apache.spark.ml.linalg.{BLAS, DenseVector, Vector, Vectors}
+import org.apache.spark.ml.util.MetadataUtils
+import org.apache.spark.sql.{Column, DataFrame, Dataset}
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types.DoubleType
+
+
+/**
+ * Metrics for clustering, which expects two input columns: prediction and label.
+ */
+@Since("3.1.0")
+class ClusteringMetrics private[spark](dataset: Dataset[_]) {
+
+  private var distanceMeasure: String = "squaredEuclidean"
+
+  def getDistanceMeasure: String = distanceMeasure
+
+  def setDistanceMeasure(value: String) : Unit = distanceMeasure = value

Review comment:
       Should set return `self.type`? When we have more setters it is easier to chain.




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



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


[GitHub] [spark] SparkQA commented on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-631237557


   **[Test build #122873 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/122873/testReport)** for PR 28590 at commit [`e1a0a31`](https://github.com/apache/spark/commit/e1a0a316fca9138a03b3f79fa5f9f2805eabcbe0).


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



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


[GitHub] [spark] AmplabJenkins commented on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-631237933






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



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


[GitHub] [spark] AmplabJenkins commented on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
AmplabJenkins commented on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-631262591






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



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


[GitHub] [spark] zhengruifeng commented on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
zhengruifeng commented on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-631962737


   I do not object this PR. Is there any way to recompute another metric in evaluator without involving mllib.metrics?


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



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


[GitHub] [spark] AmplabJenkins removed a comment on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
AmplabJenkins removed a comment on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-631237933






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



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


[GitHub] [spark] srowen commented on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
srowen commented on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-633232852


   Merged to master


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



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


[GitHub] [spark] SparkQA removed a comment on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
SparkQA removed a comment on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-631237557


   **[Test build #122873 has started](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/122873/testReport)** for PR 28590 at commit [`e1a0a31`](https://github.com/apache/spark/commit/e1a0a316fca9138a03b3f79fa5f9f2805eabcbe0).


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



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


[GitHub] [spark] mengxr commented on a change in pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
mengxr commented on a change in pull request #28590:
URL: https://github.com/apache/spark/pull/28590#discussion_r563956258



##########
File path: mllib/src/main/scala/org/apache/spark/ml/evaluation/ClusteringMetrics.scala
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.evaluation
+
+import org.apache.spark.SparkContext
+import org.apache.spark.annotation.Since
+import org.apache.spark.broadcast.Broadcast
+import org.apache.spark.ml.linalg.{BLAS, DenseVector, Vector, Vectors}
+import org.apache.spark.ml.util.MetadataUtils
+import org.apache.spark.sql.{Column, DataFrame, Dataset}
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types.DoubleType
+
+
+/**
+ * Metrics for clustering, which expects two input columns: prediction and label.
+ */
+@Since("3.1.0")
+class ClusteringMetrics private[spark](dataset: Dataset[_]) {
+
+  private var distanceMeasure: String = "squaredEuclidean"
+
+  def getDistanceMeasure: String = distanceMeasure
+
+  def setDistanceMeasure(value: String) : Unit = distanceMeasure = value
+
+  /**
+   * Returns the silhouette score
+   */
+  @Since("3.1.0")
+  lazy val silhouette: Double = {

Review comment:
       This method will trigger I/O. By convention you should have `()` in the defintion.

##########
File path: mllib/src/main/scala/org/apache/spark/ml/evaluation/ClusteringMetrics.scala
##########
@@ -0,0 +1,575 @@
+/*
+ * 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.evaluation
+
+import org.apache.spark.SparkContext
+import org.apache.spark.annotation.Since
+import org.apache.spark.broadcast.Broadcast
+import org.apache.spark.ml.linalg.{BLAS, DenseVector, Vector, Vectors}
+import org.apache.spark.ml.util.MetadataUtils
+import org.apache.spark.sql.{Column, DataFrame, Dataset}
+import org.apache.spark.sql.functions._
+import org.apache.spark.sql.types.DoubleType
+
+
+/**
+ * Metrics for clustering, which expects two input columns: prediction and label.
+ */
+@Since("3.1.0")
+class ClusteringMetrics private[spark](dataset: Dataset[_]) {
+
+  private var distanceMeasure: String = "squaredEuclidean"
+
+  def getDistanceMeasure: String = distanceMeasure
+
+  def setDistanceMeasure(value: String) : Unit = distanceMeasure = value

Review comment:
       Should set return `self.type`? When we have more setters it is easier to chain.




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



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


[GitHub] [spark] srowen closed pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
srowen closed pull request #28590:
URL: https://github.com/apache/spark/pull/28590


   


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



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


[GitHub] [spark] huaxingao commented on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
huaxingao commented on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-632246256


   > Is there any way to recompute another metric in evaluator without involving mllib.metrics?
   
   It might be better to create a ml wrapper for mllib.metrics instead of using it directly, something like the approach in ```LogisticRegressionSummary```. This wrapper can be used both by evaluator and summary. 


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



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


[GitHub] [spark] SparkQA commented on pull request #28590: [SPARK-31768][ML] add getMetrics in Evaluators

Posted by GitBox <gi...@apache.org>.
SparkQA commented on pull request #28590:
URL: https://github.com/apache/spark/pull/28590#issuecomment-631262177


   **[Test build #122873 has finished](https://amplab.cs.berkeley.edu/jenkins/job/SparkPullRequestBuilder/122873/testReport)** for PR 28590 at commit [`e1a0a31`](https://github.com/apache/spark/commit/e1a0a316fca9138a03b3f79fa5f9f2805eabcbe0).
    * This patch passes all tests.
    * This patch merges cleanly.
    * This patch adds the following public classes _(experimental)_:
     * `  case class ClusterStats(featureSum: Vector, squaredNormSum: Double, numOfPoints: Long)`


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



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