-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-28985][PYTHON][ML] Add common classes (JavaPredictor/JavaClassificationModel/JavaProbabilisticClassifier) in PYTHON #25776
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 3 commits
fed9831
d6c7da2
de6f8e8
95f88f5
bc1d9e1
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 |
|---|---|---|
|
|
@@ -26,8 +26,8 @@ | |
| DecisionTreeRegressionModel, GBTParams, HasVarianceImpurity, RandomForestParams, \ | ||
| TreeEnsembleModel | ||
| from pyspark.ml.util import * | ||
| from pyspark.ml.wrapper import JavaEstimator, JavaModel, JavaParams | ||
| from pyspark.ml.wrapper import JavaWrapper | ||
| from pyspark.ml.wrapper import JavaEstimator, JavaModel, JavaParams, \ | ||
| JavaPredictor, JavaPredictorParams, JavaPredictionModel, JavaWrapper | ||
| from pyspark.ml.common import inherit_doc, _java2py, _py2java | ||
| from pyspark.ml.linalg import Vectors | ||
| from pyspark.sql import DataFrame | ||
|
|
@@ -47,14 +47,43 @@ | |
| 'OneVsRest', 'OneVsRestModel'] | ||
|
|
||
|
|
||
| class JavaClassifierParams(HasRawPredictionCol, JavaPredictorParams): | ||
| """ | ||
| (Private) Java Classifier Params for classification tasks. | ||
| """ | ||
| pass | ||
|
|
||
|
|
||
| @inherit_doc | ||
| class JavaClassifier(JavaPredictor, JavaClassifierParams): | ||
| """ | ||
| Java Classifier for classification tasks. | ||
| Classes are indexed {0, 1, ..., numClasses - 1}. | ||
| """ | ||
|
|
||
| @since("3.0.0") | ||
| def setRawPredictionCol(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`rawPredictionCol`. | ||
| """ | ||
| return self._set(rawPredictionCol=value) | ||
|
|
||
|
|
||
| @inherit_doc | ||
| class JavaClassificationModel(JavaPredictionModel): | ||
| class JavaClassificationModel(JavaPredictionModel, JavaClassifierParams): | ||
| """ | ||
| (Private) Java Model produced by a ``Classifier``. | ||
| Java Model produced by a ``Classifier``. | ||
| Classes are indexed {0, 1, ..., numClasses - 1}. | ||
| To be mixed in with class:`pyspark.ml.JavaModel` | ||
| """ | ||
|
|
||
| @since("3.0.0") | ||
| def setRawPredictionCol(self, value): | ||
|
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. ditto |
||
| """ | ||
| Sets the value of :py:attr:`rawPredictionCol`. | ||
| """ | ||
| return self._set(rawPredictionCol=value) | ||
|
|
||
| @property | ||
| @since("2.1.0") | ||
| def numClasses(self): | ||
|
|
@@ -64,10 +93,60 @@ def numClasses(self): | |
| return self._call_java("numClasses") | ||
|
|
||
|
|
||
| class JavaProbabilisticClassifierParams(HasProbabilityCol, HasThresholds, JavaClassifierParams): | ||
| """ | ||
| (Private) Java Probabilistic Classifier Params for classification tasks. | ||
| """ | ||
| pass | ||
|
srowen marked this conversation as resolved.
|
||
|
|
||
|
|
||
| @inherit_doc | ||
| class LinearSVC(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, HasMaxIter, | ||
| HasRegParam, HasTol, HasRawPredictionCol, HasFitIntercept, HasStandardization, | ||
| HasWeightCol, HasAggregationDepth, HasThreshold, JavaMLWritable, JavaMLReadable): | ||
| class JavaProbabilisticClassifier(JavaClassifier, JavaProbabilisticClassifierParams): | ||
| """ | ||
| Java Probabilistic Classifier for classification tasks. | ||
| """ | ||
|
|
||
| @since("3.0.0") | ||
| def setProbabilityCol(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`probabilityCol`. | ||
| """ | ||
| return self._set(probabilityCol=value) | ||
|
|
||
| @since("3.0.0") | ||
| def setThresholds(self, value): | ||
|
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. ditto |
||
| """ | ||
| Sets the value of :py:attr:`thresholds`. | ||
| """ | ||
| return self._set(thresholds=value) | ||
|
|
||
|
|
||
| @inherit_doc | ||
| class JavaProbabilisticClassificationModel(JavaClassificationModel, | ||
| JavaProbabilisticClassifierParams): | ||
| """ | ||
| Java Model produced by a ``ProbabilisticClassifier``. | ||
| """ | ||
|
|
||
| @since("3.0.0") | ||
| def setProbabilityCol(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`probabilityCol`. | ||
| """ | ||
| return self._set(probabilityCol=value) | ||
|
|
||
| @since("3.0.0") | ||
| def setThresholds(self, value): | ||
| """ | ||
| Sets the value of :py:attr:`thresholds`. | ||
| """ | ||
| return self._set(thresholds=value) | ||
|
|
||
|
|
||
| @inherit_doc | ||
| class LinearSVC(JavaClassifier, HasMaxIter, HasRegParam, HasTol, | ||
| HasFitIntercept, HasStandardization, HasWeightCol, HasAggregationDepth, | ||
| HasThreshold, JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| `Linear SVM Classifier <https://en.wikipedia.org/wiki/Support_vector_machine#Linear_SVM>`_ | ||
|
|
||
|
|
@@ -81,6 +160,8 @@ class LinearSVC(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, Ha | |
| ... Row(label=0.0, features=Vectors.dense(1.0, 2.0, 3.0))]).toDF() | ||
| >>> svm = LinearSVC(maxIter=5, regParam=0.01) | ||
| >>> model = svm.fit(df) | ||
| >>> model.setPredictionCol("prediction") | ||
|
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. What about changing the value to a non-default value like "newPrediction", and making sure that the ouput dataframe/row has changed column name? |
||
| LinearSVC... | ||
| >>> model.coefficients | ||
| DenseVector([0.0, -0.2792, -0.1833]) | ||
| >>> model.intercept | ||
|
|
@@ -90,6 +171,8 @@ class LinearSVC(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, Ha | |
| >>> model.numFeatures | ||
| 3 | ||
| >>> test0 = sc.parallelize([Row(features=Vectors.dense(-1.0, -1.0, -1.0))]).toDF() | ||
| >>> model.predict(test0.head().features) | ||
| 1.0 | ||
| >>> result = model.transform(test0).head() | ||
| >>> result.prediction | ||
| 1.0 | ||
|
|
@@ -156,7 +239,7 @@ def _create_model(self, java_model): | |
| return LinearSVCModel(java_model) | ||
|
|
||
|
|
||
| class LinearSVCModel(JavaModel, JavaClassificationModel, JavaMLWritable, JavaMLReadable): | ||
| class LinearSVCModel(JavaClassificationModel, JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| Model fitted by LinearSVC. | ||
|
|
||
|
|
@@ -181,8 +264,7 @@ def intercept(self): | |
|
|
||
|
|
||
| @inherit_doc | ||
| class LogisticRegression(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, HasMaxIter, | ||
| HasRegParam, HasTol, HasProbabilityCol, HasRawPredictionCol, | ||
| class LogisticRegression(JavaProbabilisticClassifier, HasMaxIter, HasRegParam, HasTol, | ||
| HasElasticNetParam, HasFitIntercept, HasStandardization, HasThresholds, | ||
| HasWeightCol, HasAggregationDepth, JavaMLWritable, JavaMLReadable): | ||
| """ | ||
|
|
@@ -198,6 +280,8 @@ class LogisticRegression(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredicti | |
| ... Row(label=0.0, weight=4.0, features=Vectors.dense(3.0, 3.0))]).toDF() | ||
| >>> blor = LogisticRegression(regParam=0.01, weightCol="weight") | ||
| >>> blorModel = blor.fit(bdf) | ||
| >>> blorModel.setFeaturesCol("features") | ||
| LogisticRegressionModel... | ||
| >>> blorModel.coefficients | ||
| DenseVector([-1.080..., -0.646...]) | ||
| >>> blorModel.intercept | ||
|
|
@@ -211,6 +295,8 @@ class LogisticRegression(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredicti | |
| >>> mlorModel.interceptVector | ||
| DenseVector([0.04..., -0.42..., 0.37...]) | ||
| >>> test0 = sc.parallelize([Row(features=Vectors.dense(-1.0, 1.0))]).toDF() | ||
| >>> blorModel.predict(test0.head().features) | ||
| 1.0 | ||
| >>> result = blorModel.transform(test0).head() | ||
| >>> result.prediction | ||
| 1.0 | ||
|
|
@@ -481,7 +567,7 @@ def getUpperBoundsOnIntercepts(self): | |
| return self.getOrDefault(self.upperBoundsOnIntercepts) | ||
|
|
||
|
|
||
| class LogisticRegressionModel(JavaModel, JavaClassificationModel, JavaMLWritable, JavaMLReadable, | ||
| class LogisticRegressionModel(JavaProbabilisticClassificationModel, JavaMLWritable, JavaMLReadable, | ||
| HasTrainingSummary): | ||
| """ | ||
| Model fitted by LogisticRegression. | ||
|
|
@@ -872,8 +958,7 @@ def getImpurity(self): | |
|
|
||
|
|
||
| @inherit_doc | ||
| class DecisionTreeClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasWeightCol, | ||
| HasPredictionCol, HasProbabilityCol, HasRawPredictionCol, | ||
| class DecisionTreeClassifier(JavaProbabilisticClassifier, HasWeightCol, | ||
| DecisionTreeParams, TreeClassifierParams, HasCheckpointInterval, | ||
| HasSeed, JavaMLWritable, JavaMLReadable): | ||
| """ | ||
|
|
@@ -892,6 +977,10 @@ class DecisionTreeClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasWeig | |
| >>> td = si_model.transform(df) | ||
| >>> dt = DecisionTreeClassifier(maxDepth=2, labelCol="indexed", leafCol="leafId") | ||
| >>> model = dt.fit(td) | ||
| >>> model.getLabelCol() | ||
| 'indexed' | ||
| >>> model.setFeaturesCol("features") | ||
| DecisionTreeClassificationModel... | ||
| >>> model.numNodes | ||
| 3 | ||
| >>> model.depth | ||
|
|
@@ -905,6 +994,8 @@ class DecisionTreeClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasWeig | |
| >>> print(model.toDebugString) | ||
| DecisionTreeClassificationModel (uid=...) of depth 1 with 3 nodes... | ||
| >>> test0 = spark.createDataFrame([(Vectors.dense(-1.0),)], ["features"]) | ||
| >>> model.predict(test0.head().features) | ||
| 0.0 | ||
| >>> result = model.transform(test0).head() | ||
| >>> result.prediction | ||
| 0.0 | ||
|
|
@@ -1031,8 +1122,8 @@ def setImpurity(self, value): | |
|
|
||
|
|
||
| @inherit_doc | ||
| class DecisionTreeClassificationModel(DecisionTreeModel, JavaClassificationModel, JavaMLWritable, | ||
| JavaMLReadable): | ||
| class DecisionTreeClassificationModel(DecisionTreeModel, JavaProbabilisticClassificationModel, | ||
| JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| Model fitted by DecisionTreeClassifier. | ||
|
|
||
|
|
@@ -1062,9 +1153,8 @@ def featureImportances(self): | |
|
|
||
|
|
||
| @inherit_doc | ||
| class RandomForestClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, HasSeed, | ||
| HasRawPredictionCol, HasProbabilityCol, | ||
| RandomForestParams, TreeClassifierParams, HasCheckpointInterval, | ||
| class RandomForestClassifier(JavaProbabilisticClassifier, HasSeed, RandomForestParams, | ||
| TreeClassifierParams, HasCheckpointInterval, | ||
| JavaMLWritable, JavaMLReadable): | ||
|
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. What about adding some simple tests for it? |
||
| """ | ||
| `Random Forest <http://en.wikipedia.org/wiki/Random_forest>`_ | ||
|
|
@@ -1231,8 +1321,8 @@ def setFeatureSubsetStrategy(self, value): | |
| return self._set(featureSubsetStrategy=value) | ||
|
|
||
|
|
||
| class RandomForestClassificationModel(TreeEnsembleModel, JavaClassificationModel, JavaMLWritable, | ||
| JavaMLReadable): | ||
| class RandomForestClassificationModel(TreeEnsembleModel, JavaProbabilisticClassificationModel, | ||
| JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| Model fitted by RandomForestClassifier. | ||
|
|
||
|
|
@@ -1284,9 +1374,8 @@ def getLossType(self): | |
|
|
||
|
|
||
| @inherit_doc | ||
| class GBTClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, | ||
| GBTClassifierParams, HasCheckpointInterval, HasSeed, JavaMLWritable, | ||
| JavaMLReadable): | ||
| class GBTClassifier(JavaProbabilisticClassifier, GBTClassifierParams, HasCheckpointInterval, | ||
| HasSeed, JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| `Gradient-Boosted Trees (GBTs) <http://en.wikipedia.org/wiki/Gradient_boosting>`_ | ||
| learning algorithm for classification. | ||
|
|
@@ -1318,11 +1407,17 @@ class GBTClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol | |
| >>> gbt.getFeatureSubsetStrategy() | ||
| 'all' | ||
| >>> model = gbt.fit(td) | ||
| >>> model.getLabelCol() | ||
| 'indexed' | ||
| >>> model.setFeaturesCol("features") | ||
| GBTClassificationModel... | ||
| >>> model.featureImportances | ||
| SparseVector(1, {0: 1.0}) | ||
| >>> allclose(model.treeWeights, [1.0, 0.1, 0.1, 0.1, 0.1]) | ||
| True | ||
| >>> test0 = spark.createDataFrame([(Vectors.dense(-1.0),)], ["features"]) | ||
| >>> model.predict(test0.head().features) | ||
| 0.0 | ||
| >>> result = model.transform(test0).head() | ||
| >>> result.prediction | ||
| 0.0 | ||
|
|
@@ -1485,8 +1580,8 @@ def setValidationIndicatorCol(self, value): | |
| return self._set(validationIndicatorCol=value) | ||
|
|
||
|
|
||
| class GBTClassificationModel(TreeEnsembleModel, JavaClassificationModel, JavaMLWritable, | ||
| JavaMLReadable): | ||
| class GBTClassificationModel(TreeEnsembleModel, JavaProbabilisticClassificationModel, | ||
| JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| Model fitted by GBTClassifier. | ||
|
|
||
|
|
@@ -1527,8 +1622,8 @@ def evaluateEachIteration(self, dataset): | |
|
|
||
|
|
||
| @inherit_doc | ||
| class NaiveBayes(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, HasProbabilityCol, | ||
| HasRawPredictionCol, HasThresholds, HasWeightCol, JavaMLWritable, JavaMLReadable): | ||
| class NaiveBayes(JavaProbabilisticClassifier, HasThresholds, HasWeightCol, | ||
| JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| Naive Bayes Classifiers. | ||
| It supports both Multinomial and Bernoulli NB. `Multinomial NB | ||
|
|
@@ -1547,11 +1642,15 @@ class NaiveBayes(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, H | |
| ... Row(label=1.0, weight=1.0, features=Vectors.dense([1.0, 0.0]))]) | ||
| >>> nb = NaiveBayes(smoothing=1.0, modelType="multinomial", weightCol="weight") | ||
| >>> model = nb.fit(df) | ||
| >>> model.setFeaturesCol("features") | ||
| NaiveBayes_... | ||
| >>> model.pi | ||
| DenseVector([-0.81..., -0.58...]) | ||
| >>> model.theta | ||
| DenseMatrix(2, 2, [-0.91..., -0.51..., -0.40..., -1.09...], 1) | ||
| >>> test0 = sc.parallelize([Row(features=Vectors.dense([1.0, 0.0]))]).toDF() | ||
| >>> model.predict(test0.head().features) | ||
| 1.0 | ||
| >>> result = model.transform(test0).head() | ||
| >>> result.prediction | ||
| 1.0 | ||
|
|
@@ -1651,7 +1750,7 @@ def getModelType(self): | |
| return self.getOrDefault(self.modelType) | ||
|
|
||
|
|
||
| class NaiveBayesModel(JavaModel, JavaClassificationModel, JavaMLWritable, JavaMLReadable): | ||
| class NaiveBayesModel(JavaProbabilisticClassificationModel, JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| Model fitted by NaiveBayes. | ||
|
|
||
|
|
@@ -1676,10 +1775,8 @@ def theta(self): | |
|
|
||
|
|
||
| @inherit_doc | ||
| class MultilayerPerceptronClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, HasPredictionCol, | ||
| HasMaxIter, HasTol, HasSeed, HasStepSize, HasSolver, | ||
| JavaMLWritable, JavaMLReadable, HasProbabilityCol, | ||
| HasRawPredictionCol): | ||
| class MultilayerPerceptronClassifier(JavaProbabilisticClassifier, HasMaxIter, HasTol, HasSeed, | ||
| HasStepSize, HasSolver, JavaMLWritable, JavaMLReadable): | ||
| """ | ||
| Classifier trainer based on the Multilayer Perceptron. | ||
| Each layer has sigmoid activation function, output layer has softmax. | ||
|
|
@@ -1694,13 +1791,17 @@ class MultilayerPerceptronClassifier(JavaEstimator, HasFeaturesCol, HasLabelCol, | |
| ... (0.0, Vectors.dense([1.0, 1.0]))], ["label", "features"]) | ||
| >>> mlp = MultilayerPerceptronClassifier(maxIter=100, layers=[2, 2, 2], blockSize=1, seed=123) | ||
| >>> model = mlp.fit(df) | ||
| >>> model.setFeaturesCol("features") | ||
| MultilayerPerceptronClassifier... | ||
| >>> model.layers | ||
| [2, 2, 2] | ||
| >>> model.weights.size | ||
| 12 | ||
| >>> testDF = spark.createDataFrame([ | ||
| ... (Vectors.dense([1.0, 0.0]),), | ||
| ... (Vectors.dense([0.0, 0.0]),)], ["features"]) | ||
| >>> model.predict(testDF.head().features) | ||
| 1.0 | ||
| >>> model.transform(testDF).select("features", "prediction").show() | ||
| +---------+----------+ | ||
| | features|prediction| | ||
|
|
@@ -1839,7 +1940,7 @@ def getInitialWeights(self): | |
| return self.getOrDefault(self.initialWeights) | ||
|
|
||
|
|
||
| class MultilayerPerceptronClassificationModel(JavaModel, JavaClassificationModel, JavaMLWritable, | ||
| class MultilayerPerceptronClassificationModel(JavaProbabilisticClassificationModel, JavaMLWritable, | ||
| JavaMLReadable): | ||
| """ | ||
| Model fitted by MultilayerPerceptronClassifier. | ||
|
|
@@ -1864,8 +1965,7 @@ def weights(self): | |
| return self._call_java("weights") | ||
|
|
||
|
|
||
| class OneVsRestParams(HasFeaturesCol, HasLabelCol, HasWeightCol, HasPredictionCol, | ||
| HasRawPredictionCol): | ||
| class OneVsRestParams(JavaClassifierParams, HasWeightCol): | ||
| """ | ||
| Parameters for OneVsRest and OneVsRestModel. | ||
| """ | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.