From d63e222969b73d6374a57fb0c628d3671f270cfd Mon Sep 17 00:00:00 2001 From: Dante Gama Dessavre Date: Mon, 7 Oct 2024 17:40:59 -0500 Subject: [PATCH] ENH Make get_param_names a class method to match Scikit-learn --- python/cuml/cuml/_thirdparty/sklearn/preprocessing/_data.py | 3 ++- .../cuml/_thirdparty/sklearn/preprocessing/_discretization.py | 3 ++- .../cuml/cuml/_thirdparty/sklearn/preprocessing/_imputation.py | 3 ++- python/cuml/cuml/dask/cluster/dbscan.py | 3 ++- python/cuml/cuml/dask/cluster/kmeans.py | 3 ++- python/cuml/cuml/dask/decomposition/pca.py | 3 ++- python/cuml/cuml/dask/decomposition/tsvd.py | 3 ++- python/cuml/cuml/dask/linear_model/linear_regression.py | 3 ++- python/cuml/cuml/dask/linear_model/ridge.py | 3 ++- python/cuml/cuml/decomposition/incremental_pca.py | 3 ++- python/cuml/cuml/feature_extraction/_tfidf.py | 3 ++- python/cuml/cuml/linear_model/lasso.py | 3 ++- python/cuml/cuml/multiclass/multiclass.py | 3 ++- python/cuml/cuml/naive_bayes/naive_bayes.py | 3 ++- python/cuml/cuml/neighbors/kernel_density.py | 3 ++- python/cuml/cuml/preprocessing/LabelEncoder.py | 3 ++- python/cuml/cuml/preprocessing/TargetEncoder.py | 3 ++- python/cuml/cuml/preprocessing/encoders.py | 3 ++- python/cuml/cuml/preprocessing/label.py | 3 ++- python/cuml/cuml/svm/linear_svc.py | 3 ++- python/cuml/cuml/svm/linear_svr.py | 3 ++- 21 files changed, 42 insertions(+), 21 deletions(-) diff --git a/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_data.py b/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_data.py index afe8f8742e..b510ff97ac 100644 --- a/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_data.py +++ b/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_data.py @@ -326,7 +326,8 @@ def _reset(self): del self.data_max_ del self.data_range_ - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + [ "feature_range", "copy" diff --git a/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py b/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py index 02762f6585..5bafc40677 100644 --- a/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py +++ b/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_discretization.py @@ -158,7 +158,8 @@ def __init__(self, n_bins=5, *, encode='onehot', strategy='quantile'): self.encode = encode self.strategy = strategy - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + [ "n_bins", "encode", diff --git a/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_imputation.py b/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_imputation.py index 3fe160beac..8074ee4b54 100644 --- a/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_imputation.py +++ b/python/cuml/cuml/_thirdparty/sklearn/preprocessing/_imputation.py @@ -243,7 +243,8 @@ def __init__(self, *, missing_values=np.nan, strategy="mean", self.fill_value = fill_value self.copy = copy - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + [ "strategy", "fill_value", diff --git a/python/cuml/cuml/dask/cluster/dbscan.py b/python/cuml/cuml/dask/cluster/dbscan.py index 51c22abca6..367de9971f 100644 --- a/python/cuml/cuml/dask/cluster/dbscan.py +++ b/python/cuml/cuml/dask/cluster/dbscan.py @@ -160,5 +160,6 @@ def fit_predict(self, X, out_dtype="int32"): self.fit(X, out_dtype) return self.get_combined_model().labels_ - def get_param_names(self): + @classmethod + def get_param_names(cls): return list(self.kwargs.keys()) diff --git a/python/cuml/cuml/dask/cluster/kmeans.py b/python/cuml/cuml/dask/cluster/kmeans.py index b014dc4fb2..282aa8095f 100644 --- a/python/cuml/cuml/dask/cluster/kmeans.py +++ b/python/cuml/cuml/dask/cluster/kmeans.py @@ -302,5 +302,6 @@ def score(self, X, sample_weight=None): cp.asarray(self.client.compute(scores, sync=True)) * -1.0 ) - def get_param_names(self): + @classmethod + def get_param_names(cls): return list(self.kwargs.keys()) diff --git a/python/cuml/cuml/dask/decomposition/pca.py b/python/cuml/cuml/dask/decomposition/pca.py index 896eb58606..02285a9822 100644 --- a/python/cuml/cuml/dask/decomposition/pca.py +++ b/python/cuml/cuml/dask/decomposition/pca.py @@ -215,7 +215,8 @@ def inverse_transform(self, X, delayed=True): """ return self._inverse_transform(X, n_dims=2, delayed=delayed) - def get_param_names(self): + @classmethod + def get_param_names(cls): return list(self.kwargs.keys()) @staticmethod diff --git a/python/cuml/cuml/dask/decomposition/tsvd.py b/python/cuml/cuml/dask/decomposition/tsvd.py index d76d08bda2..3ebe80039d 100644 --- a/python/cuml/cuml/dask/decomposition/tsvd.py +++ b/python/cuml/cuml/dask/decomposition/tsvd.py @@ -177,7 +177,8 @@ def inverse_transform(self, X, delayed=True): """ return self._inverse_transform(X, n_dims=2, delayed=delayed) - def get_param_names(self): + @classmethod + def get_param_names(cls): return list(self.kwargs.keys()) @staticmethod diff --git a/python/cuml/cuml/dask/linear_model/linear_regression.py b/python/cuml/cuml/dask/linear_model/linear_regression.py index f8c5dfd7b0..26d702b3bb 100644 --- a/python/cuml/cuml/dask/linear_model/linear_regression.py +++ b/python/cuml/cuml/dask/linear_model/linear_regression.py @@ -106,7 +106,8 @@ def predict(self, X, delayed=True): """ return self._predict(X, delayed=delayed) - def get_param_names(self): + @classmethod + def get_param_names(cls): return list(self.kwargs.keys()) @staticmethod diff --git a/python/cuml/cuml/dask/linear_model/ridge.py b/python/cuml/cuml/dask/linear_model/ridge.py index 32db990979..93be31477d 100644 --- a/python/cuml/cuml/dask/linear_model/ridge.py +++ b/python/cuml/cuml/dask/linear_model/ridge.py @@ -114,7 +114,8 @@ def predict(self, X, delayed=True): """ return self._predict(X, delayed=delayed) - def get_param_names(self): + @classmethod + def get_param_names(cls): return list(self.kwargs.keys()) @staticmethod diff --git a/python/cuml/cuml/decomposition/incremental_pca.py b/python/cuml/cuml/decomposition/incremental_pca.py index 0288f0e9a8..acd5eb3702 100644 --- a/python/cuml/cuml/decomposition/incremental_pca.py +++ b/python/cuml/cuml/decomposition/incremental_pca.py @@ -449,7 +449,8 @@ def transform(self, X, convert_dtype=False) -> CumlArray: else: return super().transform(X) - def get_param_names(self): + @classmethod + def get_param_names(cls): # Skip super() since we dont pass any extra parameters in __init__ return Base.get_param_names(self) + self._hyperparams diff --git a/python/cuml/cuml/feature_extraction/_tfidf.py b/python/cuml/cuml/feature_extraction/_tfidf.py index 18b358a461..b1c4a65ed7 100644 --- a/python/cuml/cuml/feature_extraction/_tfidf.py +++ b/python/cuml/cuml/feature_extraction/_tfidf.py @@ -301,7 +301,8 @@ def idf_(self, value): (value, 0), shape=(n_features, n_features), dtype=cp.float32 ) - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + [ "norm", "use_idf", diff --git a/python/cuml/cuml/linear_model/lasso.py b/python/cuml/cuml/linear_model/lasso.py index 21a7add877..c81272a875 100644 --- a/python/cuml/cuml/linear_model/lasso.py +++ b/python/cuml/cuml/linear_model/lasso.py @@ -165,5 +165,6 @@ def __init__( verbose=verbose, ) - def get_param_names(self): + @classmethod + def get_param_names(cls): return list(set(super().get_param_names()) - {"l1_ratio"}) diff --git a/python/cuml/cuml/multiclass/multiclass.py b/python/cuml/cuml/multiclass/multiclass.py index 58c4151094..45671a275b 100644 --- a/python/cuml/cuml/multiclass/multiclass.py +++ b/python/cuml/cuml/multiclass/multiclass.py @@ -192,7 +192,8 @@ def decision_function(self, X) -> CumlArray: with cuml.internals.exit_internal_api(): return self.multiclass_estimator.decision_function(X) - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + ["estimator", "strategy"] diff --git a/python/cuml/cuml/naive_bayes/naive_bayes.py b/python/cuml/cuml/naive_bayes/naive_bayes.py index 89a6d55e5e..b393090ea6 100644 --- a/python/cuml/cuml/naive_bayes/naive_bayes.py +++ b/python/cuml/cuml/naive_bayes/naive_bayes.py @@ -724,7 +724,8 @@ def _joint_log_likelihood(self, X): return cp.array(joint_log_likelihood).T - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + ["priors", "var_smoothing"] diff --git a/python/cuml/cuml/neighbors/kernel_density.py b/python/cuml/cuml/neighbors/kernel_density.py index 9c85df321a..ceb88e91b2 100644 --- a/python/cuml/cuml/neighbors/kernel_density.py +++ b/python/cuml/cuml/neighbors/kernel_density.py @@ -225,7 +225,8 @@ def __init__( if kernel not in VALID_KERNELS: raise ValueError("invalid kernel: '{0}'".format(kernel)) - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + [ "bandwidth", "kernel", diff --git a/python/cuml/cuml/preprocessing/LabelEncoder.py b/python/cuml/cuml/preprocessing/LabelEncoder.py index ca20950ee1..ba6c1975ab 100644 --- a/python/cuml/cuml/preprocessing/LabelEncoder.py +++ b/python/cuml/cuml/preprocessing/LabelEncoder.py @@ -284,7 +284,8 @@ def inverse_transform(self, y: cudf.Series) -> cudf.Series: return res - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + [ "handle_unknown", ] diff --git a/python/cuml/cuml/preprocessing/TargetEncoder.py b/python/cuml/cuml/preprocessing/TargetEncoder.py index 43162b8805..d0df2fbf66 100644 --- a/python/cuml/cuml/preprocessing/TargetEncoder.py +++ b/python/cuml/cuml/preprocessing/TargetEncoder.py @@ -508,7 +508,8 @@ def _get_output_type(self, x): return "numpy" return "cupy" - def get_param_names(self): + @classmethod + def get_param_names(cls): return [ "n_folds", "smooth", diff --git a/python/cuml/cuml/preprocessing/encoders.py b/python/cuml/cuml/preprocessing/encoders.py index 01264572e7..7874aaf4dc 100644 --- a/python/cuml/cuml/preprocessing/encoders.py +++ b/python/cuml/cuml/preprocessing/encoders.py @@ -602,7 +602,8 @@ def get_feature_names(self, input_features=None): return np.array(feature_names, dtype=object) - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + [ "categories", "drop", diff --git a/python/cuml/cuml/preprocessing/label.py b/python/cuml/cuml/preprocessing/label.py index c34a9d9249..742383188d 100644 --- a/python/cuml/cuml/preprocessing/label.py +++ b/python/cuml/cuml/preprocessing/label.py @@ -287,7 +287,8 @@ def inverse_transform(self, y, threshold=None) -> CumlArray: return invert_labels(y_mapped, self.classes_) - def get_param_names(self): + @classmethod + def get_param_names(cls): return super().get_param_names() + [ "neg_label", "pos_label", diff --git a/python/cuml/cuml/svm/linear_svc.py b/python/cuml/cuml/svm/linear_svc.py index ed606464ba..5b66732a4f 100644 --- a/python/cuml/cuml/svm/linear_svc.py +++ b/python/cuml/cuml/svm/linear_svc.py @@ -173,7 +173,8 @@ def loss(self, loss: str): ) self.__loss = loss - def get_param_names(self): + @classmethod + def get_param_names(cls): return list( { "handle", diff --git a/python/cuml/cuml/svm/linear_svr.py b/python/cuml/cuml/svm/linear_svr.py index 76bba4b1e1..87c48c060b 100644 --- a/python/cuml/cuml/svm/linear_svr.py +++ b/python/cuml/cuml/svm/linear_svr.py @@ -152,7 +152,8 @@ def loss(self, loss: str): ) self.__loss = loss - def get_param_names(self): + @classmethod + def get_param_names(cls): return list( { "handle",