Github user sethah commented on a diff in the pull request:
https://github.com/apache/spark/pull/11610#discussion_r55909088
--- Diff:
mllib/src/main/scala/org/apache/spark/ml/optim/WeightedLeastSquares.scala ---
@@ -108,6 +101,53 @@ private[ml] class WeightedLeastSquares(
"Consider setting fitIntercept=true.")
}
}
+ /*
+ If more than of the features in the data are constant (i.e, data
matrix has constant columns),
+ then A^T.A is no longer positive definite and Cholesky decomposition
fails (because the
+ normal equation does not have a solution).
+ In order to find a solution, we need to drop constant columns from
the data matrix. Or,
+ we can drop corresponding column and row from A^T.A matrix.
+ Once we drop rows/columns from A^T.A matrix, the Cholesky
decomposition will produce
+ correct coefficients. But, for the final result, we need to add
zeros to the list of
+ coefficients corresponding to the constant features.
+ */
+ val aVarRaw = summary.aVar.values
+ // this will keep track of features to keep in the model, and remove
+ // features with zero variance.
+ val nzVarIndex = aVarRaw.zipWithIndex.filter( _._1 != 0 ).map( _._2 )
+ val nz = nzVarIndex.length
+ // if there are features with zero variance, then ATA is not positive
definite, and we need to
+ // keep track of that.
+ val singular = summary.k > nz
+ val k = if (fitIntercept) nz + 1 else nz
+ val triK = nz * (nz + 1) / 2
+
+ val aVar = if (singular) {
+ for (i <- nzVarIndex) yield {aVarRaw(i)}
--- End diff --
The curly braces here and below are not necessary.
---
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]