Skip to content
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

fix: SparkML StandardScaler conversion fails when withStd or withMean is set to true #555

Merged
merged 2 commits into from
Jun 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions onnxmltools/convert/sparkml/operator_converters/scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ def convert_sparkml_scaler(scope, operator, container):
attrs = {'name': scope.get_unique_operator_name(op_type)}
if isinstance(op, StandardScalerModel):
C = operator.inputs[0].type.shape[1]
attrs['offset'] = op.mean if op.getOrDefault("withMean") else [0.0] * C
attrs['scale'] = [1.0 / x for x in op.std] if op.getOrDefault("withStd") else [1.0] * C
attrs['offset'] = op.mean.toArray() if op.getOrDefault("withMean") else [0.0] * C
attrs['scale'] = [1.0 / x for x in op.std.toArray()] if op.getOrDefault("withStd") else [1.0] * C
elif isinstance(op, MinMaxScalerModel):
epsilon = 1.0e-8 # to avoid dividing by zero
attrs['offset'] = [x for x in op.originalMin]
Expand Down
2 changes: 1 addition & 1 deletion tests/sparkml/test_scaler.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def test_standard_scaler(self):
(1, Vectors.dense([2.0, 1.1, 1.0]),),
(2, Vectors.dense([3.0, 10.1, 3.0]),)
], ["id", "features"])
scaler = StandardScaler(inputCol='features', outputCol='scaled_features')
scaler = StandardScaler(inputCol='features', outputCol='scaled_features', withStd=True, withMean=True)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need to test other values of withStd, withMean?

model = scaler.fit(data)

# the input names must match the inputCol(s) above
Expand Down