huaxingao commented on a change in pull request #28990:
URL: https://github.com/apache/spark/pull/28990#discussion_r449915974



##########
File path: 
mllib/src/main/scala/org/apache/spark/ml/regression/GeneralizedLinearRegression.scala
##########
@@ -1220,9 +1220,42 @@ class GeneralizedLinearRegressionSummary 
private[regression] (
 
   private[regression] lazy val link: Link = familyLink.link
 
+

Review comment:
       super nit: extra blank line?

##########
File path: 
mllib/src/main/scala/org/apache/spark/ml/regression/GeneralizedLinearRegression.scala
##########
@@ -1220,9 +1220,42 @@ class GeneralizedLinearRegressionSummary 
private[regression] (
 
   private[regression] lazy val link: Link = familyLink.link
 
+
+  /**
+   * summary row containing:
+   *  numInstances, weightSum, deviance, rss, weighted average of label - 
offset.
+   */
+  private lazy val glrSummary = {
+    val devUDF = udf { (label: Double, pred: Double, weight: Double) =>
+      family.deviance(label, pred, weight)
+    }
+    val devCol = sum(devUDF(label, prediction, weight))
+
+    val rssCol = if (model.getFamily.toLowerCase(Locale.ROOT) != Binomial.name 
&&
+      model.getFamily.toLowerCase(Locale.ROOT) != Poisson.name) {
+      val rssUDF = udf { (label: Double, pred: Double, weight: Double) =>
+        (label - pred) * (label - pred) * weight / family.variance(pred)
+      }
+      sum(rssUDF(label, prediction, weight))
+    } else {
+      lit(Double.NaN)
+    }
+
+    val avgCol = if (model.getFitIntercept &&
+      (!model.hasOffsetCol || (model.hasOffsetCol && family == Gaussian && 
link == Identity))) {
+      sum((label - offset) * weight) / sum(weight)
+    } else {
+      lit(Double.NaN)
+    }
+
+    predictions
+      .select(count(label), sum(weight), devCol, rssCol, avgCol)
+      .head()
+  }
+
   /** Number of instances in DataFrame predictions. */
   @Since("2.2.0")
-  lazy val numInstances: Long = predictions.count()
+  lazy val numInstances: Long = glrSummary.getLong(0)
 
 

Review comment:
       remove this extra blank line too?




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



---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to