-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-20736][Python] PySpark StringIndexer supports StringOrderType #17978
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
11 commits
Select commit
Hold shift + click to select a range
ddf34a5
Python API to StringOrderType in StringIndexer
c1966bb
fix typo
e5c8dcf
fix typo
bd80b37
fix style
1f336ab
fix style
44f0a36
add tests
f66a445
fix test error
36006bf
address comments
6acabc2
minor style fix
2fe9432
add default value for stringOrderType in docstring
5bfa4dc
fix example error
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 |
|---|---|---|
|
|
@@ -2082,10 +2082,12 @@ class StringIndexer(JavaEstimator, HasInputCol, HasOutputCol, HasHandleInvalid, | |
| """ | ||
| A label indexer that maps a string column of labels to an ML column of label indices. | ||
| If the input column is numeric, we cast it to string and index the string values. | ||
| The indices are in [0, numLabels), ordered by label frequencies. | ||
| So the most frequent label gets index 0. | ||
| The indices are in [0, numLabels). By default, this is ordered by label frequencies | ||
| so the most frequent label gets index 0. The ordering behavior is controlled by | ||
| setting :py:attr:`stringOrderType`. Its default value is 'frequencyDesc'. | ||
|
|
||
| >>> stringIndexer = StringIndexer(inputCol="label", outputCol="indexed", handleInvalid='error') | ||
| >>> stringIndexer = StringIndexer(inputCol="label", outputCol="indexed", handleInvalid="error", | ||
| ... stringOrderType="frequencyDesc") | ||
| >>> model = stringIndexer.fit(stringIndDf) | ||
| >>> td = model.transform(stringIndDf) | ||
| >>> sorted(set([(i[0], i[1]) for i in td.select(td.id, td.indexed).collect()]), | ||
|
|
@@ -2111,26 +2113,45 @@ class StringIndexer(JavaEstimator, HasInputCol, HasOutputCol, HasHandleInvalid, | |
| >>> loadedInverter = IndexToString.load(indexToStringPath) | ||
| >>> loadedInverter.getLabels() == inverter.getLabels() | ||
| True | ||
| >>> stringIndexer.getStringOrderType() | ||
| 'frequencyDesc' | ||
| >>> stringIndexer = StringIndexer(inputCol="label", outputCol="indexed", handleInvalid="error", | ||
| ... stringOrderType="alphabetDesc") | ||
| >>> model = stringIndexer.fit(stringIndDf) | ||
| >>> td = model.transform(stringIndDf) | ||
| >>> sorted(set([(i[0], i[1]) for i in td.select(td.id, td.indexed).collect()]), | ||
| ... key=lambda x: x[0]) | ||
| [(0, 2.0), (1, 1.0), (2, 0.0), (3, 2.0), (4, 2.0), (5, 0.0)] | ||
|
|
||
| .. versionadded:: 1.4.0 | ||
| """ | ||
|
|
||
| stringOrderType = Param(Params._dummy(), "stringOrderType", | ||
| "How to order labels of string column. The first label after " + | ||
| "ordering is assigned an index of 0. Supported options: " + | ||
| "frequencyDesc, frequencyAsc, alphabetDesc, alphabetAsc.", | ||
| typeConverter=TypeConverters.toString) | ||
|
|
||
| @keyword_only | ||
| def __init__(self, inputCol=None, outputCol=None, handleInvalid="error"): | ||
| def __init__(self, inputCol=None, outputCol=None, handleInvalid="error", | ||
| stringOrderType="frequencyDesc"): | ||
| """ | ||
| __init__(self, inputCol=None, outputCol=None, handleInvalid="error") | ||
| __init__(self, inputCol=None, outputCol=None, handleInvalid="error", \ | ||
|
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. I guess we need at least a doctest.
Contributor
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. @HyukjinKwon Thank you. Added tests. |
||
| stringOrderType="frequencyDesc") | ||
| """ | ||
| super(StringIndexer, self).__init__() | ||
| self._java_obj = self._new_java_obj("org.apache.spark.ml.feature.StringIndexer", self.uid) | ||
| self._setDefault(handleInvalid="error") | ||
| self._setDefault(handleInvalid="error", stringOrderType="frequencyDesc") | ||
| kwargs = self._input_kwargs | ||
| self.setParams(**kwargs) | ||
|
|
||
| @keyword_only | ||
| @since("1.4.0") | ||
| def setParams(self, inputCol=None, outputCol=None, handleInvalid="error"): | ||
| def setParams(self, inputCol=None, outputCol=None, handleInvalid="error", | ||
| stringOrderType="frequencyDesc"): | ||
| """ | ||
| setParams(self, inputCol=None, outputCol=None, handleInvalid="error") | ||
| setParams(self, inputCol=None, outputCol=None, handleInvalid="error", \ | ||
| stringOrderType="frequencyDesc") | ||
| Sets params for this StringIndexer. | ||
| """ | ||
| kwargs = self._input_kwargs | ||
|
|
@@ -2139,6 +2160,20 @@ def setParams(self, inputCol=None, outputCol=None, handleInvalid="error"): | |
| def _create_model(self, java_model): | ||
| return StringIndexerModel(java_model) | ||
|
|
||
| @since("2.3.0") | ||
| def setStringOrderType(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`stringOrderType`. | ||
| """ | ||
| return self._set(stringOrderType=value) | ||
|
|
||
| @since("2.3.0") | ||
| def getStringOrderType(self): | ||
| """ | ||
| Gets the value of :py:attr:`stringOrderType` or its default value 'frequencyDesc'. | ||
| """ | ||
| return self.getOrDefault(self.stringOrderType) | ||
|
|
||
|
|
||
| class StringIndexerModel(JavaModel, JavaMLReadable, JavaMLWritable): | ||
| """ | ||
|
|
||
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.
I think this should be generated instead of hardcoded - you can find a example on python..
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.
@felixcheung
stringOrderTypeis not a shared trait on the Scala side, and I thought only the shared traits should be automatically generated.I have looked at the code for other ML transformers, and many of them hard coded, for example, Imputer and OneHotEncoder.
Please let me know if I'm wrong, and a reference to example would be greatly appreciated. Thanks much!
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.
hmm, ok, I see a few examples that they are not generated
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.
I know were mixed on doing this, but I like including the default value in the docstring, makes the documentation closer to the Scala doc and makes it easier to read without having to refer to the ScalaDoc.