-
Notifications
You must be signed in to change notification settings - Fork 28.9k
[SPARK-24333][ML][PYTHON]Add fit with validation set to spark.ml GBT: Python API #21465
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
8 commits
Select commit
Hold shift + click to select a range
aa27851
[SPARK-24333][ML][PYTHON]Add fit with validation set to spark.ml GBT:…
huaxingao 43ff084
add validationIndicatorCol in init
huaxingao c0e5757
change version to 3.0
huaxingao 3919057
address comment
huaxingao c0fcbb3
add GBTClassifierParams and GBTRegressorParams
huaxingao c0586bd
fix docstring problem
huaxingao 30a743d
address comments
huaxingao 6fc95a7
regenerate shared.py
huaxingao 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 |
|---|---|---|
|
|
@@ -23,7 +23,7 @@ | |
| from pyspark.ml import Estimator, Model | ||
| from pyspark.ml.param.shared import * | ||
| from pyspark.ml.regression import DecisionTreeModel, DecisionTreeRegressionModel, \ | ||
| RandomForestParams, TreeEnsembleModel, TreeEnsembleParams | ||
| GBTParams, HasVarianceImpurity, RandomForestParams, TreeEnsembleModel, TreeEnsembleParams | ||
| from pyspark.ml.util import * | ||
| from pyspark.ml.wrapper import JavaEstimator, JavaModel, JavaParams | ||
| from pyspark.ml.wrapper import JavaWrapper | ||
|
|
@@ -895,15 +895,6 @@ def getImpurity(self): | |
| return self.getOrDefault(self.impurity) | ||
|
|
||
|
|
||
| class GBTParams(TreeEnsembleParams): | ||
| """ | ||
| Private class to track supported GBT params. | ||
|
|
||
| .. versionadded:: 1.4.0 | ||
| """ | ||
| supportedLossTypes = ["logistic"] | ||
|
|
||
|
|
||
| @inherit_doc | ||
| class DecisionTreeClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, | ||
| HasProbabilityCol, HasRawPredictionCol, DecisionTreeParams, | ||
|
|
@@ -1174,9 +1165,31 @@ def trees(self): | |
| return [DecisionTreeClassificationModel(m) for m in list(self._call_java("trees"))] | ||
|
|
||
|
|
||
| class GBTClassifierParams(GBTParams, HasVarianceImpurity): | ||
| """ | ||
| Private class to track supported GBTClassifier params. | ||
|
|
||
| .. versionadded:: 3.0.0 | ||
| """ | ||
|
|
||
| supportedLossTypes = ["logistic"] | ||
|
|
||
| lossType = Param(Params._dummy(), "lossType", | ||
| "Loss function which GBT tries to minimize (case-insensitive). " + | ||
| "Supported options: " + ", ".join(supportedLossTypes), | ||
| typeConverter=TypeConverters.toString) | ||
|
|
||
| @since("1.4.0") | ||
| def getLossType(self): | ||
| """ | ||
| Gets the value of lossType or its default value. | ||
| """ | ||
| return self.getOrDefault(self.lossType) | ||
|
|
||
|
|
||
| @inherit_doc | ||
| class GBTClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, HasMaxIter, | ||
| GBTParams, HasCheckpointInterval, HasStepSize, HasSeed, JavaMLWritable, | ||
| class GBTClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, | ||
| GBTClassifierParams, HasCheckpointInterval, HasSeed, JavaMLWritable, | ||
| JavaMLReadable): | ||
| """ | ||
| `Gradient-Boosted Trees (GBTs) <http://en.wikipedia.org/wiki/Gradient_boosting>`_ | ||
|
|
@@ -1242,40 +1255,36 @@ class GBTClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol | |
| [0.25..., 0.23..., 0.21..., 0.19..., 0.18...] | ||
| >>> model.numClasses | ||
| 2 | ||
| >>> gbt = gbt.setValidationIndicatorCol("validationIndicator") | ||
| >>> gbt.getValidationIndicatorCol() | ||
| 'validationIndicator' | ||
| >>> gbt.getValidationTol() | ||
| 0.01 | ||
|
|
||
| .. versionadded:: 1.4.0 | ||
| """ | ||
|
|
||
| lossType = Param(Params._dummy(), "lossType", | ||
| "Loss function which GBT tries to minimize (case-insensitive). " + | ||
| "Supported options: " + ", ".join(GBTParams.supportedLossTypes), | ||
| typeConverter=TypeConverters.toString) | ||
|
|
||
| stepSize = Param(Params._dummy(), "stepSize", | ||
| "Step size (a.k.a. learning rate) in interval (0, 1] for shrinking " + | ||
| "the contribution of each estimator.", | ||
| typeConverter=TypeConverters.toFloat) | ||
|
|
||
| @keyword_only | ||
| def __init__(self, featuresCol="features", labelCol="label", predictionCol="prediction", | ||
| maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, | ||
| maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, lossType="logistic", | ||
| maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, | ||
| featureSubsetStrategy="all"): | ||
| maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, impurity="variance", | ||
|
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.
|
||
| featureSubsetStrategy="all", validationTol=0.01, validationIndicatorCol=None): | ||
| """ | ||
| __init__(self, featuresCol="features", labelCol="label", predictionCol="prediction", \ | ||
| maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, \ | ||
| maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, \ | ||
| lossType="logistic", maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, \ | ||
| featureSubsetStrategy="all") | ||
| impurity="variance", featureSubsetStrategy="all", validationTol=0.01, \ | ||
| validationIndicatorCol=None) | ||
| """ | ||
| super(GBTClassifier, self).__init__() | ||
| self._java_obj = self._new_java_obj( | ||
| "org.apache.spark.ml.classification.GBTClassifier", self.uid) | ||
| self._setDefault(maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, | ||
| maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, | ||
| lossType="logistic", maxIter=20, stepSize=0.1, subsamplingRate=1.0, | ||
| featureSubsetStrategy="all") | ||
| impurity="variance", featureSubsetStrategy="all", validationTol=0.01) | ||
| kwargs = self._input_kwargs | ||
| self.setParams(**kwargs) | ||
|
|
||
|
|
@@ -1285,13 +1294,15 @@ def setParams(self, featuresCol="features", labelCol="label", predictionCol="pre | |
| maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, | ||
| maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, | ||
| lossType="logistic", maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, | ||
| featureSubsetStrategy="all"): | ||
| impurity="variance", featureSubsetStrategy="all", validationTol=0.01, | ||
| validationIndicatorCol=None): | ||
| """ | ||
| setParams(self, featuresCol="features", labelCol="label", predictionCol="prediction", \ | ||
| maxDepth=5, maxBins=32, minInstancesPerNode=1, minInfoGain=0.0, \ | ||
| maxMemoryInMB=256, cacheNodeIds=False, checkpointInterval=10, \ | ||
| lossType="logistic", maxIter=20, stepSize=0.1, seed=None, subsamplingRate=1.0, \ | ||
| featureSubsetStrategy="all") | ||
| impurity="variance", featureSubsetStrategy="all", validationTol=0.01, \ | ||
| validationIndicatorCol=None) | ||
| Sets params for Gradient Boosted Tree Classification. | ||
| """ | ||
| kwargs = self._input_kwargs | ||
|
|
@@ -1307,20 +1318,20 @@ def setLossType(self, value): | |
| """ | ||
| return self._set(lossType=value) | ||
|
|
||
| @since("1.4.0") | ||
| def getLossType(self): | ||
| """ | ||
| Gets the value of lossType or its default value. | ||
| """ | ||
| return self.getOrDefault(self.lossType) | ||
|
|
||
| @since("2.4.0") | ||
| def setFeatureSubsetStrategy(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`featureSubsetStrategy`. | ||
| """ | ||
| return self._set(featureSubsetStrategy=value) | ||
|
|
||
| @since("3.0.0") | ||
| def setValidationIndicatorCol(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`validationIndicatorCol`. | ||
| """ | ||
| return self._set(validationIndicatorCol=value) | ||
|
|
||
|
|
||
| class GBTClassificationModel(TreeEnsembleModel, JavaClassificationModel, JavaMLWritable, | ||
| JavaMLReadable): | ||
|
|
||
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
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.
this should extend
TreeClassifierParamsThere 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.
@BryanCutler Thanks for your review.
Seems recently #22986 added
trait HasVarianceImpurityand madeprivate[ml] trait GBTClassifierParams extends GBTParams with HasVarianceImpurityThere 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.
ah, I see. let me take another look..
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.
Yeah, you're correct, this is fine