-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-24283][ML] Make ml.StandardScaler skip conversion of Spar… #21942
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
Closed
Closed
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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 hidden or 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 |
|---|---|---|
|
|
@@ -21,7 +21,7 @@ import org.apache.hadoop.fs.Path | |
|
|
||
| import org.apache.spark.annotation.Since | ||
| import org.apache.spark.ml._ | ||
| import org.apache.spark.ml.linalg.{Vector, VectorUDT} | ||
| import org.apache.spark.ml.linalg.{DenseVector, SparseVector, Vector, Vectors, VectorUDT} | ||
| import org.apache.spark.ml.param._ | ||
| import org.apache.spark.ml.param.shared._ | ||
| import org.apache.spark.ml.util._ | ||
|
|
@@ -160,15 +160,88 @@ class StandardScalerModel private[ml] ( | |
| @Since("2.0.0") | ||
| override def transform(dataset: Dataset[_]): DataFrame = { | ||
| transformSchema(dataset.schema, logging = true) | ||
| val scaler = new feature.StandardScalerModel(std, mean, $(withStd), $(withMean)) | ||
|
|
||
| // TODO: Make the transformer natively in ml framework to avoid extra conversion. | ||
| val transformer: Vector => Vector = v => scaler.transform(OldVectors.fromML(v)).asML | ||
| val transformer: Vector => Vector = v => transform(v) | ||
|
|
||
| val scale = udf(transformer) | ||
| dataset.withColumn($(outputCol), scale(col($(inputCol)))) | ||
| } | ||
|
|
||
| /** | ||
| * Since `shift` will be only used in `withMean` branch, we have it as | ||
| * `lazy val` so it will be evaluated in that branch. Note that we don't | ||
| * want to create this array multiple times in `transform` function. | ||
| */ | ||
| private lazy val shift: Array[Double] = mean.toArray | ||
|
|
||
| /** | ||
| * Applies standardization transformation on a vector. | ||
| * | ||
| * @param vector Vector to be standardized. | ||
| * @return Standardized vector. If the std of a column is zero, it will return default `0.0` | ||
| * for the column with zero std. | ||
| */ | ||
| private[spark] def transform(vector: Vector): Vector = { | ||
| require(mean.size == vector.size) | ||
| if ($(withMean)) { | ||
| /** | ||
| * By default, Scala generates Java methods for member variables. So every time | ||
| * member variables are accessed, `invokespecial` is called. This is an expensive | ||
| * operation, and can be avoided by having a local reference of `shift`. | ||
| */ | ||
| val localShift = shift | ||
| /** Must have a copy of the values since they will be modified in place. */ | ||
| val values = vector match { | ||
| /** Handle DenseVector specially because its `toArray` method does not clone values. */ | ||
| case d: DenseVector => d.values.clone() | ||
| case v: Vector => v.toArray | ||
| } | ||
| val size = values.length | ||
| if ($(withStd)) { | ||
| var i = 0 | ||
| while (i < size) { | ||
| values(i) = if (std(i) != 0.0) (values(i) - localShift(i)) * (1.0 / std(i)) else 0.0 | ||
| i += 1 | ||
| } | ||
| } else { | ||
| var i = 0 | ||
| while (i < size) { | ||
| values(i) -= localShift(i) | ||
| i += 1 | ||
| } | ||
| } | ||
| Vectors.dense(values) | ||
| } else if ($(withStd)) { | ||
|
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. Maybe leave a comment withStd and not mean since when tracing the code by hand the nested if/else if can get a bit confusing flow wise. |
||
| vector match { | ||
| case DenseVector(vs) => | ||
| val values = vs.clone() | ||
| val size = values.length | ||
| var i = 0 | ||
| while(i < size) { | ||
| values(i) *= (if (std(i) != 0.0) 1.0 / std(i) else 0.0) | ||
| i += 1 | ||
| } | ||
| Vectors.dense(values) | ||
| case SparseVector(size, indices, vs) => | ||
| /** | ||
| * For sparse vector, the `index` array inside sparse vector object will not be changed, | ||
| * so we can re-use it to save memory. | ||
| */ | ||
| val values = vs.clone() | ||
| val nnz = values.length | ||
| var i = 0 | ||
| while (i < nnz) { | ||
| values(i) *= (if (std(indices(i)) != 0.0) 1.0 / std(indices(i)) else 0.0) | ||
| i += 1 | ||
| } | ||
| Vectors.sparse(size, indices, values) | ||
| case v => throw new IllegalArgumentException("Do not support vector type " + v.getClass) | ||
| } | ||
| } else { | ||
| /** Note that it's safe since we always assume that the data in RDD should be immutable. */ | ||
| vector | ||
| } | ||
| } | ||
|
|
||
| @Since("1.4.0") | ||
| override def transformSchema(schema: StructType): StructType = { | ||
| validateAndTransformSchema(schema) | ||
|
|
||
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.
How does this interplay with serialization? Would it make sense to evaluate this before we serialize the UDF so it isn't done on each worker or?