-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-4581][MLlib] Refactorize StandardScaler to improve the transformation performance #3435
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -96,14 +96,14 @@ class StandardScalerModel private[mllib] ( | |
| */ | ||
| override def transform(vector: Vector): Vector = { | ||
| require(mean.size == vector.size) | ||
| val localFactor = factor | ||
| if (withMean) { | ||
| val localShift = shift | ||
|
Contributor
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.
Member
Author
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. Oh, I'll change it back to lazy since it will not be evaluated in those branches which don't use
Contributor
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.
Member
Author
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. For different implementation of vector, toArray can be very expensive. For example, toArray for sparse vector requires to create a new array object and loop through all the non zero values. As a result, we can have a global |
||
| vector match { | ||
| case dv: DenseVector => | ||
| val values = dv.values.clone() | ||
| var i = 0 | ||
| if(withStd) { | ||
| val localFactor = factor | ||
| while (i < values.length) { | ||
| values(i) = (values(i) - localShift(i)) * localFactor(i) | ||
| i += 1 | ||
|
|
@@ -118,6 +118,7 @@ class StandardScalerModel private[mllib] ( | |
| case v => throw new IllegalArgumentException("Do not support vector type " + v.getClass) | ||
| } | ||
| } else if (withStd) { | ||
| val localFactor = factor | ||
| vector match { | ||
| case dv: DenseVector => | ||
| val values = dv.values.clone() | ||
|
|
||
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.
It is worth to leave a comment here and explain why we need local reference.