-
Notifications
You must be signed in to change notification settings - Fork 29k
[Spark-8530] [ML] add python API for MinMaxScaler #7150
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 5 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
77f57ef
add python API for MinMaxScaler
hhbyyh 3333ec9
Merge remote-tracking branch 'upstream/master' into pythonMinMax
hhbyyh 583bacf
resolve conflict
hhbyyh 86e8482
Merge remote-tracking branch 'upstream/master' into pythonMinMax
hhbyyh 7b97e6a
change ut and comment
hhbyyh 99042c5
merge upstream
hhbyyh 9785b56
add some comments
hhbyyh 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 |
|---|---|---|
|
|
@@ -27,10 +27,10 @@ | |
| from pyspark.mllib.linalg import _convert_to_vector | ||
|
|
||
| __all__ = ['Binarizer', 'Bucketizer', 'ElementwiseProduct', 'HashingTF', 'IDF', 'IDFModel', | ||
| 'NGram', 'Normalizer', 'OneHotEncoder', 'PolynomialExpansion', 'RegexTokenizer', | ||
| 'StandardScaler', 'StandardScalerModel', 'StringIndexer', 'StringIndexerModel', | ||
| 'Tokenizer', 'VectorAssembler', 'VectorIndexer', 'Word2Vec', 'Word2VecModel', | ||
| 'PCA', 'PCAModel', 'RFormula', 'RFormulaModel'] | ||
| 'MinMaxScaler', 'MinMaxScalerModel', 'NGram', 'Normalizer', 'OneHotEncoder', | ||
| 'PolynomialExpansion', 'RegexTokenizer', 'StandardScaler', 'StandardScalerModel', | ||
| 'StringIndexer', 'StringIndexerModel', 'Tokenizer', 'VectorAssembler', 'VectorIndexer', | ||
| 'Word2Vec', 'Word2VecModel', 'PCA', 'PCAModel', 'RFormula', 'RFormulaModel'] | ||
|
|
||
|
|
||
| @inherit_doc | ||
|
|
@@ -1291,6 +1291,96 @@ class RFormulaModel(JavaModel): | |
| """ | ||
|
|
||
|
|
||
| @inherit_doc | ||
| class MinMaxScaler(JavaEstimator, HasInputCol, HasOutputCol): | ||
| """ | ||
| Rescale each feature individually to a common range [min, max] linearly using column summary | ||
| statistics, which is also known as min-max normalization or Rescaling. The rescaled value for | ||
| feature E is calculated as, | ||
|
|
||
| Rescaled(e_i) = (e_i - E_min) / (E_max - E_min) * (max - min) + min | ||
|
|
||
| For the case E_max == E_min, Rescaled(e_i) = 0.5 * (max + min) | ||
|
|
||
| Note that since zero values will probably be transformed to non-zero values, output of the | ||
| transformer will be DenseVector even for sparse input. | ||
|
|
||
| >>> from pyspark.mllib.linalg import Vectors | ||
| >>> df = sqlContext.createDataFrame([(Vectors.dense([0.0]),), (Vectors.dense([2.0]),)], ["a"]) | ||
| >>> mmScaler = MinMaxScaler(inputCol="a", outputCol="scaled") | ||
| >>> model = mmScaler.fit(df) | ||
| >>> model.transform(df).show() | ||
| +-----+------+ | ||
| | a|scaled| | ||
| +-----+------+ | ||
| |[0.0]| [0.0]| | ||
| |[2.0]| [1.0]| | ||
| +-----+------+ | ||
| ... | ||
| """ | ||
|
|
||
| # a placeholder to make it appear in the generated doc | ||
| min = Param(Params._dummy(), "min", "Lower bound of the output feature range") | ||
| max = Param(Params._dummy(), "max", "Upper bound of the output feature range") | ||
|
|
||
| @keyword_only | ||
| def __init__(self, min=0.0, max=1.0, inputCol=None, outputCol=None): | ||
| """ | ||
| __init__(self, min=0.0, max=1.0, inputCol=None, outputCol=None) | ||
| """ | ||
| super(MinMaxScaler, self).__init__() | ||
| self._java_obj = self._new_java_obj("org.apache.spark.ml.feature.MinMaxScaler", self.uid) | ||
| self.min = Param(self, "min", "Lower bound of the output feature range") | ||
| self.max = Param(self, "max", "Upper bound of the output feature range") | ||
| self._setDefault(min=0.0, max=1.0) | ||
| kwargs = self.__init__._input_kwargs | ||
| self.setParams(**kwargs) | ||
|
|
||
| @keyword_only | ||
| def setParams(self, min=0.0, max=1.0, inputCol=None, outputCol=None): | ||
| """ | ||
| setParams(self, min=0.0, max=1.0, inputCol=None, outputCol=None) | ||
| Sets params for this MinMaxScaler. | ||
| """ | ||
| kwargs = self.setParams._input_kwargs | ||
| return self._set(**kwargs) | ||
|
|
||
| def setMin(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`min`. | ||
| """ | ||
| self._paramMap[self.min] = value | ||
| return self | ||
|
|
||
| def getMin(self): | ||
| """ | ||
| Gets the value of min or its default value. | ||
| """ | ||
| return self.getOrDefault(self.min) | ||
|
|
||
| def setMax(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`max`. | ||
| """ | ||
| self._paramMap[self.max] = value | ||
| return self | ||
|
|
||
| def getMax(self): | ||
| """ | ||
| Gets the value of max or its default value. | ||
| """ | ||
| return self.getOrDefault(self.max) | ||
|
|
||
| def _create_model(self, java_model): | ||
| return MinMaxScalerModel(java_model) | ||
|
|
||
|
|
||
| class MinMaxScalerModel(JavaModel): | ||
| """ | ||
| Model fitted by MinMaxScaler. | ||
|
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. Nicer to write: |
||
| """ | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| import doctest | ||
| from pyspark.context import SparkContext | ||
|
|
||
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.
mark with
.. note:: Experimental