-
Notifications
You must be signed in to change notification settings - Fork 393
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve test coverage for VectorsCombiner and make vector aggregator efficient #168
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f4f392c
Moved VectorsCombiner.combine to RichVector + updated tests
tovbinm 14522f8
style
tovbinm 33dd9dd
added SumVector
tovbinm d23029f
Merge branch 'master' into mt/vectors-combiner
tovbinm 3d4afa4
Added dot product + error checks
tovbinm 750a376
Merge branch 'master' into mt/vectors-combiner
tovbinm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,7 +31,9 @@ | |
package com.salesforce.op.utils.spark | ||
|
||
import breeze.linalg.{DenseVector => BreezeDenseVector, SparseVector => BreezeSparseVector, Vector => BreezeVector} | ||
import org.apache.spark.ml.linalg.{DenseVector, SparseVector, Vector} | ||
import org.apache.spark.ml.linalg.{DenseVector, SparseVector, Vector, Vectors} | ||
|
||
import scala.collection.mutable.ArrayBuffer | ||
|
||
/** | ||
* [[org.apache.spark.ml.linalg.Vector]] enrichment functions | ||
|
@@ -64,6 +66,30 @@ object RichVector { | |
toSpark(res) | ||
} | ||
|
||
/** | ||
* Dot product between vectors | ||
* | ||
* @param that another vector | ||
* @throws IllegalArgumentException if the vectors have different sizes | ||
* @return dot product | ||
*/ | ||
def dot(that: Vector): Double = { | ||
require(v.size == that.size, | ||
s"Vectors must have the same length: a.length == b.length (${v.size} != ${that.size})" | ||
) | ||
v.toBreeze dot that.toBreeze | ||
} | ||
|
||
/** | ||
* Combine multiple vectors into one | ||
* | ||
* @param that another vector | ||
* @param other other vectors | ||
* @return result vector | ||
*/ | ||
def combine(that: Vector, other: Vector*): Vector = | ||
com.salesforce.op.utils.spark.RichVector.combine(v +: that +: other) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need the full import here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is to distinguish the object from the implicit class. |
||
|
||
/** | ||
* Convert to [[breeze.linalg.Vector]] | ||
* | ||
|
@@ -85,4 +111,26 @@ object RichVector { | |
|
||
} | ||
|
||
/** | ||
* Combine multiple vectors into one | ||
* | ||
* @param vectors input vectors | ||
* @return result vector | ||
*/ | ||
def combine(vectors: Seq[Vector]): Vector = { | ||
val indices = ArrayBuffer.empty[Int] | ||
val values = ArrayBuffer.empty[Double] | ||
|
||
val size = vectors.foldLeft(0)((size, vector) => { | ||
vector.foreachActive { case (i, v) => | ||
if (v != 0.0) { | ||
indices += size + i | ||
values += v | ||
} | ||
} | ||
size + vector.size | ||
}) | ||
Vectors.sparse(size, indices.toArray, values.toArray).compressed | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
as long as you are adding addition and subtraction why not dot product and cross product?