zhengruifeng commented on a change in pull request #26982: [SPARK-30329][ML]
add iterator/foreach methods for Vectors
URL: https://github.com/apache/spark/pull/26982#discussion_r361058571
##########
File path: mllib-local/src/main/scala/org/apache/spark/ml/linalg/Vectors.scala
##########
@@ -753,6 +782,39 @@ class SparseVector @Since("2.0.0") (
}.unzip
new SparseVector(selectedIndices.length, sliceInds.toArray,
sliceVals.toArray)
}
+
+ @Since("3.0.0")
+ override def iterator: Iterator[(Int, Double)] = {
Review comment:
what about making them `private[spark]`?
I add the iterators, then I can make some impls (like `.ml.stat.Summarizer`)
based on them, so that I can reuse it in both .ml and .mllib sides without
vector conversion.
As to the performance gain of
`SparseVector.iterator`/`SparseVector.foreach`, it is because that in this impl
only numFeatures checks `if (i == k)` are needed to traserval all elements,
while in the existing usages like in `RobustScaler`:
```scala
var i = 0
while (i < vec.size) {
agg(i) = agg(i).insert(vec(i))
i += 1
}
```
numFeatures binarysearch are needed.
----------------------------------------------------------------
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]
With regards,
Apache Git Services
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]