srowen commented on a change in pull request #32808:
URL: https://github.com/apache/spark/pull/32808#discussion_r656706703



##########
File path: 
mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala
##########
@@ -439,13 +438,27 @@ class RowMatrix @Since("1.0.0") (
       "  Cannot compute the covariance of a RowMatrix with <= 1 row.")
     val mean = Vectors.fromML(summary.mean)
 
-    if (rows.first().isInstanceOf[DenseVector]) {
+    val sparsityThreshold = 0.5
+    val sparsity = calcSparsity()
+
+    if (sparsity<sparsityThreshold) {

Review comment:
       Just inline the two vars and put a space around <

##########
File path: 
mllib/src/main/scala/org/apache/spark/mllib/linalg/distributed/RowMatrix.scala
##########
@@ -439,13 +438,27 @@ class RowMatrix @Since("1.0.0") (
       "  Cannot compute the covariance of a RowMatrix with <= 1 row.")
     val mean = Vectors.fromML(summary.mean)
 
-    if (rows.first().isInstanceOf[DenseVector]) {
+    val sparsityThreshold = 0.5
+    val sparsity = calcSparsity()
+
+    if (sparsity<sparsityThreshold) {
       computeDenseVectorCovariance(mean, n, m)
     } else {
       computeSparseVectorCovariance(mean, n, m)
     }
   }
 
+  /**
+   * Calculate percent sparsity of the matrix as number-of-zero-elements
+   * divided by the total number of elements of the matrix
+   */
+  @Since("3.2.0")
+  def calcSparsity(): Double = rows.map{

Review comment:
       This should be private




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