-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-20899][PySpark] PySpark supports stringIndexerOrderType in RFormula #18122
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 1 commit
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4bca4d9
Pyhton port for Rformula stringIndexerOrderType
actuaryzhang c3f4430
fix doc issue
actuaryzhang 3510e24
move test to test file
actuaryzhang 320203e
update test
actuaryzhang 4af4b35
fix test issues
actuaryzhang 2e854a8
improve tests
actuaryzhang 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 |
|---|---|---|
|
|
@@ -3032,6 +3032,18 @@ class RFormula(JavaEstimator, HasFeaturesCol, HasLabelCol, JavaMLReadable, JavaM | |
| ... | ||
| >>> str(loadedModel) | ||
| 'RFormulaModel(ResolvedRFormula(label=y, terms=[x,s], hasIntercept=true)) (uid=...)' | ||
| >>> rf = RFormula(formula="y ~ x + s", stringIndexerOrderType="alphabetDesc") | ||
| >>> rf.getStringIndexerOrderType() | ||
| 'alphabetDesc' | ||
| >>> rf.fit(df).transform(df).show() | ||
| +---+---+---+---------+-----+ | ||
| | y| x| s| features|label| | ||
| +---+---+---+---------+-----+ | ||
| |1.0|1.0| a|[1.0,0.0]| 1.0| | ||
| |0.0|2.0| b|[2.0,1.0]| 0.0| | ||
| |0.0|0.0| a|(2,[],[])| 0.0| | ||
| +---+---+---+---------+-----+ | ||
| ... | ||
|
|
||
| .. versionadded:: 1.5.0 | ||
| """ | ||
|
|
@@ -3043,26 +3055,35 @@ class RFormula(JavaEstimator, HasFeaturesCol, HasLabelCol, JavaMLReadable, JavaM | |
| "Force to index label whether it is numeric or string", | ||
| typeConverter=TypeConverters.toBoolean) | ||
|
|
||
| stringIndexerOrderType = Param(Params._dummy(), "stringIndexerOrderType", | ||
| "How to order categories of a string FEATURE column used by " + | ||
|
||
| "StringIndexer. The last category after ordering is dropped " + | ||
| "when encoding strings. Supported options: frequencyDesc, " + | ||
| "frequencyAsc, alphabetDesc, alphabetAsc. The default value " + | ||
| "is frequencyDesc. When the ordering is set to alphabetDesc, " + | ||
| "RFormula drops the same category as R when encoding strings.", | ||
| typeConverter=TypeConverters.toString) | ||
|
|
||
| @keyword_only | ||
| def __init__(self, formula=None, featuresCol="features", labelCol="label", | ||
| forceIndexLabel=False): | ||
| forceIndexLabel=False, stringIndexerOrderType="frequencyDesc"): | ||
| """ | ||
| __init__(self, formula=None, featuresCol="features", labelCol="label", \ | ||
| forceIndexLabel=False) | ||
| forceIndexLabel=False, stringIndexerOrderType="frequencyDesc") | ||
| """ | ||
| super(RFormula, self).__init__() | ||
| self._java_obj = self._new_java_obj("org.apache.spark.ml.feature.RFormula", self.uid) | ||
| self._setDefault(forceIndexLabel=False) | ||
| self._setDefault(forceIndexLabel=False, stringIndexerOrderType="frequencyDesc") | ||
| kwargs = self._input_kwargs | ||
| self.setParams(**kwargs) | ||
|
|
||
| @keyword_only | ||
| @since("1.5.0") | ||
| def setParams(self, formula=None, featuresCol="features", labelCol="label", | ||
| forceIndexLabel=False): | ||
| forceIndexLabel=False, stringIndexerOrderType="frequencyDesc"): | ||
| """ | ||
| setParams(self, formula=None, featuresCol="features", labelCol="label", \ | ||
| forceIndexLabel=False) | ||
| forceIndexLabel=False, stringIndexerOrderType="frequencyDesc") | ||
| Sets params for RFormula. | ||
| """ | ||
| kwargs = self._input_kwargs | ||
|
|
@@ -3096,6 +3117,20 @@ def getForceIndexLabel(self): | |
| """ | ||
| return self.getOrDefault(self.forceIndexLabel) | ||
|
|
||
| @since("2.3.0") | ||
| def setStringIndexerOrderType(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`stringIndexerOrderType`. | ||
| """ | ||
| return self._set(stringIndexerOrderType=value) | ||
|
|
||
| @since("2.3.0") | ||
| def getStringIndexerOrderType(self): | ||
| """ | ||
| Gets the value of :py:attr:`stringIndexerOrderType` or its default value 'frequencyDesc'. | ||
| """ | ||
| return self.getOrDefault(self.stringIndexerOrderType) | ||
|
|
||
| def _create_model(self, java_model): | ||
| return RFormulaModel(java_model) | ||
|
|
||
|
|
||
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 move the newly added test to
tests.py? We keep the basic doc tests here both for test and example, other tests should be placed attests.py. Thanks.