-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[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
Changes from 7 commits
ddf34a5
c1966bb
e5c8dcf
bd80b37
1f336ab
44f0a36
f66a445
36006bf
6acabc2
2fe9432
5bfa4dc
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2082,8 +2082,9 @@ 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 stringOrderType. | ||
|
|
||
| >>> stringIndexer = StringIndexer(inputCol="label", outputCol="indexed", handleInvalid='error') | ||
| >>> model = stringIndexer.fit(stringIndDf) | ||
|
|
@@ -2111,26 +2112,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.", | ||
|
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 think this should be generated instead of hardcoded - you can find a example on python..
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. @felixcheung
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. hmm, ok, I see a few examples that they are not generated
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. 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. |
||
| 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 +2159,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. | ||
|
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. should it say what the default value is?
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. OK, added default value. |
||
| """ | ||
| return self.getOrDefault(self.stringOrderType) | ||
|
|
||
|
|
||
| class StringIndexerModel(JavaModel, JavaMLReadable, JavaMLWritable): | ||
| """ | ||
|
|
||
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 you need to backtick and add a tag for the attribute
Uh oh!
There was an error while loading. Please reload this page.
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.
Added tag. Thanks