Github user dbtsai commented on a diff in the pull request:
https://github.com/apache/spark/pull/10702#discussion_r51070506
--- Diff:
mllib/src/main/scala/org/apache/spark/ml/regression/LinearRegression.scala ---
@@ -219,33 +219,43 @@ class LinearRegression @Since("1.3.0")
(@Since("1.3.0") override val uid: String
}
val yMean = ySummarizer.mean(0)
- val yStd = math.sqrt(ySummarizer.variance(0))
-
- // If the yStd is zero, then the intercept is yMean with zero
coefficient;
- // as a result, training is not needed.
- if (yStd == 0.0) {
- logWarning(s"The standard deviation of the label is zero, so the
coefficients will be " +
- s"zeros and the intercept will be the mean of the label; as a
result, " +
- s"training is not needed.")
- if (handlePersistence) instances.unpersist()
- val coefficients = Vectors.sparse(numFeatures, Seq())
- val intercept = yMean
-
- val model = new LinearRegressionModel(uid, coefficients, intercept)
- // Handle possible missing or invalid prediction columns
- val (summaryModel, predictionColName) =
model.findSummaryModelAndPredictionCol()
-
- val trainingSummary = new LinearRegressionTrainingSummary(
- summaryModel.transform(dataset),
- predictionColName,
- $(labelCol),
- model,
- Array(0D),
- $(featuresCol),
- Array(0D))
- return copyValues(model.setSummary(trainingSummary))
+ val rawYStd = math.sqrt(ySummarizer.variance(0))
+ if (rawYStd == 0.0) {
+ if ($(fitIntercept)) {
+ // If the rawYStd is zero and fitIntercept=true, then the
intercept is yMean with
+ // zero coefficient; as a result, training is not needed.
+ logWarning(s"The standard deviation of the label is zero, so the
coefficients will be " +
+ s"zeros and the intercept will be the mean of the label; as a
result, " +
+ s"training is not needed.")
+ if (handlePersistence) instances.unpersist()
+ val coefficients = Vectors.sparse(numFeatures, Seq())
+ val intercept = yMean
+
+ val model = new LinearRegressionModel(uid, coefficients, intercept)
+ // Handle possible missing or invalid prediction columns
+ val (summaryModel, predictionColName) =
model.findSummaryModelAndPredictionCol()
+
+ val trainingSummary = new LinearRegressionTrainingSummary(
+ summaryModel.transform(dataset),
+ predictionColName,
+ $(labelCol),
+ model,
+ Array(0D),
+ $(featuresCol),
+ Array(0D))
+ return copyValues(model.setSummary(trainingSummary))
+ } else {
+ require(!($(regParam) > 0.0 && $(standardization)),
+ "The standard deviation of the label is zero. " +
+ "Model cannot be regularized with standardization=true")
+ logWarning(s"The standard deviation of the label is zero. " +
+ "Consider setting fitIntercept=true.")
+ }
}
+ // if y is constant (rawYStd is zero), then y cannot be scaled. In
this case
+ // setting yStd=1.0 ensures that y is not scaled anymore in l-bfgs
algorithm.
+ val yStd = if (rawYStd > 0) rawYStd else 1.0
--- End diff --
Actually, in the case of `yMean == 0.0 && yStd == 0.0`, the coefficients
will be all zeros as well even `fitIntercept == false`. This is rare case, so
we can let model training to figure out. But if you want to handle this
explicitly, it's great.
---
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]