-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-21633][ML][Python] UnaryTransformer in Python #18746
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
ajaysaini725
wants to merge
9
commits into
apache:master
from
ajaysaini725:AddPythonUnaryTransformer
Closed
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
10ab7bc
Started
ajaysaini725 4fe9420
Started
ajaysaini725 7d25c70
Implemented Unary Transforme in Python.
ajaysaini725 960de95
Fixed merge conflict.
ajaysaini725 11f8f29
Fixed small issue with None being returned.
ajaysaini725 0eed7c3
Some progress on testing
ajaysaini725 692aa5d
Added test for unary transformer.
ajaysaini725 527bc88
Fixed based on pull request comments.
ajaysaini725 a30ae39
Fixed test based on comments
ajaysaini725 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
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
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 |
|---|---|---|
|
|
@@ -45,7 +45,7 @@ | |
| import inspect | ||
|
|
||
| from pyspark import keyword_only, SparkContext | ||
| from pyspark.ml import Estimator, Model, Pipeline, PipelineModel, Transformer | ||
| from pyspark.ml import Estimator, Model, Pipeline, PipelineModel, Transformer, UnaryTransformer | ||
| from pyspark.ml.classification import * | ||
| from pyspark.ml.clustering import * | ||
| from pyspark.ml.common import _java2py, _py2java | ||
|
|
@@ -66,6 +66,7 @@ | |
| from pyspark.serializers import PickleSerializer | ||
| from pyspark.sql import DataFrame, Row, SparkSession | ||
| from pyspark.sql.functions import rand | ||
| from pyspark.sql.types import DoubleType, IntegerType | ||
| from pyspark.storagelevel import * | ||
| from pyspark.tests import ReusedPySparkTestCase as PySparkTestCase | ||
|
|
||
|
|
@@ -121,6 +122,36 @@ def _transform(self, dataset): | |
| return dataset | ||
|
|
||
|
|
||
| class MockUnaryTransformer(UnaryTransformer): | ||
|
|
||
| shift = Param(Params._dummy(), "shift", "The amount by which to shift " + | ||
| "data in a DataFrame", | ||
| typeConverter=TypeConverters.toFloat) | ||
|
|
||
| def __init__(self, shiftVal=1): | ||
| super(MockUnaryTransformer, self).__init__() | ||
| self._setDefault(shift=1) | ||
| self._set(shift=shiftVal) | ||
|
|
||
| def getShift(self): | ||
| return self.getOrDefault(self.shift) | ||
|
|
||
| def setShift(self, shift): | ||
| self._set(shift=shift) | ||
|
|
||
| def createTransformFunc(self): | ||
| shiftVal = self.getShift() | ||
| return lambda x: x + shiftVal | ||
|
|
||
| def outputDataType(self): | ||
| return DoubleType() | ||
|
|
||
| def validateInputType(self, inputType): | ||
| if inputType != DoubleType(): | ||
| raise TypeError("Bad input type: {}. ".format(inputType) + | ||
| "Requires Integer.") | ||
|
|
||
|
|
||
| class MockEstimator(Estimator, HasFake): | ||
|
|
||
| def __init__(self): | ||
|
|
@@ -1957,6 +1988,40 @@ def test_chisquaretest(self): | |
| self.assertTrue(all(field in fieldNames for field in expectedFields)) | ||
|
|
||
|
|
||
| class UnaryTransformerTests(SparkSessionTestCase): | ||
|
|
||
| def test_unary_transformer_validate_input_type(self): | ||
| shiftVal = 3 | ||
| transformer = MockUnaryTransformer(shiftVal=shiftVal)\ | ||
| .setInputCol("input").setOutputCol("output") | ||
|
|
||
| # should not raise any errors | ||
| transformer.validateInputType(DoubleType()) | ||
|
|
||
| with self.assertRaises(TypeError): | ||
| # passing the wrong input type should raise an error | ||
| transformer.validateInputType(IntegerType()) | ||
|
|
||
| def test_unary_transformer_transform(self): | ||
| shiftVal = 3 | ||
| transformer = MockUnaryTransformer(shiftVal=shiftVal)\ | ||
| .setInputCol("input").setOutputCol("output") | ||
|
|
||
| df = self.spark.range(0, 10).toDF('input') | ||
| df = df.withColumn("input", df.input.cast(dataType="double")) | ||
|
|
||
| transformed_df = transformer.transform(df) | ||
| inputCol = transformed_df.select("input").collect() | ||
|
Member
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 this instead: |
||
| outputCol = transformed_df.select("output").collect() | ||
|
|
||
| original = range(10) | ||
| self.assertEqual(len(original), len(inputCol)) | ||
| self.assertEqual(len(original), len(outputCol)) | ||
| for i, out in enumerate(outputCol): | ||
| self.assertEqual(original[i], inputCol[i].input) | ||
| self.assertEqual(original[i] + shiftVal, out.output) | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| from pyspark.ml.tests import * | ||
| if xmlrunner: | ||
|
|
||
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.
Could you also please test validateInputType?