Github user sethah commented on a diff in the pull request:

    https://github.com/apache/spark/pull/11610#discussion_r55909205
  
    --- 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)}
    +    } else {
    +      aVarRaw
    +    }
    +    val aBar = if (singular) {
    +      val aBarTemp = summary.aBar.values
    +      for (i <- nzVarIndex) yield {aBarTemp(i)}
    +    } else {
    +      summary.aBar.values
    +    }
    +    val abBar = if (singular) {
    +      val abBarTemp = summary.abBar.values
    +      for (i <- nzVarIndex) yield {abBarTemp(i)}
    +    } else {
    +      summary.abBar.values
    +    }
    +    val aaBar = if (singular) {
    --- End diff --
    
    Though you mentioned it, subtly, in the comment above, it is not clear what 
is going on here. It would be nice to add a more thorough comment that you are 
removing a row and column of the ATA matrix here and why.


---
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 infrastruct...@apache.org or file a JIRA ticket
with INFRA.
---

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

Reply via email to