Advertisement
Guest User

Untitled

a guest
Feb 22nd, 2019
68
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.90 KB | None | 0 0
  1. import org.apache.spark.mllib.linalg.{Vectors,Vector,Matrix,SingularValueDecomposition,DenseMatrix,DenseVector}
  2. import org.apache.spark.mllib.linalg.distributed.RowMatrix
  3.  
  4. def computeInverse(X: RowMatrix): DenseMatrix = {
  5. val nCoef = X.numCols.toInt
  6. val svd = X.computeSVD(nCoef, computeU = true)
  7. if (svd.s.size < nCoef) {
  8. sys.error(s"RowMatrix.computeInverse called on singular matrix.")
  9. }
  10.  
  11. // Create the inv diagonal matrix from S
  12. val invS = DenseMatrix.diag(new DenseVector(svd.s.toArray.map(x => math.pow(x,-1))))
  13.  
  14. // U cannot be a RowMatrix
  15. val U = new DenseMatrix(svd.U.numRows().toInt,svd.U.numCols().toInt,svd.U.rows.collect.flatMap(x => x.toArray))
  16.  
  17. // If you could make V distributed, then this may be better. However its alreadly local...so maybe this is fine.
  18. val V = svd.V
  19. // inv(X) = V*inv(S)*transpose(U) --- the U is already transposed.
  20. (V.multiply(invS)).multiply(U)
  21. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement