-
Notifications
You must be signed in to change notification settings - Fork 29.2k
[SPARK-21088][ML] CrossValidator, TrainValidationSplit support collect all models when fitting: Python API #19627
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
6 commits
Select commit
Hold shift + click to select a range
1edd66b
init pr
WeichenXu123 9e27f6b
add submodels save load support
WeichenXu123 758bc24
fix_RAT_check
WeichenXu123 ae082f5
fix python style
WeichenXu123 81473b0
merge master & update code logic
WeichenXu123 80f07fb
address comments
WeichenXu123 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
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 |
|---|---|---|
|
|
@@ -1018,6 +1018,48 @@ def test_parallel_evaluation(self): | |
| cvParallelModel = cv.fit(dataset) | ||
| self.assertEqual(cvSerialModel.avgMetrics, cvParallelModel.avgMetrics) | ||
|
|
||
| def test_expose_sub_models(self): | ||
|
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. Nice tests. Can you make one addition: Test the copy() method to make sure it copies the submodels. |
||
| temp_path = tempfile.mkdtemp() | ||
| dataset = self.spark.createDataFrame( | ||
| [(Vectors.dense([0.0]), 0.0), | ||
| (Vectors.dense([0.4]), 1.0), | ||
| (Vectors.dense([0.5]), 0.0), | ||
| (Vectors.dense([0.6]), 1.0), | ||
| (Vectors.dense([1.0]), 1.0)] * 10, | ||
| ["features", "label"]) | ||
|
|
||
| lr = LogisticRegression() | ||
| grid = ParamGridBuilder().addGrid(lr.maxIter, [0, 1]).build() | ||
| evaluator = BinaryClassificationEvaluator() | ||
|
|
||
| numFolds = 3 | ||
| cv = CrossValidator(estimator=lr, estimatorParamMaps=grid, evaluator=evaluator, | ||
| numFolds=numFolds, collectSubModels=True) | ||
|
|
||
| def checkSubModels(subModels): | ||
| assert len(subModels) == numFolds | ||
| for i in range(numFolds): | ||
| assert len(subModels[i]) == len(grid) | ||
|
|
||
| cvModel = cv.fit(dataset) | ||
| checkSubModels(cvModel.subModels) | ||
|
|
||
| # Test the default value for option "persistSubModel" to be "true" | ||
| testSubPath = temp_path + "/testCrossValidatorSubModels" | ||
| savingPathWithSubModels = testSubPath + "cvModel3" | ||
| cvModel.save(savingPathWithSubModels) | ||
| cvModel3 = CrossValidatorModel.load(savingPathWithSubModels) | ||
| checkSubModels(cvModel3.subModels) | ||
|
|
||
| savingPathWithoutSubModels = testSubPath + "cvModel2" | ||
| cvModel.write().option("persistSubModels", "false").save(savingPathWithoutSubModels) | ||
| cvModel2 = CrossValidatorModel.load(savingPathWithoutSubModels) | ||
| assert cvModel2.subModels is None | ||
|
|
||
| for i in range(numFolds): | ||
| for j in range(len(grid)): | ||
| self.assertEqual(cvModel.subModels[i][j].uid, cvModel3.subModels[i][j].uid) | ||
|
|
||
| def test_save_load_nested_estimator(self): | ||
| temp_path = tempfile.mkdtemp() | ||
| dataset = self.spark.createDataFrame( | ||
|
|
@@ -1186,6 +1228,38 @@ def test_parallel_evaluation(self): | |
| tvsParallelModel = tvs.fit(dataset) | ||
| self.assertEqual(tvsSerialModel.validationMetrics, tvsParallelModel.validationMetrics) | ||
|
|
||
| def test_expose_sub_models(self): | ||
| temp_path = tempfile.mkdtemp() | ||
| dataset = self.spark.createDataFrame( | ||
| [(Vectors.dense([0.0]), 0.0), | ||
| (Vectors.dense([0.4]), 1.0), | ||
| (Vectors.dense([0.5]), 0.0), | ||
| (Vectors.dense([0.6]), 1.0), | ||
| (Vectors.dense([1.0]), 1.0)] * 10, | ||
| ["features", "label"]) | ||
| lr = LogisticRegression() | ||
| grid = ParamGridBuilder().addGrid(lr.maxIter, [0, 1]).build() | ||
| evaluator = BinaryClassificationEvaluator() | ||
| tvs = TrainValidationSplit(estimator=lr, estimatorParamMaps=grid, evaluator=evaluator, | ||
| collectSubModels=True) | ||
| tvsModel = tvs.fit(dataset) | ||
| assert len(tvsModel.subModels) == len(grid) | ||
|
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. Use self.assertEqual here and elsewhere. |
||
|
|
||
| # Test the default value for option "persistSubModel" to be "true" | ||
| testSubPath = temp_path + "/testTrainValidationSplitSubModels" | ||
| savingPathWithSubModels = testSubPath + "cvModel3" | ||
| tvsModel.save(savingPathWithSubModels) | ||
| tvsModel3 = TrainValidationSplitModel.load(savingPathWithSubModels) | ||
| assert len(tvsModel3.subModels) == len(grid) | ||
|
|
||
| savingPathWithoutSubModels = testSubPath + "cvModel2" | ||
| tvsModel.write().option("persistSubModels", "false").save(savingPathWithoutSubModels) | ||
| tvsModel2 = TrainValidationSplitModel.load(savingPathWithoutSubModels) | ||
| assert tvsModel2.subModels is None | ||
|
|
||
| for i in range(len(grid)): | ||
| self.assertEqual(tvsModel.subModels[i].uid, tvsModel3.subModels[i].uid) | ||
|
|
||
| def test_save_load_nested_estimator(self): | ||
| # This tests saving and loading the trained model only. | ||
| # Save/load for TrainValidationSplit will be added later: SPARK-13786 | ||
|
|
||
Oops, something went wrong.
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.
It would be nice to add the full description from Scala.