class sklearn.preprocessing.Normalizer(norm='l2', *, copy=True) [source]

Normalize samples individually to unit norm. Each sample (i.e. each row of the data matrix) with at least one non zero component is rescaled independently of other samples so that its norm (l1, l2 or inf) equals one. This transformer is able to work both with dense numpy arrays and scipy.sparse matrix (use CSR format if you want to avoid the burden of a copy / conversion). Scaling inputs to unit norms is a common operation for text classification or clustering for instance. For instance the dot product of two l2-normalized TF-IDF vectors is the cosine similarity of the vectors and is the base similarity metric for the Vector Space Model commonly used by the Information Retrieval community. Read more in the User Guide.  Parameters

norm{‘l1’, ‘l2’, ‘max’}, default=’l2’

The norm to use to normalize each non zero sample. If norm=’max’ is used, values will be rescaled by the maximum of the absolute values.
copybool, default=True

set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR matrix).      See also
normalize

Equivalent function without the estimator API.    Notes This estimator is stateless (besides constructor parameters), the fit method does nothing but is useful when used in a pipeline. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. Examples >>> from sklearn.preprocessing import Normalizer
>>> X = [[4, 1, 2, 2],
...      [1, 3, 9, 3],
...      [5, 7, 5, 1]]
>>> transformer = Normalizer().fit(X)  # fit does nothing.
>>> transformer
Normalizer()
>>> transformer.transform(X)
array([[0.8, 0.2, 0.4, 0.4],
       [0.1, 0.3, 0.9, 0.3],
       [0.5, 0.7, 0.5, 0.1]])
 Methods
fit(X[, y]) Do nothing and return the estimator unchanged
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
transform(X[, copy]) Scale each non zero row of X to unit norm
fit(X, y=None) [source]

Do nothing and return the estimator unchanged This method is just there to implement the usual API and hence work in pipelines.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to estimate the normalization parameters.
yNone

Ignored.    Returns

selfobject

Fitted transformer.

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X, copy=None) [source]

Scale each non zero row of X to unit norm  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to normalize, row by row. scipy.sparse matrices should be in CSR format to avoid an un-necessary copy.
copybool, default=None

Copy the input X or not.    Returns

X_tr{ndarray, sparse matrix} of shape (n_samples, n_features)

Transformed array.


sklearn.preprocessing.Normalizer
class sklearn.preprocessing.Normalizer(norm='l2', *, copy=True) [source]

Normalize samples individually to unit norm. Each sample (i.e. each row of the data matrix) with at least one non zero component is rescaled independently of other samples so that its norm (l1, l2 or inf) equals one. This transformer is able to work both with dense numpy arrays and scipy.sparse matrix (use CSR format if you want to avoid the burden of a copy / conversion). Scaling inputs to unit norms is a common operation for text classification or clustering for instance. For instance the dot product of two l2-normalized TF-IDF vectors is the cosine similarity of the vectors and is the base similarity metric for the Vector Space Model commonly used by the Information Retrieval community. Read more in the User Guide.  Parameters

norm{‘l1’, ‘l2’, ‘max’}, default=’l2’

The norm to use to normalize each non zero sample. If norm=’max’ is used, values will be rescaled by the maximum of the absolute values.
copybool, default=True

set to False to perform inplace row normalization and avoid a copy (if the input is already a numpy array or a scipy.sparse CSR matrix).      See also
normalize

Equivalent function without the estimator API.    Notes This estimator is stateless (besides constructor parameters), the fit method does nothing but is useful when used in a pipeline. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. Examples >>> from sklearn.preprocessing import Normalizer
>>> X = [[4, 1, 2, 2],
...      [1, 3, 9, 3],
...      [5, 7, 5, 1]]
>>> transformer = Normalizer().fit(X)  # fit does nothing.
>>> transformer
Normalizer()
>>> transformer.transform(X)
array([[0.8, 0.2, 0.4, 0.4],
       [0.1, 0.3, 0.9, 0.3],
       [0.5, 0.7, 0.5, 0.1]])
 Methods
fit(X[, y]) Do nothing and return the estimator unchanged
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
transform(X[, copy]) Scale each non zero row of X to unit norm
fit(X, y=None) [source]

Do nothing and return the estimator unchanged This method is just there to implement the usual API and hence work in pipelines.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to estimate the normalization parameters.
yNone

Ignored.    Returns

selfobject

Fitted transformer.

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X, copy=None) [source]

Scale each non zero row of X to unit norm  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to normalize, row by row. scipy.sparse matrices should be in CSR format to avoid an un-necessary copy.
copybool, default=None

Copy the input X or not.    Returns

X_tr{ndarray, sparse matrix} of shape (n_samples, n_features)

Transformed array.

 Examples using sklearn.preprocessing.Normalizer

  Scalable learning with polynomial kernel aproximation

  Compare the effect of different scalers on data with outliers

  Clustering text documents using k-means


fit(X, y=None) [source]

Do nothing and return the estimator unchanged This method is just there to implement the usual API and hence work in pipelines.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to estimate the normalization parameters.
yNone

Ignored.    Returns

selfobject

Fitted transformer.


fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.


set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


transform(X, copy=None) [source]

Scale each non zero row of X to unit norm  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to normalize, row by row. scipy.sparse matrices should be in CSR format to avoid an un-necessary copy.
copybool, default=None

Copy the input X or not.    Returns

X_tr{ndarray, sparse matrix} of shape (n_samples, n_features)

Transformed array.


class sklearn.preprocessing.OneHotEncoder(*, categories='auto', drop=None, sparse=True, dtype=<class 'numpy.float64'>, handle_unknown='error') [source]

Encode categorical features as a one-hot numeric array. The input to this transformer should be an array-like of integers or strings, denoting the values taken on by categorical (discrete) features. The features are encoded using a one-hot (aka ‘one-of-K’ or ‘dummy’) encoding scheme. This creates a binary column for each category and returns a sparse matrix or dense array (depending on the sparse parameter) By default, the encoder derives the categories based on the unique values in each feature. Alternatively, you can also specify the categories manually. This encoding is needed for feeding categorical data to many scikit-learn estimators, notably linear models and SVMs with the standard kernels. Note: a one-hot encoding of y labels should use a LabelBinarizer instead. Read more in the User Guide.  Changed in version 0.20.   Parameters

categories‘auto’ or a list of array-like, default=’auto’

Categories (unique values) per feature:  ‘auto’ : Determine categories automatically from the training data. list : categories[i] holds the categories expected in the ith column. The passed categories should not mix strings and numeric values within a single feature, and should be sorted in case of numeric values.  The used categories can be found in the categories_ attribute.  New in version 0.20.
drop{‘first’, ‘if_binary’} or a array-like of shape (n_features,), default=None

Specifies a methodology to use to drop one of the categories per feature. This is useful in situations where perfectly collinear features cause problems, such as when feeding the resulting data into a neural network or an unregularized regression. However, dropping one category breaks the symmetry of the original representation and can therefore induce a bias in downstream models, for instance for penalized linear classification or regression models.  None : retain all features (the default). ‘first’ : drop the first category in each feature. If only one category is present, the feature will be dropped entirely. ‘if_binary’ : drop the first category in each feature with two categories. Features with 1 or more than 2 categories are left intact. array : drop[i] is the category in feature X[:, i] that should be dropped.   Changed in version 0.23: Added option ‘if_binary’.
sparsebool, default=True

Will return sparse matrix if set True else will return an array.
dtypenumber type, default=float

Desired dtype of output.
handle_unknown{‘error’, ‘ignore’}, default=’error’

Whether to raise an error or ignore if an unknown categorical feature is present during transform (default is to raise). When this parameter is set to ‘ignore’ and an unknown category is encountered during transform, the resulting one-hot encoded columns for this feature will be all zeros. In the inverse transform, an unknown category will be denoted as None.    Attributes

categories_list of arrays

The categories of each feature determined during fitting (in order of the features in X and corresponding with the output of transform). This includes the category specified in drop (if any).
drop_idx_array of shape (n_features,)

drop_idx_[i] is the index in categories_[i] of the category to be dropped for each feature.
drop_idx_[i] = None if no category is to be dropped from the feature with index i, e.g. when drop='if_binary' and the feature isn’t binary.
drop_idx_ = None if all the transformed features will be retained.   Changed in version 0.23: Added the possibility to contain None values.       See also
OrdinalEncoder

Performs an ordinal (integer) encoding of the categorical features.
sklearn.feature_extraction.DictVectorizer

Performs a one-hot encoding of dictionary items (also handles string-valued features).
sklearn.feature_extraction.FeatureHasher

Performs an approximate one-hot encoding of dictionary items or strings.
LabelBinarizer

Binarizes labels in a one-vs-all fashion.
MultiLabelBinarizer

Transforms between iterable of iterables and a multilabel format, e.g. a (samples x classes) binary matrix indicating the presence of a class label.    Examples Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to a binary one-hot encoding. >>> from sklearn.preprocessing import OneHotEncoder
 One can discard categories not seen during fit: >>> enc = OneHotEncoder(handle_unknown='ignore')
>>> X = [['Male', 1], ['Female', 3], ['Female', 2]]
>>> enc.fit(X)
OneHotEncoder(handle_unknown='ignore')
>>> enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> enc.transform([['Female', 1], ['Male', 4]]).toarray()
array([[1., 0., 1., 0., 0.],
       [0., 1., 0., 0., 0.]])
>>> enc.inverse_transform([[0, 1, 1, 0, 0], [0, 0, 0, 1, 0]])
array([['Male', 1],
       [None, 2]], dtype=object)
>>> enc.get_feature_names(['gender', 'group'])
array(['gender_Female', 'gender_Male', 'group_1', 'group_2', 'group_3'],
  dtype=object)
 One can always drop the first column for each feature: >>> drop_enc = OneHotEncoder(drop='first').fit(X)
>>> drop_enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> drop_enc.transform([['Female', 1], ['Male', 2]]).toarray()
array([[0., 0., 0.],
       [1., 1., 0.]])
 Or drop a column for feature only having 2 categories: >>> drop_binary_enc = OneHotEncoder(drop='if_binary').fit(X)
>>> drop_binary_enc.transform([['Female', 1], ['Male', 2]]).toarray()
array([[0., 1., 0., 0.],
       [1., 0., 1., 0.]])
 Methods
fit(X[, y]) Fit OneHotEncoder to X.
fit_transform(X[, y]) Fit OneHotEncoder to X, then transform X.
get_feature_names([input_features]) Return feature names for output features.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Convert the data back to the original representation.
set_params(**params) Set the parameters of this estimator.
transform(X) Transform X using one-hot encoding.
fit(X, y=None) [source]

Fit OneHotEncoder to X.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to determine the categories of each feature.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns
 self

fit_transform(X, y=None) [source]

Fit OneHotEncoder to X, then transform X. Equivalent to fit(X).transform(X) but more convenient.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns

X_outsparse matrix if sparse=True else a 2-d array

Transformed input.

get_feature_names(input_features=None) [source]

Return feature names for output features.  Parameters

input_featureslist of str of shape (n_features,)

String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.    Returns

output_feature_namesndarray of shape (n_output_features,)

Array of feature names.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Convert the data back to the original representation. In case unknown categories are encountered (all zeros in the one-hot encoding), None is used to represent this category.  Parameters

Xarray-like or sparse matrix, shape [n_samples, n_encoded_features]

The transformed data.    Returns

X_trarray-like, shape [n_samples, n_features]

Inverse transformed array.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Transform X using one-hot encoding.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.    Returns

X_outsparse matrix if sparse=True else a 2-d array

Transformed input.


sklearn.preprocessing.OneHotEncoder
class sklearn.preprocessing.OneHotEncoder(*, categories='auto', drop=None, sparse=True, dtype=<class 'numpy.float64'>, handle_unknown='error') [source]

Encode categorical features as a one-hot numeric array. The input to this transformer should be an array-like of integers or strings, denoting the values taken on by categorical (discrete) features. The features are encoded using a one-hot (aka ‘one-of-K’ or ‘dummy’) encoding scheme. This creates a binary column for each category and returns a sparse matrix or dense array (depending on the sparse parameter) By default, the encoder derives the categories based on the unique values in each feature. Alternatively, you can also specify the categories manually. This encoding is needed for feeding categorical data to many scikit-learn estimators, notably linear models and SVMs with the standard kernels. Note: a one-hot encoding of y labels should use a LabelBinarizer instead. Read more in the User Guide.  Changed in version 0.20.   Parameters

categories‘auto’ or a list of array-like, default=’auto’

Categories (unique values) per feature:  ‘auto’ : Determine categories automatically from the training data. list : categories[i] holds the categories expected in the ith column. The passed categories should not mix strings and numeric values within a single feature, and should be sorted in case of numeric values.  The used categories can be found in the categories_ attribute.  New in version 0.20.
drop{‘first’, ‘if_binary’} or a array-like of shape (n_features,), default=None

Specifies a methodology to use to drop one of the categories per feature. This is useful in situations where perfectly collinear features cause problems, such as when feeding the resulting data into a neural network or an unregularized regression. However, dropping one category breaks the symmetry of the original representation and can therefore induce a bias in downstream models, for instance for penalized linear classification or regression models.  None : retain all features (the default). ‘first’ : drop the first category in each feature. If only one category is present, the feature will be dropped entirely. ‘if_binary’ : drop the first category in each feature with two categories. Features with 1 or more than 2 categories are left intact. array : drop[i] is the category in feature X[:, i] that should be dropped.   Changed in version 0.23: Added option ‘if_binary’.
sparsebool, default=True

Will return sparse matrix if set True else will return an array.
dtypenumber type, default=float

Desired dtype of output.
handle_unknown{‘error’, ‘ignore’}, default=’error’

Whether to raise an error or ignore if an unknown categorical feature is present during transform (default is to raise). When this parameter is set to ‘ignore’ and an unknown category is encountered during transform, the resulting one-hot encoded columns for this feature will be all zeros. In the inverse transform, an unknown category will be denoted as None.    Attributes

categories_list of arrays

The categories of each feature determined during fitting (in order of the features in X and corresponding with the output of transform). This includes the category specified in drop (if any).
drop_idx_array of shape (n_features,)

drop_idx_[i] is the index in categories_[i] of the category to be dropped for each feature.
drop_idx_[i] = None if no category is to be dropped from the feature with index i, e.g. when drop='if_binary' and the feature isn’t binary.
drop_idx_ = None if all the transformed features will be retained.   Changed in version 0.23: Added the possibility to contain None values.       See also
OrdinalEncoder

Performs an ordinal (integer) encoding of the categorical features.
sklearn.feature_extraction.DictVectorizer

Performs a one-hot encoding of dictionary items (also handles string-valued features).
sklearn.feature_extraction.FeatureHasher

Performs an approximate one-hot encoding of dictionary items or strings.
LabelBinarizer

Binarizes labels in a one-vs-all fashion.
MultiLabelBinarizer

Transforms between iterable of iterables and a multilabel format, e.g. a (samples x classes) binary matrix indicating the presence of a class label.    Examples Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to a binary one-hot encoding. >>> from sklearn.preprocessing import OneHotEncoder
 One can discard categories not seen during fit: >>> enc = OneHotEncoder(handle_unknown='ignore')
>>> X = [['Male', 1], ['Female', 3], ['Female', 2]]
>>> enc.fit(X)
OneHotEncoder(handle_unknown='ignore')
>>> enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> enc.transform([['Female', 1], ['Male', 4]]).toarray()
array([[1., 0., 1., 0., 0.],
       [0., 1., 0., 0., 0.]])
>>> enc.inverse_transform([[0, 1, 1, 0, 0], [0, 0, 0, 1, 0]])
array([['Male', 1],
       [None, 2]], dtype=object)
>>> enc.get_feature_names(['gender', 'group'])
array(['gender_Female', 'gender_Male', 'group_1', 'group_2', 'group_3'],
  dtype=object)
 One can always drop the first column for each feature: >>> drop_enc = OneHotEncoder(drop='first').fit(X)
>>> drop_enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> drop_enc.transform([['Female', 1], ['Male', 2]]).toarray()
array([[0., 0., 0.],
       [1., 1., 0.]])
 Or drop a column for feature only having 2 categories: >>> drop_binary_enc = OneHotEncoder(drop='if_binary').fit(X)
>>> drop_binary_enc.transform([['Female', 1], ['Male', 2]]).toarray()
array([[0., 1., 0., 0.],
       [1., 0., 1., 0.]])
 Methods
fit(X[, y]) Fit OneHotEncoder to X.
fit_transform(X[, y]) Fit OneHotEncoder to X, then transform X.
get_feature_names([input_features]) Return feature names for output features.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Convert the data back to the original representation.
set_params(**params) Set the parameters of this estimator.
transform(X) Transform X using one-hot encoding.
fit(X, y=None) [source]

Fit OneHotEncoder to X.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to determine the categories of each feature.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns
 self

fit_transform(X, y=None) [source]

Fit OneHotEncoder to X, then transform X. Equivalent to fit(X).transform(X) but more convenient.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns

X_outsparse matrix if sparse=True else a 2-d array

Transformed input.

get_feature_names(input_features=None) [source]

Return feature names for output features.  Parameters

input_featureslist of str of shape (n_features,)

String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.    Returns

output_feature_namesndarray of shape (n_output_features,)

Array of feature names.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Convert the data back to the original representation. In case unknown categories are encountered (all zeros in the one-hot encoding), None is used to represent this category.  Parameters

Xarray-like or sparse matrix, shape [n_samples, n_encoded_features]

The transformed data.    Returns

X_trarray-like, shape [n_samples, n_features]

Inverse transformed array.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Transform X using one-hot encoding.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.    Returns

X_outsparse matrix if sparse=True else a 2-d array

Transformed input.

 Examples using sklearn.preprocessing.OneHotEncoder

  Release Highlights for scikit-learn 0.23

  Feature transformations with ensembles of trees

  Categorical Feature Support in Gradient Boosting

  Combine predictors using stacking

  Poisson regression and non-normal loss

  Tweedie regression on insurance claims

  Permutation Importance vs Random Forest Feature Importance (MDI)

  Common pitfalls in interpretation of coefficients of linear models

  Column Transformer with Mixed Types


fit(X, y=None) [source]

Fit OneHotEncoder to X.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to determine the categories of each feature.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns
 self


fit_transform(X, y=None) [source]

Fit OneHotEncoder to X, then transform X. Equivalent to fit(X).transform(X) but more convenient.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns

X_outsparse matrix if sparse=True else a 2-d array

Transformed input.


get_feature_names(input_features=None) [source]

Return feature names for output features.  Parameters

input_featureslist of str of shape (n_features,)

String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.    Returns

output_feature_namesndarray of shape (n_output_features,)

Array of feature names.


inverse_transform(X) [source]

Convert the data back to the original representation. In case unknown categories are encountered (all zeros in the one-hot encoding), None is used to represent this category.  Parameters

Xarray-like or sparse matrix, shape [n_samples, n_encoded_features]

The transformed data.    Returns

X_trarray-like, shape [n_samples, n_features]

Inverse transformed array.


transform(X) [source]

Transform X using one-hot encoding.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.    Returns

X_outsparse matrix if sparse=True else a 2-d array

Transformed input.


class sklearn.preprocessing.OrdinalEncoder(*, categories='auto', dtype=<class 'numpy.float64'>, handle_unknown='error', unknown_value=None) [source]

Encode categorical features as an integer array. The input to this transformer should be an array-like of integers or strings, denoting the values taken on by categorical (discrete) features. The features are converted to ordinal integers. This results in a single column of integers (0 to n_categories - 1) per feature. Read more in the User Guide.  New in version 0.20.   Parameters

categories‘auto’ or a list of array-like, default=’auto’

Categories (unique values) per feature:  ‘auto’ : Determine categories automatically from the training data. list : categories[i] holds the categories expected in the ith column. The passed categories should not mix strings and numeric values, and should be sorted in case of numeric values.  The used categories can be found in the categories_ attribute.
dtypenumber type, default np.float64

Desired dtype of output.
handle_unknown{‘error’, ‘use_encoded_value’}, default=’error’

When set to ‘error’ an error will be raised in case an unknown categorical feature is present during transform. When set to ‘use_encoded_value’, the encoded value of unknown categories will be set to the value given for the parameter unknown_value. In inverse_transform, an unknown category will be denoted as None.  New in version 0.24.
unknown_valueint or np.nan, default=None

When the parameter handle_unknown is set to ‘use_encoded_value’, this parameter is required and will set the encoded value of unknown categories. It has to be distinct from the values used to encode any of the categories in fit. If set to np.nan, the dtype parameter must be a float dtype.  New in version 0.24.     Attributes

categories_list of arrays

The categories of each feature determined during fit (in order of the features in X and corresponding with the output of transform). This does not include categories that weren’t seen during fit.      See also
OneHotEncoder

Performs a one-hot encoding of categorical features.
LabelEncoder

Encodes target labels with values between 0 and n_classes-1.    Examples Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to an ordinal encoding. >>> from sklearn.preprocessing import OrdinalEncoder
>>> enc = OrdinalEncoder()
>>> X = [['Male', 1], ['Female', 3], ['Female', 2]]
>>> enc.fit(X)
OrdinalEncoder()
>>> enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> enc.transform([['Female', 3], ['Male', 1]])
array([[0., 2.],
       [1., 0.]])
 >>> enc.inverse_transform([[1, 0], [0, 1]])
array([['Male', 1],
       ['Female', 2]], dtype=object)
 Methods
fit(X[, y]) Fit the OrdinalEncoder to X.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Convert the data back to the original representation.
set_params(**params) Set the parameters of this estimator.
transform(X) Transform X to ordinal codes.
fit(X, y=None) [source]

Fit the OrdinalEncoder to X.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to determine the categories of each feature.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns
 self

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Convert the data back to the original representation.  Parameters

Xarray-like or sparse matrix, shape [n_samples, n_encoded_features]

The transformed data.    Returns

X_trarray-like, shape [n_samples, n_features]

Inverse transformed array.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Transform X to ordinal codes.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.    Returns

X_outsparse matrix or a 2-d array

Transformed input.


sklearn.preprocessing.OrdinalEncoder
class sklearn.preprocessing.OrdinalEncoder(*, categories='auto', dtype=<class 'numpy.float64'>, handle_unknown='error', unknown_value=None) [source]

Encode categorical features as an integer array. The input to this transformer should be an array-like of integers or strings, denoting the values taken on by categorical (discrete) features. The features are converted to ordinal integers. This results in a single column of integers (0 to n_categories - 1) per feature. Read more in the User Guide.  New in version 0.20.   Parameters

categories‘auto’ or a list of array-like, default=’auto’

Categories (unique values) per feature:  ‘auto’ : Determine categories automatically from the training data. list : categories[i] holds the categories expected in the ith column. The passed categories should not mix strings and numeric values, and should be sorted in case of numeric values.  The used categories can be found in the categories_ attribute.
dtypenumber type, default np.float64

Desired dtype of output.
handle_unknown{‘error’, ‘use_encoded_value’}, default=’error’

When set to ‘error’ an error will be raised in case an unknown categorical feature is present during transform. When set to ‘use_encoded_value’, the encoded value of unknown categories will be set to the value given for the parameter unknown_value. In inverse_transform, an unknown category will be denoted as None.  New in version 0.24.
unknown_valueint or np.nan, default=None

When the parameter handle_unknown is set to ‘use_encoded_value’, this parameter is required and will set the encoded value of unknown categories. It has to be distinct from the values used to encode any of the categories in fit. If set to np.nan, the dtype parameter must be a float dtype.  New in version 0.24.     Attributes

categories_list of arrays

The categories of each feature determined during fit (in order of the features in X and corresponding with the output of transform). This does not include categories that weren’t seen during fit.      See also
OneHotEncoder

Performs a one-hot encoding of categorical features.
LabelEncoder

Encodes target labels with values between 0 and n_classes-1.    Examples Given a dataset with two features, we let the encoder find the unique values per feature and transform the data to an ordinal encoding. >>> from sklearn.preprocessing import OrdinalEncoder
>>> enc = OrdinalEncoder()
>>> X = [['Male', 1], ['Female', 3], ['Female', 2]]
>>> enc.fit(X)
OrdinalEncoder()
>>> enc.categories_
[array(['Female', 'Male'], dtype=object), array([1, 2, 3], dtype=object)]
>>> enc.transform([['Female', 3], ['Male', 1]])
array([[0., 2.],
       [1., 0.]])
 >>> enc.inverse_transform([[1, 0], [0, 1]])
array([['Male', 1],
       ['Female', 2]], dtype=object)
 Methods
fit(X[, y]) Fit the OrdinalEncoder to X.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Convert the data back to the original representation.
set_params(**params) Set the parameters of this estimator.
transform(X) Transform X to ordinal codes.
fit(X, y=None) [source]

Fit the OrdinalEncoder to X.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to determine the categories of each feature.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns
 self

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Convert the data back to the original representation.  Parameters

Xarray-like or sparse matrix, shape [n_samples, n_encoded_features]

The transformed data.    Returns

X_trarray-like, shape [n_samples, n_features]

Inverse transformed array.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Transform X to ordinal codes.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.    Returns

X_outsparse matrix or a 2-d array

Transformed input.

 Examples using sklearn.preprocessing.OrdinalEncoder

  Categorical Feature Support in Gradient Boosting

  Combine predictors using stacking

  Poisson regression and non-normal loss


fit(X, y=None) [source]

Fit the OrdinalEncoder to X.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to determine the categories of each feature.
yNone

Ignored. This parameter exists only for compatibility with Pipeline.    Returns
 self


inverse_transform(X) [source]

Convert the data back to the original representation.  Parameters

Xarray-like or sparse matrix, shape [n_samples, n_encoded_features]

The transformed data.    Returns

X_trarray-like, shape [n_samples, n_features]

Inverse transformed array.


transform(X) [source]

Transform X to ordinal codes.  Parameters

Xarray-like, shape [n_samples, n_features]

The data to encode.    Returns

X_outsparse matrix or a 2-d array

Transformed input.


class sklearn.preprocessing.PolynomialFeatures(degree=2, *, interaction_only=False, include_bias=True, order='C') [source]

Generate polynomial and interaction features. Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].  Parameters

degreeint, default=2

The degree of the polynomial features.
interaction_onlybool, default=False

If true, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] ** 2, x[0] * x[2] ** 3, etc.).
include_biasbool, default=True

If True (default), then include a bias column, the feature in which all polynomial powers are zero (i.e. a column of ones - acts as an intercept term in a linear model).
order{‘C’, ‘F’}, default=’C’

Order of output array in the dense case. ‘F’ order is faster to compute, but may slow down subsequent estimators.  New in version 0.21.     Attributes

powers_ndarray of shape (n_output_features, n_input_features)

powers_[i, j] is the exponent of the jth input in the ith output.
n_input_features_int

The total number of input features.
n_output_features_int

The total number of polynomial output features. The number of output features is computed by iterating over all suitably sized combinations of input features.     Notes Be aware that the number of features in the output array scales polynomially in the number of features of the input array, and exponentially in the degree. High degrees can cause overfitting. See examples/linear_model/plot_polynomial_interpolation.py Examples >>> import numpy as np
>>> from sklearn.preprocessing import PolynomialFeatures
>>> X = np.arange(6).reshape(3, 2)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> poly = PolynomialFeatures(2)
>>> poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.,  0.,  1.],
       [ 1.,  2.,  3.,  4.,  6.,  9.],
       [ 1.,  4.,  5., 16., 20., 25.]])
>>> poly = PolynomialFeatures(interaction_only=True)
>>> poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.],
       [ 1.,  2.,  3.,  6.],
       [ 1.,  4.,  5., 20.]])
 Methods
fit(X[, y]) Compute number of output features.
fit_transform(X[, y]) Fit to data, then transform it.
get_feature_names([input_features]) Return feature names for output features
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
transform(X) Transform data to polynomial features
fit(X, y=None) [source]

Compute number of output features.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data.
yNone

Ignored.    Returns

selfobject

Fitted transformer.

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_feature_names(input_features=None) [source]

Return feature names for output features  Parameters

input_featureslist of str of shape (n_features,), default=None

String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.    Returns

output_feature_nameslist of str of shape (n_output_features,)

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Transform data to polynomial features  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to transform, row by row. Prefer CSR over CSC for sparse input (for speed), but CSC is required if the degree is 4 or higher. If the degree is less than 4 and the input format is CSC, it will be converted to CSR, have its polynomial features generated, then converted back to CSC. If the degree is 2 or 3, the method described in “Leveraging Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices Using K-Simplex Numbers” by Andrew Nystrom and John Hughes is used, which is much faster than the method used on CSC input. For this reason, a CSC input will be converted to CSR, and the output will be converted back to CSC prior to being returned, hence the preference of CSR.    Returns

XP{ndarray, sparse matrix} of shape (n_samples, NP)

The matrix of features, where NP is the number of polynomial features generated from the combination of inputs. If a sparse matrix is provided, it will be converted into a sparse csr_matrix.


sklearn.preprocessing.PolynomialFeatures
class sklearn.preprocessing.PolynomialFeatures(degree=2, *, interaction_only=False, include_bias=True, order='C') [source]

Generate polynomial and interaction features. Generate a new feature matrix consisting of all polynomial combinations of the features with degree less than or equal to the specified degree. For example, if an input sample is two dimensional and of the form [a, b], the degree-2 polynomial features are [1, a, b, a^2, ab, b^2].  Parameters

degreeint, default=2

The degree of the polynomial features.
interaction_onlybool, default=False

If true, only interaction features are produced: features that are products of at most degree distinct input features (so not x[1] ** 2, x[0] * x[2] ** 3, etc.).
include_biasbool, default=True

If True (default), then include a bias column, the feature in which all polynomial powers are zero (i.e. a column of ones - acts as an intercept term in a linear model).
order{‘C’, ‘F’}, default=’C’

Order of output array in the dense case. ‘F’ order is faster to compute, but may slow down subsequent estimators.  New in version 0.21.     Attributes

powers_ndarray of shape (n_output_features, n_input_features)

powers_[i, j] is the exponent of the jth input in the ith output.
n_input_features_int

The total number of input features.
n_output_features_int

The total number of polynomial output features. The number of output features is computed by iterating over all suitably sized combinations of input features.     Notes Be aware that the number of features in the output array scales polynomially in the number of features of the input array, and exponentially in the degree. High degrees can cause overfitting. See examples/linear_model/plot_polynomial_interpolation.py Examples >>> import numpy as np
>>> from sklearn.preprocessing import PolynomialFeatures
>>> X = np.arange(6).reshape(3, 2)
>>> X
array([[0, 1],
       [2, 3],
       [4, 5]])
>>> poly = PolynomialFeatures(2)
>>> poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.,  0.,  1.],
       [ 1.,  2.,  3.,  4.,  6.,  9.],
       [ 1.,  4.,  5., 16., 20., 25.]])
>>> poly = PolynomialFeatures(interaction_only=True)
>>> poly.fit_transform(X)
array([[ 1.,  0.,  1.,  0.],
       [ 1.,  2.,  3.,  6.],
       [ 1.,  4.,  5., 20.]])
 Methods
fit(X[, y]) Compute number of output features.
fit_transform(X[, y]) Fit to data, then transform it.
get_feature_names([input_features]) Return feature names for output features
get_params([deep]) Get parameters for this estimator.
set_params(**params) Set the parameters of this estimator.
transform(X) Transform data to polynomial features
fit(X, y=None) [source]

Compute number of output features.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data.
yNone

Ignored.    Returns

selfobject

Fitted transformer.

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_feature_names(input_features=None) [source]

Return feature names for output features  Parameters

input_featureslist of str of shape (n_features,), default=None

String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.    Returns

output_feature_nameslist of str of shape (n_output_features,)

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Transform data to polynomial features  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to transform, row by row. Prefer CSR over CSC for sparse input (for speed), but CSC is required if the degree is 4 or higher. If the degree is less than 4 and the input format is CSC, it will be converted to CSR, have its polynomial features generated, then converted back to CSC. If the degree is 2 or 3, the method described in “Leveraging Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices Using K-Simplex Numbers” by Andrew Nystrom and John Hughes is used, which is much faster than the method used on CSC input. For this reason, a CSC input will be converted to CSR, and the output will be converted back to CSC prior to being returned, hence the preference of CSR.    Returns

XP{ndarray, sparse matrix} of shape (n_samples, NP)

The matrix of features, where NP is the number of polynomial features generated from the combination of inputs. If a sparse matrix is provided, it will be converted into a sparse csr_matrix.

 Examples using sklearn.preprocessing.PolynomialFeatures

  Release Highlights for scikit-learn 0.24

  Polynomial interpolation

  Robust linear estimator fitting

  Poisson regression and non-normal loss

  Underfitting vs. Overfitting


fit(X, y=None) [source]

Compute number of output features.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data.
yNone

Ignored.    Returns

selfobject

Fitted transformer.


fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


get_feature_names(input_features=None) [source]

Return feature names for output features  Parameters

input_featureslist of str of shape (n_features,), default=None

String names for input features if available. By default, “x0”, “x1”, … “xn_features” is used.    Returns

output_feature_nameslist of str of shape (n_output_features,)


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.


set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


transform(X) [source]

Transform data to polynomial features  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to transform, row by row. Prefer CSR over CSC for sparse input (for speed), but CSC is required if the degree is 4 or higher. If the degree is less than 4 and the input format is CSC, it will be converted to CSR, have its polynomial features generated, then converted back to CSC. If the degree is 2 or 3, the method described in “Leveraging Sparsity to Speed Up Polynomial Feature Expansions of CSR Matrices Using K-Simplex Numbers” by Andrew Nystrom and John Hughes is used, which is much faster than the method used on CSC input. For this reason, a CSC input will be converted to CSR, and the output will be converted back to CSC prior to being returned, hence the preference of CSR.    Returns

XP{ndarray, sparse matrix} of shape (n_samples, NP)

The matrix of features, where NP is the number of polynomial features generated from the combination of inputs. If a sparse matrix is provided, it will be converted into a sparse csr_matrix.


class sklearn.preprocessing.PowerTransformer(method='yeo-johnson', *, standardize=True, copy=True) [source]

Apply a power transform featurewise to make data more Gaussian-like. Power transforms are a family of parametric, monotonic transformations that are applied to make data more Gaussian-like. This is useful for modeling issues related to heteroscedasticity (non-constant variance), or other situations where normality is desired. Currently, PowerTransformer supports the Box-Cox transform and the Yeo-Johnson transform. The optimal parameter for stabilizing variance and minimizing skewness is estimated through maximum likelihood. Box-Cox requires input data to be strictly positive, while Yeo-Johnson supports both positive or negative data. By default, zero-mean, unit-variance normalization is applied to the transformed data. Read more in the User Guide.  New in version 0.20.   Parameters

method{‘yeo-johnson’, ‘box-cox’}, default=’yeo-johnson’

The power transform method. Available methods are:  ‘yeo-johnson’ [1], works with positive and negative values ‘box-cox’ [2], only works with strictly positive values
standardizebool, default=True

Set to True to apply zero-mean, unit-variance normalization to the transformed output.
copybool, default=True

Set to False to perform inplace computation during transformation.    Attributes

lambdas_ndarray of float of shape (n_features,)

The parameters of the power transformation for the selected features.      See also
power_transform

Equivalent function without the estimator API.
QuantileTransformer

Maps data to a standard normal distribution with the parameter output_distribution='normal'.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. References
1
I.K. Yeo and R.A. Johnson, “A new family of power transformations to improve normality or symmetry.” Biometrika, 87(4), pp.954-959, (2000).
2
G.E.P. Box and D.R. Cox, “An Analysis of Transformations”, Journal of the Royal Statistical Society B, 26, 211-252 (1964).   Examples >>> import numpy as np
>>> from sklearn.preprocessing import PowerTransformer
>>> pt = PowerTransformer()
>>> data = [[1, 2], [3, 2], [4, 5]]
>>> print(pt.fit(data))
PowerTransformer()
>>> print(pt.lambdas_)
[ 1.386... -3.100...]
>>> print(pt.transform(data))
[[-1.316... -0.707...]
 [ 0.209... -0.707...]
 [ 1.106...  1.414...]]
 Methods
fit(X[, y]) Estimate the optimal parameter lambda for each feature.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Apply the inverse power transformation using the fitted lambdas.
set_params(**params) Set the parameters of this estimator.
transform(X) Apply the power transform to each feature using the fitted lambdas.
fit(X, y=None) [source]

Estimate the optimal parameter lambda for each feature. The optimal lambda parameter for minimizing skewness is estimated on each feature independently using maximum likelihood.  Parameters

Xarray-like of shape (n_samples, n_features)

The data used to estimate the optimal transformation parameters.
yNone

Ignored.    Returns

selfobject

Fitted transformer.

fit_transform(X, y=None) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Apply the inverse power transformation using the fitted lambdas. The inverse of the Box-Cox transformation is given by: if lambda_ == 0:
    X = exp(X_trans)
else:
    X = (X_trans * lambda_ + 1) ** (1 / lambda_)
 The inverse of the Yeo-Johnson transformation is given by: if X >= 0 and lambda_ == 0:
    X = exp(X_trans) - 1
elif X >= 0 and lambda_ != 0:
    X = (X_trans * lambda_ + 1) ** (1 / lambda_) - 1
elif X < 0 and lambda_ != 2:
    X = 1 - (-(2 - lambda_) * X_trans + 1) ** (1 / (2 - lambda_))
elif X < 0 and lambda_ == 2:
    X = 1 - exp(-X_trans)
  Parameters

Xarray-like of shape (n_samples, n_features)

The transformed data.    Returns

Xndarray of shape (n_samples, n_features)

The original data.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Apply the power transform to each feature using the fitted lambdas.  Parameters

Xarray-like of shape (n_samples, n_features)

The data to be transformed using a power transformation.    Returns

X_transndarray of shape (n_samples, n_features)

The transformed data.


sklearn.preprocessing.PowerTransformer
class sklearn.preprocessing.PowerTransformer(method='yeo-johnson', *, standardize=True, copy=True) [source]

Apply a power transform featurewise to make data more Gaussian-like. Power transforms are a family of parametric, monotonic transformations that are applied to make data more Gaussian-like. This is useful for modeling issues related to heteroscedasticity (non-constant variance), or other situations where normality is desired. Currently, PowerTransformer supports the Box-Cox transform and the Yeo-Johnson transform. The optimal parameter for stabilizing variance and minimizing skewness is estimated through maximum likelihood. Box-Cox requires input data to be strictly positive, while Yeo-Johnson supports both positive or negative data. By default, zero-mean, unit-variance normalization is applied to the transformed data. Read more in the User Guide.  New in version 0.20.   Parameters

method{‘yeo-johnson’, ‘box-cox’}, default=’yeo-johnson’

The power transform method. Available methods are:  ‘yeo-johnson’ [1], works with positive and negative values ‘box-cox’ [2], only works with strictly positive values
standardizebool, default=True

Set to True to apply zero-mean, unit-variance normalization to the transformed output.
copybool, default=True

Set to False to perform inplace computation during transformation.    Attributes

lambdas_ndarray of float of shape (n_features,)

The parameters of the power transformation for the selected features.      See also
power_transform

Equivalent function without the estimator API.
QuantileTransformer

Maps data to a standard normal distribution with the parameter output_distribution='normal'.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. References
1
I.K. Yeo and R.A. Johnson, “A new family of power transformations to improve normality or symmetry.” Biometrika, 87(4), pp.954-959, (2000).
2
G.E.P. Box and D.R. Cox, “An Analysis of Transformations”, Journal of the Royal Statistical Society B, 26, 211-252 (1964).   Examples >>> import numpy as np
>>> from sklearn.preprocessing import PowerTransformer
>>> pt = PowerTransformer()
>>> data = [[1, 2], [3, 2], [4, 5]]
>>> print(pt.fit(data))
PowerTransformer()
>>> print(pt.lambdas_)
[ 1.386... -3.100...]
>>> print(pt.transform(data))
[[-1.316... -0.707...]
 [ 0.209... -0.707...]
 [ 1.106...  1.414...]]
 Methods
fit(X[, y]) Estimate the optimal parameter lambda for each feature.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Apply the inverse power transformation using the fitted lambdas.
set_params(**params) Set the parameters of this estimator.
transform(X) Apply the power transform to each feature using the fitted lambdas.
fit(X, y=None) [source]

Estimate the optimal parameter lambda for each feature. The optimal lambda parameter for minimizing skewness is estimated on each feature independently using maximum likelihood.  Parameters

Xarray-like of shape (n_samples, n_features)

The data used to estimate the optimal transformation parameters.
yNone

Ignored.    Returns

selfobject

Fitted transformer.

fit_transform(X, y=None) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Apply the inverse power transformation using the fitted lambdas. The inverse of the Box-Cox transformation is given by: if lambda_ == 0:
    X = exp(X_trans)
else:
    X = (X_trans * lambda_ + 1) ** (1 / lambda_)
 The inverse of the Yeo-Johnson transformation is given by: if X >= 0 and lambda_ == 0:
    X = exp(X_trans) - 1
elif X >= 0 and lambda_ != 0:
    X = (X_trans * lambda_ + 1) ** (1 / lambda_) - 1
elif X < 0 and lambda_ != 2:
    X = 1 - (-(2 - lambda_) * X_trans + 1) ** (1 / (2 - lambda_))
elif X < 0 and lambda_ == 2:
    X = 1 - exp(-X_trans)
  Parameters

Xarray-like of shape (n_samples, n_features)

The transformed data.    Returns

Xndarray of shape (n_samples, n_features)

The original data.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Apply the power transform to each feature using the fitted lambdas.  Parameters

Xarray-like of shape (n_samples, n_features)

The data to be transformed using a power transformation.    Returns

X_transndarray of shape (n_samples, n_features)

The transformed data.

 Examples using sklearn.preprocessing.PowerTransformer

  Map data to a normal distribution

  Compare the effect of different scalers on data with outliers


fit(X, y=None) [source]

Estimate the optimal parameter lambda for each feature. The optimal lambda parameter for minimizing skewness is estimated on each feature independently using maximum likelihood.  Parameters

Xarray-like of shape (n_samples, n_features)

The data used to estimate the optimal transformation parameters.
yNone

Ignored.    Returns

selfobject

Fitted transformer.


fit_transform(X, y=None) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


inverse_transform(X) [source]

Apply the inverse power transformation using the fitted lambdas. The inverse of the Box-Cox transformation is given by: if lambda_ == 0:
    X = exp(X_trans)
else:
    X = (X_trans * lambda_ + 1) ** (1 / lambda_)
 The inverse of the Yeo-Johnson transformation is given by: if X >= 0 and lambda_ == 0:
    X = exp(X_trans) - 1
elif X >= 0 and lambda_ != 0:
    X = (X_trans * lambda_ + 1) ** (1 / lambda_) - 1
elif X < 0 and lambda_ != 2:
    X = 1 - (-(2 - lambda_) * X_trans + 1) ** (1 / (2 - lambda_))
elif X < 0 and lambda_ == 2:
    X = 1 - exp(-X_trans)
  Parameters

Xarray-like of shape (n_samples, n_features)

The transformed data.    Returns

Xndarray of shape (n_samples, n_features)

The original data.


transform(X) [source]

Apply the power transform to each feature using the fitted lambdas.  Parameters

Xarray-like of shape (n_samples, n_features)

The data to be transformed using a power transformation.    Returns

X_transndarray of shape (n_samples, n_features)

The transformed data.


sklearn.preprocessing.power_transform(X, method='yeo-johnson', *, standardize=True, copy=True) [source]

Power transforms are a family of parametric, monotonic transformations that are applied to make data more Gaussian-like. This is useful for modeling issues related to heteroscedasticity (non-constant variance), or other situations where normality is desired. Currently, power_transform supports the Box-Cox transform and the Yeo-Johnson transform. The optimal parameter for stabilizing variance and minimizing skewness is estimated through maximum likelihood. Box-Cox requires input data to be strictly positive, while Yeo-Johnson supports both positive or negative data. By default, zero-mean, unit-variance normalization is applied to the transformed data. Read more in the User Guide.  Parameters

Xarray-like of shape (n_samples, n_features)

The data to be transformed using a power transformation.
method{‘yeo-johnson’, ‘box-cox’}, default=’yeo-johnson’

The power transform method. Available methods are:  ‘yeo-johnson’ [1], works with positive and negative values ‘box-cox’ [2], only works with strictly positive values   Changed in version 0.23: The default value of the method parameter changed from ‘box-cox’ to ‘yeo-johnson’ in 0.23.
standardizebool, default=True

Set to True to apply zero-mean, unit-variance normalization to the transformed output.
copybool, default=True

Set to False to perform inplace computation during transformation.    Returns

X_transndarray of shape (n_samples, n_features)

The transformed data.      See also
PowerTransformer

Equivalent transformation with the Transformer API (e.g. as part of a preprocessing Pipeline).
quantile_transform

Maps data to a standard normal distribution with the parameter output_distribution='normal'.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. References
1
I.K. Yeo and R.A. Johnson, “A new family of power transformations to improve normality or symmetry.” Biometrika, 87(4), pp.954-959, (2000).
2
G.E.P. Box and D.R. Cox, “An Analysis of Transformations”, Journal of the Royal Statistical Society B, 26, 211-252 (1964).   Examples >>> import numpy as np
>>> from sklearn.preprocessing import power_transform
>>> data = [[1, 2], [3, 2], [4, 5]]
>>> print(power_transform(data, method='box-cox'))
[[-1.332... -0.707...]
 [ 0.256... -0.707...]
 [ 1.076...  1.414...]]
  Warning Risk of data leak. Do not use power_transform unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using PowerTransformer within a Pipeline in order to prevent most risks of data leaking, e.g.: pipe = make_pipeline(PowerTransformer(),
LogisticRegression()).


class sklearn.preprocessing.QuantileTransformer(*, n_quantiles=1000, output_distribution='uniform', ignore_implicit_zeros=False, subsample=100000, random_state=None, copy=True) [source]

Transform features using quantiles information. This method transforms the features to follow a uniform or a normal distribution. Therefore, for a given feature, this transformation tends to spread out the most frequent values. It also reduces the impact of (marginal) outliers: this is therefore a robust preprocessing scheme. The transformation is applied on each feature independently. First an estimate of the cumulative distribution function of a feature is used to map the original values to a uniform distribution. The obtained values are then mapped to the desired output distribution using the associated quantile function. Features values of new/unseen data that fall below or above the fitted range will be mapped to the bounds of the output distribution. Note that this transform is non-linear. It may distort linear correlations between variables measured at the same scale but renders variables measured at different scales more directly comparable. Read more in the User Guide.  New in version 0.19.   Parameters

n_quantilesint, default=1000 or n_samples

Number of quantiles to be computed. It corresponds to the number of landmarks used to discretize the cumulative distribution function. If n_quantiles is larger than the number of samples, n_quantiles is set to the number of samples as a larger number of quantiles does not give a better approximation of the cumulative distribution function estimator.
output_distribution{‘uniform’, ‘normal’}, default=’uniform’

Marginal distribution for the transformed data. The choices are ‘uniform’ (default) or ‘normal’.
ignore_implicit_zerosbool, default=False

Only applies to sparse matrices. If True, the sparse entries of the matrix are discarded to compute the quantile statistics. If False, these entries are treated as zeros.
subsampleint, default=1e5

Maximum number of samples used to estimate the quantiles for computational efficiency. Note that the subsampling procedure may differ for value-identical sparse and dense matrices.
random_stateint, RandomState instance or None, default=None

Determines random number generation for subsampling and smoothing noise. Please see subsample for more details. Pass an int for reproducible results across multiple function calls. See Glossary
copybool, default=True

Set to False to perform inplace transformation and avoid a copy (if the input is already a numpy array).    Attributes

n_quantiles_int

The actual number of quantiles used to discretize the cumulative distribution function.
quantiles_ndarray of shape (n_quantiles, n_features)

The values corresponding the quantiles of reference.
references_ndarray of shape (n_quantiles, )

Quantiles of references.      See also
quantile_transform

Equivalent function without the estimator API.
PowerTransformer

Perform mapping to a normal distribution using a power transform.
StandardScaler

Perform standardization that is faster, but less robust to outliers.
RobustScaler

Perform robust standardization that removes the influence of outliers but does not put outliers and inliers on the same scale.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. Examples >>> import numpy as np
>>> from sklearn.preprocessing import QuantileTransformer
>>> rng = np.random.RandomState(0)
>>> X = np.sort(rng.normal(loc=0.5, scale=0.25, size=(25, 1)), axis=0)
>>> qt = QuantileTransformer(n_quantiles=10, random_state=0)
>>> qt.fit_transform(X)
array([...])
 Methods
fit(X[, y]) Compute the quantiles used for transforming.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Back-projection to the original space.
set_params(**params) Set the parameters of this estimator.
transform(X) Feature-wise transformation of the data.
fit(X, y=None) [source]

Compute the quantiles used for transforming.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.
yNone

Ignored.    Returns

selfobject

Fitted transformer.

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Back-projection to the original space.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.    Returns

Xt{ndarray, sparse matrix} of (n_samples, n_features)

The projected data.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Feature-wise transformation of the data.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.    Returns

Xt{ndarray, sparse matrix} of shape (n_samples, n_features)

The projected data.


sklearn.preprocessing.QuantileTransformer
class sklearn.preprocessing.QuantileTransformer(*, n_quantiles=1000, output_distribution='uniform', ignore_implicit_zeros=False, subsample=100000, random_state=None, copy=True) [source]

Transform features using quantiles information. This method transforms the features to follow a uniform or a normal distribution. Therefore, for a given feature, this transformation tends to spread out the most frequent values. It also reduces the impact of (marginal) outliers: this is therefore a robust preprocessing scheme. The transformation is applied on each feature independently. First an estimate of the cumulative distribution function of a feature is used to map the original values to a uniform distribution. The obtained values are then mapped to the desired output distribution using the associated quantile function. Features values of new/unseen data that fall below or above the fitted range will be mapped to the bounds of the output distribution. Note that this transform is non-linear. It may distort linear correlations between variables measured at the same scale but renders variables measured at different scales more directly comparable. Read more in the User Guide.  New in version 0.19.   Parameters

n_quantilesint, default=1000 or n_samples

Number of quantiles to be computed. It corresponds to the number of landmarks used to discretize the cumulative distribution function. If n_quantiles is larger than the number of samples, n_quantiles is set to the number of samples as a larger number of quantiles does not give a better approximation of the cumulative distribution function estimator.
output_distribution{‘uniform’, ‘normal’}, default=’uniform’

Marginal distribution for the transformed data. The choices are ‘uniform’ (default) or ‘normal’.
ignore_implicit_zerosbool, default=False

Only applies to sparse matrices. If True, the sparse entries of the matrix are discarded to compute the quantile statistics. If False, these entries are treated as zeros.
subsampleint, default=1e5

Maximum number of samples used to estimate the quantiles for computational efficiency. Note that the subsampling procedure may differ for value-identical sparse and dense matrices.
random_stateint, RandomState instance or None, default=None

Determines random number generation for subsampling and smoothing noise. Please see subsample for more details. Pass an int for reproducible results across multiple function calls. See Glossary
copybool, default=True

Set to False to perform inplace transformation and avoid a copy (if the input is already a numpy array).    Attributes

n_quantiles_int

The actual number of quantiles used to discretize the cumulative distribution function.
quantiles_ndarray of shape (n_quantiles, n_features)

The values corresponding the quantiles of reference.
references_ndarray of shape (n_quantiles, )

Quantiles of references.      See also
quantile_transform

Equivalent function without the estimator API.
PowerTransformer

Perform mapping to a normal distribution using a power transform.
StandardScaler

Perform standardization that is faster, but less robust to outliers.
RobustScaler

Perform robust standardization that removes the influence of outliers but does not put outliers and inliers on the same scale.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. Examples >>> import numpy as np
>>> from sklearn.preprocessing import QuantileTransformer
>>> rng = np.random.RandomState(0)
>>> X = np.sort(rng.normal(loc=0.5, scale=0.25, size=(25, 1)), axis=0)
>>> qt = QuantileTransformer(n_quantiles=10, random_state=0)
>>> qt.fit_transform(X)
array([...])
 Methods
fit(X[, y]) Compute the quantiles used for transforming.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Back-projection to the original space.
set_params(**params) Set the parameters of this estimator.
transform(X) Feature-wise transformation of the data.
fit(X, y=None) [source]

Compute the quantiles used for transforming.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.
yNone

Ignored.    Returns

selfobject

Fitted transformer.

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Back-projection to the original space.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.    Returns

Xt{ndarray, sparse matrix} of (n_samples, n_features)

The projected data.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Feature-wise transformation of the data.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.    Returns

Xt{ndarray, sparse matrix} of shape (n_samples, n_features)

The projected data.

 Examples using sklearn.preprocessing.QuantileTransformer

  Partial Dependence and Individual Conditional Expectation Plots

  Effect of transforming the targets in regression model

  Map data to a normal distribution

  Compare the effect of different scalers on data with outliers


fit(X, y=None) [source]

Compute the quantiles used for transforming.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.
yNone

Ignored.    Returns

selfobject

Fitted transformer.


inverse_transform(X) [source]

Back-projection to the original space.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.    Returns

Xt{ndarray, sparse matrix} of (n_samples, n_features)

The projected data.


transform(X) [source]

Feature-wise transformation of the data.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the features axis. If a sparse matrix is provided, it will be converted into a sparse csc_matrix. Additionally, the sparse matrix needs to be nonnegative if ignore_implicit_zeros is False.    Returns

Xt{ndarray, sparse matrix} of shape (n_samples, n_features)

The projected data.


sklearn.preprocessing.quantile_transform(X, *, axis=0, n_quantiles=1000, output_distribution='uniform', ignore_implicit_zeros=False, subsample=100000, random_state=None, copy=True) [source]

Transform features using quantiles information. This method transforms the features to follow a uniform or a normal distribution. Therefore, for a given feature, this transformation tends to spread out the most frequent values. It also reduces the impact of (marginal) outliers: this is therefore a robust preprocessing scheme. The transformation is applied on each feature independently. First an estimate of the cumulative distribution function of a feature is used to map the original values to a uniform distribution. The obtained values are then mapped to the desired output distribution using the associated quantile function. Features values of new/unseen data that fall below or above the fitted range will be mapped to the bounds of the output distribution. Note that this transform is non-linear. It may distort linear correlations between variables measured at the same scale but renders variables measured at different scales more directly comparable. Read more in the User Guide.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data to transform.
axisint, default=0

Axis used to compute the means and standard deviations along. If 0, transform each feature, otherwise (if 1) transform each sample.
n_quantilesint, default=1000 or n_samples

Number of quantiles to be computed. It corresponds to the number of landmarks used to discretize the cumulative distribution function. If n_quantiles is larger than the number of samples, n_quantiles is set to the number of samples as a larger number of quantiles does not give a better approximation of the cumulative distribution function estimator.
output_distribution{‘uniform’, ‘normal’}, default=’uniform’

Marginal distribution for the transformed data. The choices are ‘uniform’ (default) or ‘normal’.
ignore_implicit_zerosbool, default=False

Only applies to sparse matrices. If True, the sparse entries of the matrix are discarded to compute the quantile statistics. If False, these entries are treated as zeros.
subsampleint, default=1e5

Maximum number of samples used to estimate the quantiles for computational efficiency. Note that the subsampling procedure may differ for value-identical sparse and dense matrices.
random_stateint, RandomState instance or None, default=None

Determines random number generation for subsampling and smoothing noise. Please see subsample for more details. Pass an int for reproducible results across multiple function calls. See Glossary
copybool, default=True

Set to False to perform inplace transformation and avoid a copy (if the input is already a numpy array). If True, a copy of X is transformed, leaving the original X unchanged  ..versionchanged:: 0.23

The default value of copy changed from False to True in 0.23.      Returns

Xt{ndarray, sparse matrix} of shape (n_samples, n_features)

The transformed data.      See also
QuantileTransformer

Performs quantile-based scaling using the Transformer API (e.g. as part of a preprocessing Pipeline).
power_transform

Maps data to a normal distribution using a power transformation.
scale

Performs standardization that is faster, but less robust to outliers.
robust_scale

Performs robust standardization that removes the influence of outliers but does not put outliers and inliers on the same scale.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform.  Warning Risk of data leak Do not use quantile_transform unless you know what you are doing. A common mistake is to apply it to the entire data before splitting into training and test sets. This will bias the model evaluation because information would have leaked from the test set to the training set. In general, we recommend using QuantileTransformer within a Pipeline in order to prevent most risks of data leaking:pipe = make_pipeline(QuantileTransformer(),
LogisticRegression()).  For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. Examples >>> import numpy as np
>>> from sklearn.preprocessing import quantile_transform
>>> rng = np.random.RandomState(0)
>>> X = np.sort(rng.normal(loc=0.5, scale=0.25, size=(25, 1)), axis=0)
>>> quantile_transform(X, n_quantiles=10, random_state=0, copy=True)
array([...])


class sklearn.preprocessing.RobustScaler(*, with_centering=True, with_scaling=True, quantile_range=25.0, 75.0, copy=True, unit_variance=False) [source]

Scale features using statistics that are robust to outliers. This Scaler removes the median and scales the data according to the quantile range (defaults to IQR: Interquartile Range). The IQR is the range between the 1st quartile (25th quantile) and the 3rd quartile (75th quantile). Centering and scaling happen independently on each feature by computing the relevant statistics on the samples in the training set. Median and interquartile range are then stored to be used on later data using the transform method. Standardization of a dataset is a common requirement for many machine learning estimators. Typically this is done by removing the mean and scaling to unit variance. However, outliers can often influence the sample mean / variance in a negative way. In such cases, the median and the interquartile range often give better results.  New in version 0.17.  Read more in the User Guide.  Parameters

with_centeringbool, default=True

If True, center the data before scaling. This will cause transform to raise an exception when attempted on sparse matrices, because centering them entails building a dense matrix which in common use cases is likely to be too large to fit in memory.
with_scalingbool, default=True

If True, scale the data to interquartile range.
quantile_rangetuple (q_min, q_max), 0.0 < q_min < q_max < 100.0, default=(25.0, 75.0), == (1st quantile, 3rd quantile), == IQR

Quantile range used to calculate scale_.  New in version 0.18.
copybool, default=True

If False, try to avoid a copy and do inplace scaling instead. This is not guaranteed to always work inplace; e.g. if the data is not a NumPy array or scipy.sparse CSR matrix, a copy may still be returned.
unit_variancebool, default=False

If True, scale data so that normally distributed features have a variance of 1. In general, if the difference between the x-values of q_max and q_min for a standard normal distribution is greater than 1, the dataset will be scaled down. If less than 1, the dataset will be scaled up.  New in version 0.24.     Attributes

center_array of floats

The median value for each feature in the training set.
scale_array of floats

The (scaled) interquartile range for each feature in the training set.  New in version 0.17: scale_ attribute.       See also
robust_scale

Equivalent function without the estimator API.
PCA

Further removes the linear correlation across features with ‘whiten=True’.    Notes For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. https://en.wikipedia.org/wiki/Median https://en.wikipedia.org/wiki/Interquartile_range Examples >>> from sklearn.preprocessing import RobustScaler
>>> X = [[ 1., -2.,  2.],
...      [ -2.,  1.,  3.],
...      [ 4.,  1., -2.]]
>>> transformer = RobustScaler().fit(X)
>>> transformer
RobustScaler()
>>> transformer.transform(X)
array([[ 0. , -2. ,  0. ],
       [-1. ,  0. ,  0.4],
       [ 1. ,  0. , -1.6]])
 Methods
fit(X[, y]) Compute the median and quantiles to be used for scaling.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X) Scale back the data to the original representation
set_params(**params) Set the parameters of this estimator.
transform(X) Center and scale the data.
fit(X, y=None) [source]

Compute the median and quantiles to be used for scaling.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to compute the median and quantiles used for later scaling along the features axis.
yNone

Ignored.    Returns

selfobject

Fitted scaler.

fit_transform(X, y=None, **fit_params) [source]

Fit to data, then transform it. Fits transformer to X and y with optional parameters fit_params and returns a transformed version of X.  Parameters

Xarray-like of shape (n_samples, n_features)

Input samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs), default=None

Target values (None for unsupervised transformations).
**fit_paramsdict

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

If True, will return the parameters for this estimator and contained subobjects that are estimators.    Returns

paramsdict

Parameter names mapped to their values.

inverse_transform(X) [source]

Scale back the data to the original representation  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The rescaled data to be transformed back.    Returns

X_tr{ndarray, sparse matrix} of shape (n_samples, n_features)

Transformed array.

set_params(**params) [source]

Set the parameters of this estimator. The method works on simple estimators as well as on nested objects (such as Pipeline). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.  Parameters

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Center and scale the data.  Parameters

X{array-like, sparse matrix} of shape (n_samples, n_features)

The data used to scale along the specified axis.    Returns

X_tr{ndarray, sparse matrix} of shape (n_samples, n_features)

Transformed array.