Skip to content
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

[SPARK-29464][PYTHON][ML] PySpark ML should expose Params.clear() to unset a user supplied Param #26130

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions python/pyspark/ml/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ def setThreshold(self, value):
Clears value of :py:attr:`thresholds` if it has been set.
"""
self._set(threshold=value)
self._clear(self.thresholds)
self.clear(self.thresholds)
return self

@since("1.4.0")
Expand Down Expand Up @@ -477,7 +477,7 @@ def setThresholds(self, value):
Clears value of :py:attr:`threshold` if it has been set.
"""
self._set(thresholds=value)
self._clear(self.threshold)
self.clear(self.threshold)
return self

@since("1.5.0")
Expand Down
2 changes: 1 addition & 1 deletion python/pyspark/ml/param/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -452,7 +452,7 @@ def _set(self, **kwargs):
self._paramMap[p] = value
return self

def _clear(self, param):
def clear(self, param):
"""
Clears a param from the param map if it has been explicitly set.
"""
Expand Down
20 changes: 18 additions & 2 deletions python/pyspark/ml/tests/test_param.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
from pyspark.ml.classification import LogisticRegression
from pyspark.ml.clustering import KMeans
from pyspark.ml.feature import Binarizer, Bucketizer, ElementwiseProduct, IndexToString, \
VectorSlicer, Word2Vec
from pyspark.ml.linalg import DenseVector, SparseVector
MaxAbsScaler, VectorSlicer, Word2Vec
from pyspark.ml.linalg import DenseVector, SparseVector, Vectors
from pyspark.ml.param import Param, Params, TypeConverters
from pyspark.ml.param.shared import HasInputCol, HasMaxIter, HasSeed
from pyspark.ml.wrapper import JavaParams
Expand Down Expand Up @@ -224,6 +224,10 @@ def test_params(self):
testParams.setMaxIter(100)
self.assertTrue(testParams.isSet(maxIter))
self.assertEqual(testParams.getMaxIter(), 100)
testParams.clear(maxIter)
self.assertFalse(testParams.isSet(maxIter))
self.assertEqual(testParams.getMaxIter(), 10)
testParams.setMaxIter(100)

self.assertTrue(testParams.hasParam(inputCol.name))
self.assertFalse(testParams.hasDefault(inputCol))
Expand All @@ -248,6 +252,18 @@ def test_params(self):
"maxIter: max number of iterations (>= 0). (default: 10, current: 100)",
"seed: random seed. (default: 41, current: 43)"]))

def test_clear_param(self):
df = self.spark.createDataFrame([(Vectors.dense([1.0]),), (Vectors.dense([2.0]),)], ["a"])
maScaler = MaxAbsScaler(inputCol="a", outputCol="scaled")
model = maScaler.fit(df)
self.assertTrue(model.isSet(model.outputCol))
self.assertEqual(model.getOutputCol(), "scaled")
model.clear(model.outputCol)
self.assertFalse(model.isSet(model.outputCol))
self.assertEqual(model.getOutputCol()[:12], 'MaxAbsScaler')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead, could you check that outputCol is equal to the default value?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nvm, I guess there isn't a way to get the default value directly. This is fine then.

output = model.transform(df)
self.assertEqual(model.getOutputCol(), output.schema.names[1])

def test_kmeans_param(self):
algo = KMeans()
self.assertEqual(algo.getInitMode(), "k-means||")
Expand Down
8 changes: 8 additions & 0 deletions python/pyspark/ml/wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,14 @@ def copy(self, extra=None):
that._transfer_params_to_java()
return that

def clear(self, param):
"""
Clears a param from the param map if it has been explicitly set.
"""
super(JavaParams, self).clear(param)
java_param = self._java_obj.getParam(param.name)
self._java_obj.clear(java_param)


@inherit_doc
class JavaEstimator(JavaParams, Estimator):
Expand Down
2 changes: 1 addition & 1 deletion python/pyspark/testing/mlutils.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def check_params(test_self, py_stage, check_params_exist=True):
continue # Random seeds between Spark and PySpark are different
java_default = _java2py(test_self.sc,
java_stage.clear(java_param).getOrDefault(java_param))
py_stage._clear(p)
py_stage.clear(p)
py_default = py_stage.getOrDefault(p)
# equality test for NaN is always False
if isinstance(java_default, float) and np.isnan(java_default):
Expand Down