Skip to content
Merged
100 changes: 67 additions & 33 deletions python/cuml/cuml/_thirdparty/sklearn/preprocessing/_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
from cupyx.scipy import sparse
from scipy import optimize, stats
from scipy.special import boxcox
from sklearn.base import OneToOneFeatureMixin, ClassNamePrefixFeaturesOutMixin

from cuml.common.sparse import csr_row_normalize_l1, csr_row_normalize_l2
from cuml.internals.interop import InteropMixin
Expand Down Expand Up @@ -214,9 +215,12 @@ def scale(X, *, axis=0, with_mean=True, with_std=True, copy=True):
return X


class MinMaxScaler(TransformerMixin,
BaseEstimator,
AllowNaNTagMixin):
class MinMaxScaler(
TransformerMixin,
AllowNaNTagMixin,
OneToOneFeatureMixin,
BaseEstimator,
):
"""Transform features by scaling each feature to a given range.

This estimator scales and translates each feature individually such
Expand Down Expand Up @@ -524,11 +528,14 @@ def minmax_scale(X, feature_range=(0, 1), *, axis=0, copy=True):
return X


class StandardScaler(TransformerMixin,
AllowNaNTagMixin,
SparseInputTagMixin,
BaseEstimator,
InteropMixin):
class StandardScaler(
TransformerMixin,
AllowNaNTagMixin,
SparseInputTagMixin,
InteropMixin,
OneToOneFeatureMixin,
BaseEstimator,
):
"""Standardize features by removing the mean and scaling to unit variance

The standard score of a sample `x` is calculated as:
Expand Down Expand Up @@ -946,10 +953,13 @@ def inverse_transform(self, X, copy=None):
return X


class MaxAbsScaler(TransformerMixin,
BaseEstimator,
AllowNaNTagMixin,
SparseInputTagMixin):
class MaxAbsScaler(
TransformerMixin,
AllowNaNTagMixin,
SparseInputTagMixin,
OneToOneFeatureMixin,
BaseEstimator,
):
"""Scale each feature by its maximum absolute value.

This estimator scales and translates each feature individually such
Expand Down Expand Up @@ -1196,10 +1206,13 @@ def maxabs_scale(X, *, axis=0, copy=True):
return X


class RobustScaler(TransformerMixin,
BaseEstimator,
AllowNaNTagMixin,
SparseInputTagMixin):
class RobustScaler(
TransformerMixin,
AllowNaNTagMixin,
SparseInputTagMixin,
OneToOneFeatureMixin,
BaseEstimator,
):
"""Scale features using statistics that are robust to outliers.

This Scaler removes the median and scales the data according to the
Expand Down Expand Up @@ -1867,9 +1880,12 @@ def normalize(X, norm='l2', *, axis=1, copy=True, return_norm=False):
return X


class Normalizer(TransformerMixin,
SparseInputTagMixin,
BaseEstimator):
class Normalizer(
TransformerMixin,
SparseInputTagMixin,
OneToOneFeatureMixin,
BaseEstimator,
):
"""Normalize samples individually to unit norm.

Each sample (i.e. each row of the data matrix) with at least one
Expand Down Expand Up @@ -2002,9 +2018,12 @@ def binarize(X, *, threshold=0.0, copy=True):
return X


class Binarizer(TransformerMixin,
SparseInputTagMixin,
BaseEstimator):
class Binarizer(
TransformerMixin,
SparseInputTagMixin,
OneToOneFeatureMixin,
BaseEstimator,
):
"""Binarize data (set feature values to 0 or 1) according to a threshold

Values greater than the threshold map to 1, while values less than
Expand Down Expand Up @@ -2165,7 +2184,11 @@ def add_dummy_feature(X, value=1.0):
return X


class KernelCenterer(TransformerMixin, BaseEstimator):
class KernelCenterer(
TransformerMixin,
ClassNamePrefixFeaturesOutMixin,
BaseEstimator,
):
"""Center a kernel matrix

Let K(x, z) be a kernel defined by phi(x)^T phi(z), where phi is a
Expand Down Expand Up @@ -2208,6 +2231,15 @@ def __init__(self):
# Needed for backported inspect.signature compatibility with PyPy
pass

@property
def _n_features_out(self):
return self.n_features_in_

def __sklearn_tags__(self):
tags = super().__sklearn_tags__()
tags.input_tags.pairwise = True
return tags

@mlfunc(set_input_type=True)
def fit(self, K, y=None) -> 'KernelCenterer':
"""Fit KernelCenterer
Expand Down Expand Up @@ -2263,14 +2295,13 @@ def transform(self, K, copy=True):

return K

@property
def _pairwise(self):
return True


class QuantileTransformer(TransformerMixin,
BaseEstimator,
AllowNaNTagMixin):
class QuantileTransformer(
TransformerMixin,
AllowNaNTagMixin,
OneToOneFeatureMixin,
BaseEstimator,
):
"""Transform features using quantiles information.

This method transforms the features to follow a uniform or a normal
Expand Down Expand Up @@ -2804,9 +2835,12 @@ def quantile_transform(X, *, axis=0, n_quantiles=1000,
" axis={}".format(axis))


class PowerTransformer(TransformerMixin,
BaseEstimator,
AllowNaNTagMixin):
class PowerTransformer(
TransformerMixin,
AllowNaNTagMixin,
OneToOneFeatureMixin,
BaseEstimator,
):
"""Apply a power transform featurewise to make data more Gaussian-like.

Power transforms are a family of parametric, monotonic transformations
Expand Down
Loading
Loading