Skip to content

Commit

Permalink
[SPARK-29464][PYTHON][ML] PySpark ML should expose Params.clear() to …
Browse files Browse the repository at this point in the history
…unset a user supplied Param

### What changes were proposed in this pull request?
change PySpark ml ```Params._clear``` to ```Params.clear```

### Why are the changes needed?
PySpark ML currently has a private _clear() method that will unset a param. This should be made public to match the Scala API and give users a way to unset a user supplied param.

### Does this PR introduce any user-facing change?
Yes. PySpark ml ```Params._clear``` ---> ```Params.clear```

### How was this patch tested?
Add test.

Closes #26130 from huaxingao/spark-29464.

Authored-by: Huaxin Gao <huaxing@us.ibm.com>
Signed-off-by: Bryan Cutler <cutlerb@gmail.com>
  • Loading branch information
huaxingao authored and BryanCutler committed Oct 18, 2019
1 parent 00347a3 commit 901ff92
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
4 changes: 2 additions & 2 deletions python/pyspark/ml/classification.py
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
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
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')
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
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
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

0 comments on commit 901ff92

Please sign in to comment.