-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-21306][ML] For branch 2.1, OneVsRest should support setWeightCol #18763
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
facaiy
wants to merge
3
commits into
apache:branch-2.1
from
facaiy:BUG/branch-2.1_OneVsRest_support_setWeightCol
Closed
Changes from 2 commits
Commits
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 |
|---|---|---|
|
|
@@ -1331,7 +1331,7 @@ def weights(self): | |
| return self._call_java("weights") | ||
|
|
||
|
|
||
| class OneVsRestParams(HasFeaturesCol, HasLabelCol, HasPredictionCol): | ||
| class OneVsRestParams(HasFeaturesCol, HasLabelCol, HasWeightCol, HasPredictionCol): | ||
| """ | ||
| Parameters for OneVsRest and OneVsRestModel. | ||
| """ | ||
|
|
@@ -1394,20 +1394,22 @@ class OneVsRest(Estimator, OneVsRestParams, MLReadable, MLWritable): | |
|
|
||
| @keyword_only | ||
| def __init__(self, featuresCol="features", labelCol="label", predictionCol="prediction", | ||
| classifier=None): | ||
| classifier=None, weightCol=None): | ||
| """ | ||
| __init__(self, featuresCol="features", labelCol="label", predictionCol="prediction", \ | ||
| classifier=None) | ||
| classifier=None, weightCol=None) | ||
| """ | ||
| super(OneVsRest, self).__init__() | ||
| kwargs = self._input_kwargs | ||
| self._set(**kwargs) | ||
|
|
||
| @keyword_only | ||
| @since("2.0.0") | ||
| def setParams(self, featuresCol=None, labelCol=None, predictionCol=None, classifier=None): | ||
| def setParams(self, featuresCol=None, labelCol=None, predictionCol=None, | ||
| classifier=None, weightCol=None): | ||
| """ | ||
| setParams(self, featuresCol=None, labelCol=None, predictionCol=None, classifier=None): | ||
| setParams(self, featuresCol=None, labelCol=None, predictionCol=None, \ | ||
| classifier=None, weightCol=None): | ||
| Sets params for OneVsRest. | ||
| """ | ||
| kwargs = self._input_kwargs | ||
|
|
@@ -1423,7 +1425,18 @@ def _fit(self, dataset): | |
|
|
||
| numClasses = int(dataset.agg({labelCol: "max"}).head()["max("+labelCol+")"]) + 1 | ||
|
|
||
| multiclassLabeled = dataset.select(labelCol, featuresCol) | ||
| weightCol = None | ||
| if (self.isDefined(self.weightCol) and self.getWeightCol()): | ||
| if isinstance(classifier, HasWeightCol): | ||
| weightCol = self.getWeightCol() | ||
| else: | ||
| warnings.warn("weightCol is ignored, " | ||
| "as it is not supported by {} now.".format(classifier)) | ||
|
||
|
|
||
| if weightCol: | ||
| multiclassLabeled = dataset.select(labelCol, featuresCol, weightCol) | ||
| else: | ||
| multiclassLabeled = dataset.select(labelCol, featuresCol) | ||
|
|
||
| # persist if underlying dataset is not persistent. | ||
| handlePersistence = \ | ||
|
|
@@ -1439,6 +1452,8 @@ def trainSingleClass(index): | |
| paramMap = dict([(classifier.labelCol, binaryLabelCol), | ||
| (classifier.featuresCol, featuresCol), | ||
| (classifier.predictionCol, predictionCol)]) | ||
| if weightCol: | ||
| paramMap[classifier.weightCol] = weightCol | ||
| return classifier.fit(trainingDataset, paramMap) | ||
|
|
||
| # TODO: Parallel training for all classes. | ||
|
|
||
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
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.
@yanboliang @srowen My mistake. It will be better to use double value 1.0 in master / branch-2.2 / branch-2.3 in the same way.
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.
@facaiy It doesn't matter, since
weightColsupports any numeric type and will cast it to double type under the hood.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.
OK. Could you test and merge the PR into branch 2.1?