Github user dbtsai commented on a diff in the pull request:
https://github.com/apache/spark/pull/3288#discussion_r20553260
--- Diff: mllib/src/main/scala/org/apache/spark/mllib/linalg/Vectors.scala
---
@@ -76,6 +76,22 @@ sealed trait Vector extends Serializable {
def copy: Vector = {
throw new NotImplementedError(s"copy is not implemented for
${this.getClass}.")
}
+
+ /**
+ * It will return the iterator for the active elements of dense and
sparse vector as
+ * (index, value) pair. Note that foreach method can be overridden for
better performance
+ * in different vector implementation.
+ *
+ * @param skippingZeros Skipping zero elements explicitly if true. It
will be useful when we
+ * iterator through dense vector having lots of
zero elements which
+ * we want to skip. Default is false.
+ * @return Iterator[(Int, Double)] where the first element in the tuple
is the index,
+ * and the second element is the corresponding value.
+ */
+ private[spark] def activeIterator(skippingZeros: Boolean):
Iterator[(Int, Double)]
--- End diff --
With the following code,
sample.activeIterator(false).foreach {
case (index, value) => if(value != 0.0) add(index, value)
}
It takes 61.809 for dense vector, and 54.626 for sparse vector.
The most expensive part is calling the anonymous function even when the
values are zero.
---
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]