sklearn.preprocessing.RobustScaler
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.

 Examples using sklearn.preprocessing.RobustScaler

  Compare the effect of different scalers on data with outliers


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.


sklearn.preprocessing.robust_scale(X, *, axis=0, with_centering=True, with_scaling=True, quantile_range=25.0, 75.0, copy=True, unit_variance=False) [source]

Standardize a dataset along any axis Center to the median and component wise scale according to the interquartile range. Read more in the User Guide.  Parameters

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

The data to center and scale.
axisint, default=0

axis used to compute the medians and IQR along. If 0, independently scale each feature, otherwise (if 1) scale each sample.
with_centeringbool, default=True

If True, center the data before scaling.
with_scalingbool, default=True

If True, scale the data to unit variance (or equivalently, unit standard deviation).
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

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 and if axis is 1).
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.     Returns

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

The transformed data.      See also
RobustScaler

Performs centering and scaling using the Transformer API (e.g. as part of a preprocessing Pipeline).    Notes This implementation will refuse to center scipy.sparse matrices since it would make them non-sparse and would potentially crash the program with memory exhaustion problems. Instead the caller is expected to either set explicitly with_centering=False (in that case, only variance scaling will be performed on the features of the CSR matrix) or to call X.toarray() if he/she expects the materialized dense array to fit in memory. To avoid memory copy the caller should pass a CSR matrix. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.  Warning Risk of data leak Do not use robust_scale 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 RobustScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(RobustScaler(), LogisticRegression()).


sklearn.preprocessing.scale(X, *, axis=0, with_mean=True, with_std=True, copy=True) [source]

Standardize a dataset along any axis. Center to the mean and component wise scale to unit variance. Read more in the User Guide.  Parameters

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

The data to center and scale.
axisint, default=0

axis used to compute the means and standard deviations along. If 0, independently standardize each feature, otherwise (if 1) standardize each sample.
with_meanbool, default=True

If True, center the data before scaling.
with_stdbool, default=True

If True, scale the data to unit variance (or equivalently, unit standard deviation).
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 CSC matrix and if axis is 1).    Returns

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

The transformed data.      See also
StandardScaler

Performs scaling to unit variance using the Transformer API (e.g. as part of a preprocessing Pipeline).    Notes This implementation will refuse to center scipy.sparse matrices since it would make them non-sparse and would potentially crash the program with memory exhaustion problems. Instead the caller is expected to either set explicitly with_mean=False (in that case, only variance scaling will be performed on the features of the CSC matrix) or to call X.toarray() if he/she expects the materialized dense array to fit in memory. To avoid memory copy the caller should pass a CSC matrix. NaNs are treated as missing values: disregarded to compute the statistics, and maintained during the data transformation. We use a biased estimator for the standard deviation, equivalent to numpy.std(x, ddof=0). Note that the choice of ddof is unlikely to affect model performance. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py.  Warning Risk of data leak Do not use scale 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 StandardScaler within a Pipeline in order to prevent most risks of data leaking: pipe = make_pipeline(StandardScaler(), LogisticRegression()).


class sklearn.preprocessing.StandardScaler(*, copy=True, with_mean=True, with_std=True) [source]

Standardize features by removing the mean and scaling to unit variance The standard score of a sample x is calculated as: z = (x - u) / s where u is the mean of the training samples or zero if with_mean=False, and s is the standard deviation of the training samples or one if with_std=False. Centering and scaling happen independently on each feature by computing the relevant statistics on the samples in the training set. Mean and standard deviation are then stored to be used on later data using transform. Standardization of a dataset is a common requirement for many machine learning estimators: they might behave badly if the individual features do not more or less look like standard normally distributed data (e.g. Gaussian with 0 mean and unit variance). For instance many elements used in the objective function of a learning algorithm (such as the RBF kernel of Support Vector Machines or the L1 and L2 regularizers of linear models) assume that all features are centered around 0 and have variance in the same order. If a feature has a variance that is orders of magnitude larger that others, it might dominate the objective function and make the estimator unable to learn from other features correctly as expected. This scaler can also be applied to sparse CSR or CSC matrices by passing with_mean=False to avoid breaking the sparsity structure of the data. Read more in the User Guide.  Parameters

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.
with_meanbool, default=True

If True, center the data before scaling. This does not work (and will 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_stdbool, default=True

If True, scale the data to unit variance (or equivalently, unit standard deviation).    Attributes

scale_ndarray of shape (n_features,) or None

Per feature relative scaling of the data to achieve zero mean and unit variance. Generally this is calculated using np.sqrt(var_). If a variance is zero, we can’t achieve unit variance, and the data is left as-is, giving a scaling factor of 1. scale_ is equal to None when with_std=False.  New in version 0.17: scale_
mean_ndarray of shape (n_features,) or None

The mean value for each feature in the training set. Equal to None when with_mean=False.
var_ndarray of shape (n_features,) or None

The variance for each feature in the training set. Used to compute scale_. Equal to None when with_std=False.
n_samples_seen_int or ndarray of shape (n_features,)

The number of samples processed by the estimator for each feature. If there are no missing samples, the n_samples_seen will be an integer, otherwise it will be an array of dtype int. If sample_weights are used it will be a float (if no missing data) or an array of dtype float that sums the weights seen so far. Will be reset on new calls to fit, but increments across partial_fit calls.      See also
scale

Equivalent function without the estimator API.
PCA

Further removes the linear correlation across features with ‘whiten=True’.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform. We use a biased estimator for the standard deviation, equivalent to numpy.std(x, ddof=0). Note that the choice of ddof is unlikely to affect model performance. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. Examples >>> from sklearn.preprocessing import StandardScaler
>>> data = [[0, 0], [0, 0], [1, 1], [1, 1]]
>>> scaler = StandardScaler()
>>> print(scaler.fit(data))
StandardScaler()
>>> print(scaler.mean_)
[0.5 0.5]
>>> print(scaler.transform(data))
[[-1. -1.]
 [-1. -1.]
 [ 1.  1.]
 [ 1.  1.]]
>>> print(scaler.transform([[2, 2]]))
[[3. 3.]]
 Methods
fit(X[, y, sample_weight]) Compute the mean and std to be used for later scaling.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X[, copy]) Scale back the data to the original representation
partial_fit(X[, y, sample_weight]) Online computation of mean and std on X for later scaling.
set_params(**params) Set the parameters of this estimator.
transform(X[, copy]) Perform standardization by centering and scaling
fit(X, y=None, sample_weight=None) [source]

Compute the mean and std to be used for later scaling.  Parameters

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

The data used to compute the mean and standard deviation used for later scaling along the features axis.
yNone

Ignored.
sample_weightarray-like of shape (n_samples,), default=None

Individual weights for each sample.  New in version 0.24: parameter sample_weight support to StandardScaler.     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, copy=None) [source]

Scale back the data to the original representation  Parameters

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

The data used to scale along the features axis.
copybool, default=None

Copy the input X or not.    Returns

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

Transformed array.

partial_fit(X, y=None, sample_weight=None) [source]

Online computation of mean and std on X for later scaling. All of X is processed as a single batch. This is intended for cases when fit is not feasible due to very large number of n_samples or because X is read from a continuous stream. The algorithm for incremental mean and std is given in Equation 1.5a,b in Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. “Algorithms for computing the sample variance: Analysis and recommendations.” The American Statistician 37.3 (1983): 242-247:  Parameters

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

The data used to compute the mean and standard deviation used for later scaling along the features axis.
yNone

Ignored.
sample_weightarray-like of shape (n_samples,), default=None

Individual weights for each sample.  New in version 0.24: parameter sample_weight support to StandardScaler.     Returns

selfobject

Fitted scaler.

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]

Perform standardization by centering and scaling  Parameters

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

The data used to scale along the features axis.
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.StandardScaler
class sklearn.preprocessing.StandardScaler(*, copy=True, with_mean=True, with_std=True) [source]

Standardize features by removing the mean and scaling to unit variance The standard score of a sample x is calculated as: z = (x - u) / s where u is the mean of the training samples or zero if with_mean=False, and s is the standard deviation of the training samples or one if with_std=False. Centering and scaling happen independently on each feature by computing the relevant statistics on the samples in the training set. Mean and standard deviation are then stored to be used on later data using transform. Standardization of a dataset is a common requirement for many machine learning estimators: they might behave badly if the individual features do not more or less look like standard normally distributed data (e.g. Gaussian with 0 mean and unit variance). For instance many elements used in the objective function of a learning algorithm (such as the RBF kernel of Support Vector Machines or the L1 and L2 regularizers of linear models) assume that all features are centered around 0 and have variance in the same order. If a feature has a variance that is orders of magnitude larger that others, it might dominate the objective function and make the estimator unable to learn from other features correctly as expected. This scaler can also be applied to sparse CSR or CSC matrices by passing with_mean=False to avoid breaking the sparsity structure of the data. Read more in the User Guide.  Parameters

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.
with_meanbool, default=True

If True, center the data before scaling. This does not work (and will 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_stdbool, default=True

If True, scale the data to unit variance (or equivalently, unit standard deviation).    Attributes

scale_ndarray of shape (n_features,) or None

Per feature relative scaling of the data to achieve zero mean and unit variance. Generally this is calculated using np.sqrt(var_). If a variance is zero, we can’t achieve unit variance, and the data is left as-is, giving a scaling factor of 1. scale_ is equal to None when with_std=False.  New in version 0.17: scale_
mean_ndarray of shape (n_features,) or None

The mean value for each feature in the training set. Equal to None when with_mean=False.
var_ndarray of shape (n_features,) or None

The variance for each feature in the training set. Used to compute scale_. Equal to None when with_std=False.
n_samples_seen_int or ndarray of shape (n_features,)

The number of samples processed by the estimator for each feature. If there are no missing samples, the n_samples_seen will be an integer, otherwise it will be an array of dtype int. If sample_weights are used it will be a float (if no missing data) or an array of dtype float that sums the weights seen so far. Will be reset on new calls to fit, but increments across partial_fit calls.      See also
scale

Equivalent function without the estimator API.
PCA

Further removes the linear correlation across features with ‘whiten=True’.    Notes NaNs are treated as missing values: disregarded in fit, and maintained in transform. We use a biased estimator for the standard deviation, equivalent to numpy.std(x, ddof=0). Note that the choice of ddof is unlikely to affect model performance. For a comparison of the different scalers, transformers, and normalizers, see examples/preprocessing/plot_all_scaling.py. Examples >>> from sklearn.preprocessing import StandardScaler
>>> data = [[0, 0], [0, 0], [1, 1], [1, 1]]
>>> scaler = StandardScaler()
>>> print(scaler.fit(data))
StandardScaler()
>>> print(scaler.mean_)
[0.5 0.5]
>>> print(scaler.transform(data))
[[-1. -1.]
 [-1. -1.]
 [ 1.  1.]
 [ 1.  1.]]
>>> print(scaler.transform([[2, 2]]))
[[3. 3.]]
 Methods
fit(X[, y, sample_weight]) Compute the mean and std to be used for later scaling.
fit_transform(X[, y]) Fit to data, then transform it.
get_params([deep]) Get parameters for this estimator.
inverse_transform(X[, copy]) Scale back the data to the original representation
partial_fit(X[, y, sample_weight]) Online computation of mean and std on X for later scaling.
set_params(**params) Set the parameters of this estimator.
transform(X[, copy]) Perform standardization by centering and scaling
fit(X, y=None, sample_weight=None) [source]

Compute the mean and std to be used for later scaling.  Parameters

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

The data used to compute the mean and standard deviation used for later scaling along the features axis.
yNone

Ignored.
sample_weightarray-like of shape (n_samples,), default=None

Individual weights for each sample.  New in version 0.24: parameter sample_weight support to StandardScaler.     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, copy=None) [source]

Scale back the data to the original representation  Parameters

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

The data used to scale along the features axis.
copybool, default=None

Copy the input X or not.    Returns

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

Transformed array.

partial_fit(X, y=None, sample_weight=None) [source]

Online computation of mean and std on X for later scaling. All of X is processed as a single batch. This is intended for cases when fit is not feasible due to very large number of n_samples or because X is read from a continuous stream. The algorithm for incremental mean and std is given in Equation 1.5a,b in Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. “Algorithms for computing the sample variance: Analysis and recommendations.” The American Statistician 37.3 (1983): 242-247:  Parameters

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

The data used to compute the mean and standard deviation used for later scaling along the features axis.
yNone

Ignored.
sample_weightarray-like of shape (n_samples,), default=None

Individual weights for each sample.  New in version 0.24: parameter sample_weight support to StandardScaler.     Returns

selfobject

Fitted scaler.

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]

Perform standardization by centering and scaling  Parameters

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

The data used to scale along the features axis.
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.StandardScaler

  Release Highlights for scikit-learn 0.23

  Release Highlights for scikit-learn 0.22

  Classifier comparison

  Demo of DBSCAN clustering algorithm

  Comparing different hierarchical linkage methods on toy datasets

  A demo of K-Means clustering on the handwritten digits data

  Comparing different clustering algorithms on toy datasets

  Principal Component Regression vs Partial Least Squares Regression

  Factor Analysis (with rotation) to visualize patterns

  Combine predictors using stacking

  Prediction Latency

  MNIST classification using multinomial logistic + L1

  L1 Penalty and Sparsity in Logistic Regression

  Poisson regression and non-normal loss

  Tweedie regression on insurance claims

  Common pitfalls in interpretation of coefficients of linear models

  Visualizations with Display Objects

  Advanced Plotting With Partial Dependence

  Detection error tradeoff (DET) curve

  Comparing Nearest Neighbors with and without Neighborhood Components Analysis

  Dimensionality Reduction with Neighborhood Components Analysis

  Varying regularization in Multi-layer Perceptron

  Column Transformer with Mixed Types

  Importance of Feature Scaling

  Feature discretization

  Compare the effect of different scalers on data with outliers

  SVM-Anova: SVM with univariate feature selection

  RBF SVM parameters


fit(X, y=None, sample_weight=None) [source]

Compute the mean and std to be used for later scaling.  Parameters

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

The data used to compute the mean and standard deviation used for later scaling along the features axis.
yNone

Ignored.
sample_weightarray-like of shape (n_samples,), default=None

Individual weights for each sample.  New in version 0.24: parameter sample_weight support to StandardScaler.     Returns

selfobject

Fitted scaler.


inverse_transform(X, copy=None) [source]

Scale back the data to the original representation  Parameters

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

The data used to scale along the features axis.
copybool, default=None

Copy the input X or not.    Returns

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

Transformed array.


partial_fit(X, y=None, sample_weight=None) [source]

Online computation of mean and std on X for later scaling. All of X is processed as a single batch. This is intended for cases when fit is not feasible due to very large number of n_samples or because X is read from a continuous stream. The algorithm for incremental mean and std is given in Equation 1.5a,b in Chan, Tony F., Gene H. Golub, and Randall J. LeVeque. “Algorithms for computing the sample variance: Analysis and recommendations.” The American Statistician 37.3 (1983): 242-247:  Parameters

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

The data used to compute the mean and standard deviation used for later scaling along the features axis.
yNone

Ignored.
sample_weightarray-like of shape (n_samples,), default=None

Individual weights for each sample.  New in version 0.24: parameter sample_weight support to StandardScaler.     Returns

selfobject

Fitted scaler.


transform(X, copy=None) [source]

Perform standardization by centering and scaling  Parameters

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

The data used to scale along the features axis.
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.random_projection.GaussianRandomProjection(n_components='auto', *, eps=0.1, random_state=None) [source]

Reduce dimensionality through Gaussian random projection. The components of the random matrix are drawn from N(0, 1 / n_components). Read more in the User Guide.  New in version 0.13.   Parameters

n_componentsint or ‘auto’, default=’auto’

Dimensionality of the target projection space. n_components can be automatically adjusted according to the number of samples in the dataset and the bound given by the Johnson-Lindenstrauss lemma. In that case the quality of the embedding is controlled by the eps parameter. It should be noted that Johnson-Lindenstrauss lemma can yield very conservative estimated of the required number of components as it makes no assumption on the structure of the dataset.
epsfloat, default=0.1

Parameter to control the quality of the embedding according to the Johnson-Lindenstrauss lemma when n_components is set to ‘auto’. The value should be strictly positive. Smaller values lead to better embedding and higher number of dimensions (n_components) in the target projection space.
random_stateint, RandomState instance or None, default=None

Controls the pseudo random number generator used to generate the projection matrix at fit time. Pass an int for reproducible output across multiple function calls. See Glossary.    Attributes

n_components_int

Concrete number of components computed when n_components=”auto”.
components_ndarray of shape (n_components, n_features)

Random matrix used for the projection.      See also
SparseRandomProjection

  Examples >>> import numpy as np
>>> from sklearn.random_projection import GaussianRandomProjection
>>> rng = np.random.RandomState(42)
>>> X = rng.rand(100, 10000)
>>> transformer = GaussianRandomProjection(random_state=rng)
>>> X_new = transformer.fit_transform(X)
>>> X_new.shape
(100, 3947)
 Methods
fit(X[, y]) Generate a sparse random projection matrix.
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) Project the data by using matrix product with the random matrix
fit(X, y=None) [source]

Generate a sparse random projection matrix.  Parameters

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

Training set: only the shape is used to find optimal random matrix dimensions based on the theory referenced in the afore mentioned papers.  y

Ignored    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.

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]

Project the data by using matrix product with the random matrix  Parameters

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

The input data to project into a smaller dimensional space.    Returns

X_new{ndarray, sparse matrix} of shape (n_samples, n_components)

Projected array.


sklearn.random_projection.GaussianRandomProjection
class sklearn.random_projection.GaussianRandomProjection(n_components='auto', *, eps=0.1, random_state=None) [source]

Reduce dimensionality through Gaussian random projection. The components of the random matrix are drawn from N(0, 1 / n_components). Read more in the User Guide.  New in version 0.13.   Parameters

n_componentsint or ‘auto’, default=’auto’

Dimensionality of the target projection space. n_components can be automatically adjusted according to the number of samples in the dataset and the bound given by the Johnson-Lindenstrauss lemma. In that case the quality of the embedding is controlled by the eps parameter. It should be noted that Johnson-Lindenstrauss lemma can yield very conservative estimated of the required number of components as it makes no assumption on the structure of the dataset.
epsfloat, default=0.1

Parameter to control the quality of the embedding according to the Johnson-Lindenstrauss lemma when n_components is set to ‘auto’. The value should be strictly positive. Smaller values lead to better embedding and higher number of dimensions (n_components) in the target projection space.
random_stateint, RandomState instance or None, default=None

Controls the pseudo random number generator used to generate the projection matrix at fit time. Pass an int for reproducible output across multiple function calls. See Glossary.    Attributes

n_components_int

Concrete number of components computed when n_components=”auto”.
components_ndarray of shape (n_components, n_features)

Random matrix used for the projection.      See also
SparseRandomProjection

  Examples >>> import numpy as np
>>> from sklearn.random_projection import GaussianRandomProjection
>>> rng = np.random.RandomState(42)
>>> X = rng.rand(100, 10000)
>>> transformer = GaussianRandomProjection(random_state=rng)
>>> X_new = transformer.fit_transform(X)
>>> X_new.shape
(100, 3947)
 Methods
fit(X[, y]) Generate a sparse random projection matrix.
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) Project the data by using matrix product with the random matrix
fit(X, y=None) [source]

Generate a sparse random projection matrix.  Parameters

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

Training set: only the shape is used to find optimal random matrix dimensions based on the theory referenced in the afore mentioned papers.  y

Ignored    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.

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]

Project the data by using matrix product with the random matrix  Parameters

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

The input data to project into a smaller dimensional space.    Returns

X_new{ndarray, sparse matrix} of shape (n_samples, n_components)

Projected array.


fit(X, y=None) [source]

Generate a sparse random projection matrix.  Parameters

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

Training set: only the shape is used to find optimal random matrix dimensions based on the theory referenced in the afore mentioned papers.  y

Ignored    Returns
 self


transform(X) [source]

Project the data by using matrix product with the random matrix  Parameters

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

The input data to project into a smaller dimensional space.    Returns

X_new{ndarray, sparse matrix} of shape (n_samples, n_components)

Projected array.


sklearn.random_projection.johnson_lindenstrauss_min_dim(n_samples, *, eps=0.1) [source]

Find a ‘safe’ number of components to randomly project to. The distortion introduced by a random projection p only changes the distance between two points by a factor (1 +- eps) in an euclidean space with good probability. The projection p is an eps-embedding as defined by: (1 - eps) ||u - v||^2 < ||p(u) - p(v)||^2 < (1 + eps) ||u - v||^2 Where u and v are any rows taken from a dataset of shape (n_samples, n_features), eps is in ]0, 1[ and p is a projection by a random Gaussian N(0, 1) matrix of shape (n_components, n_features) (or a sparse Achlioptas matrix). The minimum number of components to guarantee the eps-embedding is given by: n_components >= 4 log(n_samples) / (eps^2 / 2 - eps^3 / 3) Note that the number of dimensions is independent of the original number of features but instead depends on the size of the dataset: the larger the dataset, the higher is the minimal dimensionality of an eps-embedding. Read more in the User Guide.  Parameters

n_samplesint or array-like of int

Number of samples that should be a integer greater than 0. If an array is given, it will compute a safe number of components array-wise.
epsfloat or ndarray of shape (n_components,), dtype=float, default=0.1

Maximum distortion rate in the range (0,1 ) as defined by the Johnson-Lindenstrauss lemma. If an array is given, it will compute a safe number of components array-wise.    Returns

n_componentsint or ndarray of int

The minimal number of components to guarantee with good probability an eps-embedding with n_samples.     References
1
https://en.wikipedia.org/wiki/Johnson%E2%80%93Lindenstrauss_lemma
2
Sanjoy Dasgupta and Anupam Gupta, 1999, “An elementary proof of the Johnson-Lindenstrauss Lemma.” http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.45.3654   Examples >>> johnson_lindenstrauss_min_dim(1e6, eps=0.5)
663
 >>> johnson_lindenstrauss_min_dim(1e6, eps=[0.5, 0.1, 0.01])
array([    663,   11841, 1112658])
 >>> johnson_lindenstrauss_min_dim([1e4, 1e5, 1e6], eps=0.1)
array([ 7894,  9868, 11841])


class sklearn.random_projection.SparseRandomProjection(n_components='auto', *, density='auto', eps=0.1, dense_output=False, random_state=None) [source]

Reduce dimensionality through sparse random projection. Sparse random matrix is an alternative to dense random projection matrix that guarantees similar embedding quality while being much more memory efficient and allowing faster computation of the projected data. If we note s = 1 / density the components of the random matrix are drawn from:  -sqrt(s) / sqrt(n_components) with probability 1 / 2s 0 with probability 1 - 1 / s +sqrt(s) / sqrt(n_components) with probability 1 / 2s  Read more in the User Guide.  New in version 0.13.   Parameters

n_componentsint or ‘auto’, default=’auto’

Dimensionality of the target projection space. n_components can be automatically adjusted according to the number of samples in the dataset and the bound given by the Johnson-Lindenstrauss lemma. In that case the quality of the embedding is controlled by the eps parameter. It should be noted that Johnson-Lindenstrauss lemma can yield very conservative estimated of the required number of components as it makes no assumption on the structure of the dataset.
densityfloat or ‘auto’, default=’auto’

Ratio in the range (0, 1] of non-zero component in the random projection matrix. If density = ‘auto’, the value is set to the minimum density as recommended by Ping Li et al.: 1 / sqrt(n_features). Use density = 1 / 3.0 if you want to reproduce the results from Achlioptas, 2001.
epsfloat, default=0.1

Parameter to control the quality of the embedding according to the Johnson-Lindenstrauss lemma when n_components is set to ‘auto’. This value should be strictly positive. Smaller values lead to better embedding and higher number of dimensions (n_components) in the target projection space.
dense_outputbool, default=False

If True, ensure that the output of the random projection is a dense numpy array even if the input and random projection matrix are both sparse. In practice, if the number of components is small the number of zero components in the projected data will be very small and it will be more CPU and memory efficient to use a dense representation. If False, the projected data uses a sparse representation if the input is sparse.
random_stateint, RandomState instance or None, default=None

Controls the pseudo random number generator used to generate the projection matrix at fit time. Pass an int for reproducible output across multiple function calls. See Glossary.    Attributes

n_components_int

Concrete number of components computed when n_components=”auto”.
components_sparse matrix of shape (n_components, n_features)

Random matrix used for the projection. Sparse matrix will be of CSR format.
density_float in range 0.0 - 1.0

Concrete density computed from when density = “auto”.      See also
GaussianRandomProjection

  References
1
Ping Li, T. Hastie and K. W. Church, 2006, “Very Sparse Random Projections”. https://web.stanford.edu/~hastie/Papers/Ping/KDD06_rp.pdf
2
D. Achlioptas, 2001, “Database-friendly random projections”, https://users.soe.ucsc.edu/~optas/papers/jl.pdf   Examples >>> import numpy as np
>>> from sklearn.random_projection import SparseRandomProjection
>>> rng = np.random.RandomState(42)
>>> X = rng.rand(100, 10000)
>>> transformer = SparseRandomProjection(random_state=rng)
>>> X_new = transformer.fit_transform(X)
>>> X_new.shape
(100, 3947)
>>> # very few components are non-zero
>>> np.mean(transformer.components_ != 0)
0.0100...
 Methods
fit(X[, y]) Generate a sparse random projection matrix.
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) Project the data by using matrix product with the random matrix
fit(X, y=None) [source]

Generate a sparse random projection matrix.  Parameters

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

Training set: only the shape is used to find optimal random matrix dimensions based on the theory referenced in the afore mentioned papers.  y

Ignored    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.

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]

Project the data by using matrix product with the random matrix  Parameters

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

The input data to project into a smaller dimensional space.    Returns

X_new{ndarray, sparse matrix} of shape (n_samples, n_components)

Projected array.


sklearn.random_projection.SparseRandomProjection
class sklearn.random_projection.SparseRandomProjection(n_components='auto', *, density='auto', eps=0.1, dense_output=False, random_state=None) [source]

Reduce dimensionality through sparse random projection. Sparse random matrix is an alternative to dense random projection matrix that guarantees similar embedding quality while being much more memory efficient and allowing faster computation of the projected data. If we note s = 1 / density the components of the random matrix are drawn from:  -sqrt(s) / sqrt(n_components) with probability 1 / 2s 0 with probability 1 - 1 / s +sqrt(s) / sqrt(n_components) with probability 1 / 2s  Read more in the User Guide.  New in version 0.13.   Parameters

n_componentsint or ‘auto’, default=’auto’

Dimensionality of the target projection space. n_components can be automatically adjusted according to the number of samples in the dataset and the bound given by the Johnson-Lindenstrauss lemma. In that case the quality of the embedding is controlled by the eps parameter. It should be noted that Johnson-Lindenstrauss lemma can yield very conservative estimated of the required number of components as it makes no assumption on the structure of the dataset.
densityfloat or ‘auto’, default=’auto’

Ratio in the range (0, 1] of non-zero component in the random projection matrix. If density = ‘auto’, the value is set to the minimum density as recommended by Ping Li et al.: 1 / sqrt(n_features). Use density = 1 / 3.0 if you want to reproduce the results from Achlioptas, 2001.
epsfloat, default=0.1

Parameter to control the quality of the embedding according to the Johnson-Lindenstrauss lemma when n_components is set to ‘auto’. This value should be strictly positive. Smaller values lead to better embedding and higher number of dimensions (n_components) in the target projection space.
dense_outputbool, default=False

If True, ensure that the output of the random projection is a dense numpy array even if the input and random projection matrix are both sparse. In practice, if the number of components is small the number of zero components in the projected data will be very small and it will be more CPU and memory efficient to use a dense representation. If False, the projected data uses a sparse representation if the input is sparse.
random_stateint, RandomState instance or None, default=None

Controls the pseudo random number generator used to generate the projection matrix at fit time. Pass an int for reproducible output across multiple function calls. See Glossary.    Attributes

n_components_int

Concrete number of components computed when n_components=”auto”.
components_sparse matrix of shape (n_components, n_features)

Random matrix used for the projection. Sparse matrix will be of CSR format.
density_float in range 0.0 - 1.0

Concrete density computed from when density = “auto”.      See also
GaussianRandomProjection

  References
1
Ping Li, T. Hastie and K. W. Church, 2006, “Very Sparse Random Projections”. https://web.stanford.edu/~hastie/Papers/Ping/KDD06_rp.pdf
2
D. Achlioptas, 2001, “Database-friendly random projections”, https://users.soe.ucsc.edu/~optas/papers/jl.pdf   Examples >>> import numpy as np
>>> from sklearn.random_projection import SparseRandomProjection
>>> rng = np.random.RandomState(42)
>>> X = rng.rand(100, 10000)
>>> transformer = SparseRandomProjection(random_state=rng)
>>> X_new = transformer.fit_transform(X)
>>> X_new.shape
(100, 3947)
>>> # very few components are non-zero
>>> np.mean(transformer.components_ != 0)
0.0100...
 Methods
fit(X[, y]) Generate a sparse random projection matrix.
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) Project the data by using matrix product with the random matrix
fit(X, y=None) [source]

Generate a sparse random projection matrix.  Parameters

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

Training set: only the shape is used to find optimal random matrix dimensions based on the theory referenced in the afore mentioned papers.  y

Ignored    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.

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]

Project the data by using matrix product with the random matrix  Parameters

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

The input data to project into a smaller dimensional space.    Returns

X_new{ndarray, sparse matrix} of shape (n_samples, n_components)

Projected array.

 Examples using sklearn.random_projection.SparseRandomProjection

  Manifold learning on handwritten digits: Locally Linear Embedding, Isomap…

  The Johnson-Lindenstrauss bound for embedding with random projections


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.


class sklearn.semi_supervised.LabelPropagation(kernel='rbf', *, gamma=20, n_neighbors=7, max_iter=1000, tol=0.001, n_jobs=None) [source]

Label Propagation classifier Read more in the User Guide.  Parameters

kernel{‘knn’, ‘rbf’} or callable, default=’rbf’

String identifier for kernel function to use or the kernel function itself. Only ‘rbf’ and ‘knn’ strings are valid inputs. The function passed should take two inputs, each of shape (n_samples, n_features), and return a (n_samples, n_samples) shaped weight matrix.
gammafloat, default=20

Parameter for rbf kernel.
n_neighborsint, default=7

Parameter for knn kernel which need to be strictly positive.
max_iterint, default=1000

Change maximum number of iterations allowed.
tolfloat, 1e-3

Convergence tolerance: threshold to consider the system at steady state.
n_jobsint, default=None

The number of parallel jobs to run. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.    Attributes

X_ndarray of shape (n_samples, n_features)

Input array.
classes_ndarray of shape (n_classes,)

The distinct labels used in classifying instances.
label_distributions_ndarray of shape (n_samples, n_classes)

Categorical distribution for each item.
transduction_ndarray of shape (n_samples)

Label assigned to each item via the transduction.
n_iter_int

Number of iterations run.      See also
LabelSpreading

Alternate label propagation strategy more robust to noise.    References Xiaojin Zhu and Zoubin Ghahramani. Learning from labeled and unlabeled data with label propagation. Technical Report CMU-CALD-02-107, Carnegie Mellon University, 2002 http://pages.cs.wisc.edu/~jerryzhu/pub/CMU-CALD-02-107.pdf Examples >>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.semi_supervised import LabelPropagation
>>> label_prop_model = LabelPropagation()
>>> iris = datasets.load_iris()
>>> rng = np.random.RandomState(42)
>>> random_unlabeled_points = rng.rand(len(iris.target)) < 0.3
>>> labels = np.copy(iris.target)
>>> labels[random_unlabeled_points] = -1
>>> label_prop_model.fit(iris.data, labels)
LabelPropagation(...)
 Methods
fit(X, y) Fit a semi-supervised label propagation model based
get_params([deep]) Get parameters for this estimator.
predict(X) Performs inductive inference across the model.
predict_proba(X) Predict probability for each possible outcome.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
fit(X, y) [source]

Fit a semi-supervised label propagation model based All the input data is provided matrix X (labeled and unlabeled) and corresponding label matrix y with a dedicated marker value for unlabeled samples.  Parameters

Xarray-like of shape (n_samples, n_features)

A matrix of shape (n_samples, n_samples) will be created from this.
yarray-like of shape (n_samples,)

n_labeled_samples (unlabeled points are marked as -1) All unlabeled samples will be transductively assigned labels.    Returns

selfobject

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.

predict(X) [source]

Performs inductive inference across the model.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

yndarray of shape (n_samples,)

Predictions for input data.

predict_proba(X) [source]

Predict probability for each possible outcome. Compute the probability estimates for each single sample in X and each possible outcome seen during training (categorical distribution).  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

probabilitiesndarray of shape (n_samples, n_classes)

Normalized probability distributions across class labels.

score(X, y, sample_weight=None) [source]

Return the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) wrt. y.

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.


sklearn.semi_supervised.LabelPropagation
class sklearn.semi_supervised.LabelPropagation(kernel='rbf', *, gamma=20, n_neighbors=7, max_iter=1000, tol=0.001, n_jobs=None) [source]

Label Propagation classifier Read more in the User Guide.  Parameters

kernel{‘knn’, ‘rbf’} or callable, default=’rbf’

String identifier for kernel function to use or the kernel function itself. Only ‘rbf’ and ‘knn’ strings are valid inputs. The function passed should take two inputs, each of shape (n_samples, n_features), and return a (n_samples, n_samples) shaped weight matrix.
gammafloat, default=20

Parameter for rbf kernel.
n_neighborsint, default=7

Parameter for knn kernel which need to be strictly positive.
max_iterint, default=1000

Change maximum number of iterations allowed.
tolfloat, 1e-3

Convergence tolerance: threshold to consider the system at steady state.
n_jobsint, default=None

The number of parallel jobs to run. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.    Attributes

X_ndarray of shape (n_samples, n_features)

Input array.
classes_ndarray of shape (n_classes,)

The distinct labels used in classifying instances.
label_distributions_ndarray of shape (n_samples, n_classes)

Categorical distribution for each item.
transduction_ndarray of shape (n_samples)

Label assigned to each item via the transduction.
n_iter_int

Number of iterations run.      See also
LabelSpreading

Alternate label propagation strategy more robust to noise.    References Xiaojin Zhu and Zoubin Ghahramani. Learning from labeled and unlabeled data with label propagation. Technical Report CMU-CALD-02-107, Carnegie Mellon University, 2002 http://pages.cs.wisc.edu/~jerryzhu/pub/CMU-CALD-02-107.pdf Examples >>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.semi_supervised import LabelPropagation
>>> label_prop_model = LabelPropagation()
>>> iris = datasets.load_iris()
>>> rng = np.random.RandomState(42)
>>> random_unlabeled_points = rng.rand(len(iris.target)) < 0.3
>>> labels = np.copy(iris.target)
>>> labels[random_unlabeled_points] = -1
>>> label_prop_model.fit(iris.data, labels)
LabelPropagation(...)
 Methods
fit(X, y) Fit a semi-supervised label propagation model based
get_params([deep]) Get parameters for this estimator.
predict(X) Performs inductive inference across the model.
predict_proba(X) Predict probability for each possible outcome.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
fit(X, y) [source]

Fit a semi-supervised label propagation model based All the input data is provided matrix X (labeled and unlabeled) and corresponding label matrix y with a dedicated marker value for unlabeled samples.  Parameters

Xarray-like of shape (n_samples, n_features)

A matrix of shape (n_samples, n_samples) will be created from this.
yarray-like of shape (n_samples,)

n_labeled_samples (unlabeled points are marked as -1) All unlabeled samples will be transductively assigned labels.    Returns

selfobject

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.

predict(X) [source]

Performs inductive inference across the model.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

yndarray of shape (n_samples,)

Predictions for input data.

predict_proba(X) [source]

Predict probability for each possible outcome. Compute the probability estimates for each single sample in X and each possible outcome seen during training (categorical distribution).  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

probabilitiesndarray of shape (n_samples, n_classes)

Normalized probability distributions across class labels.

score(X, y, sample_weight=None) [source]

Return the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) wrt. y.

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.


fit(X, y) [source]

Fit a semi-supervised label propagation model based All the input data is provided matrix X (labeled and unlabeled) and corresponding label matrix y with a dedicated marker value for unlabeled samples.  Parameters

Xarray-like of shape (n_samples, n_features)

A matrix of shape (n_samples, n_samples) will be created from this.
yarray-like of shape (n_samples,)

n_labeled_samples (unlabeled points are marked as -1) All unlabeled samples will be transductively assigned labels.    Returns

selfobject


predict(X) [source]

Performs inductive inference across the model.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

yndarray of shape (n_samples,)

Predictions for input data.


predict_proba(X) [source]

Predict probability for each possible outcome. Compute the probability estimates for each single sample in X and each possible outcome seen during training (categorical distribution).  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

probabilitiesndarray of shape (n_samples, n_classes)

Normalized probability distributions across class labels.


score(X, y, sample_weight=None) [source]

Return the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) wrt. y.


class sklearn.semi_supervised.LabelSpreading(kernel='rbf', *, gamma=20, n_neighbors=7, alpha=0.2, max_iter=30, tol=0.001, n_jobs=None) [source]

LabelSpreading model for semi-supervised learning This model is similar to the basic Label Propagation algorithm, but uses affinity matrix based on the normalized graph Laplacian and soft clamping across the labels. Read more in the User Guide.  Parameters

kernel{‘knn’, ‘rbf’} or callable, default=’rbf’

String identifier for kernel function to use or the kernel function itself. Only ‘rbf’ and ‘knn’ strings are valid inputs. The function passed should take two inputs, each of shape (n_samples, n_features), and return a (n_samples, n_samples) shaped weight matrix.
gammafloat, default=20

Parameter for rbf kernel.
n_neighborsint, default=7

Parameter for knn kernel which is a strictly positive integer.
alphafloat, default=0.2

Clamping factor. A value in (0, 1) that specifies the relative amount that an instance should adopt the information from its neighbors as opposed to its initial label. alpha=0 means keeping the initial label information; alpha=1 means replacing all initial information.
max_iterint, default=30

Maximum number of iterations allowed.
tolfloat, default=1e-3

Convergence tolerance: threshold to consider the system at steady state.
n_jobsint, default=None

The number of parallel jobs to run. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.    Attributes

X_ndarray of shape (n_samples, n_features)

Input array.
classes_ndarray of shape (n_classes,)

The distinct labels used in classifying instances.
label_distributions_ndarray of shape (n_samples, n_classes)

Categorical distribution for each item.
transduction_ndarray of shape (n_samples,)

Label assigned to each item via the transduction.
n_iter_int

Number of iterations run.      See also
LabelPropagation

Unregularized graph based semi-supervised learning.    References Dengyong Zhou, Olivier Bousquet, Thomas Navin Lal, Jason Weston, Bernhard Schoelkopf. Learning with local and global consistency (2004) http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.115.3219 Examples >>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.semi_supervised import LabelSpreading
>>> label_prop_model = LabelSpreading()
>>> iris = datasets.load_iris()
>>> rng = np.random.RandomState(42)
>>> random_unlabeled_points = rng.rand(len(iris.target)) < 0.3
>>> labels = np.copy(iris.target)
>>> labels[random_unlabeled_points] = -1
>>> label_prop_model.fit(iris.data, labels)
LabelSpreading(...)
 Methods
fit(X, y) Fit a semi-supervised label propagation model based
get_params([deep]) Get parameters for this estimator.
predict(X) Performs inductive inference across the model.
predict_proba(X) Predict probability for each possible outcome.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
fit(X, y) [source]

Fit a semi-supervised label propagation model based All the input data is provided matrix X (labeled and unlabeled) and corresponding label matrix y with a dedicated marker value for unlabeled samples.  Parameters

Xarray-like of shape (n_samples, n_features)

A matrix of shape (n_samples, n_samples) will be created from this.
yarray-like of shape (n_samples,)

n_labeled_samples (unlabeled points are marked as -1) All unlabeled samples will be transductively assigned labels.    Returns

selfobject

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.

predict(X) [source]

Performs inductive inference across the model.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

yndarray of shape (n_samples,)

Predictions for input data.

predict_proba(X) [source]

Predict probability for each possible outcome. Compute the probability estimates for each single sample in X and each possible outcome seen during training (categorical distribution).  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

probabilitiesndarray of shape (n_samples, n_classes)

Normalized probability distributions across class labels.

score(X, y, sample_weight=None) [source]

Return the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) wrt. y.

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.


sklearn.semi_supervised.LabelSpreading
class sklearn.semi_supervised.LabelSpreading(kernel='rbf', *, gamma=20, n_neighbors=7, alpha=0.2, max_iter=30, tol=0.001, n_jobs=None) [source]

LabelSpreading model for semi-supervised learning This model is similar to the basic Label Propagation algorithm, but uses affinity matrix based on the normalized graph Laplacian and soft clamping across the labels. Read more in the User Guide.  Parameters

kernel{‘knn’, ‘rbf’} or callable, default=’rbf’

String identifier for kernel function to use or the kernel function itself. Only ‘rbf’ and ‘knn’ strings are valid inputs. The function passed should take two inputs, each of shape (n_samples, n_features), and return a (n_samples, n_samples) shaped weight matrix.
gammafloat, default=20

Parameter for rbf kernel.
n_neighborsint, default=7

Parameter for knn kernel which is a strictly positive integer.
alphafloat, default=0.2

Clamping factor. A value in (0, 1) that specifies the relative amount that an instance should adopt the information from its neighbors as opposed to its initial label. alpha=0 means keeping the initial label information; alpha=1 means replacing all initial information.
max_iterint, default=30

Maximum number of iterations allowed.
tolfloat, default=1e-3

Convergence tolerance: threshold to consider the system at steady state.
n_jobsint, default=None

The number of parallel jobs to run. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.    Attributes

X_ndarray of shape (n_samples, n_features)

Input array.
classes_ndarray of shape (n_classes,)

The distinct labels used in classifying instances.
label_distributions_ndarray of shape (n_samples, n_classes)

Categorical distribution for each item.
transduction_ndarray of shape (n_samples,)

Label assigned to each item via the transduction.
n_iter_int

Number of iterations run.      See also
LabelPropagation

Unregularized graph based semi-supervised learning.    References Dengyong Zhou, Olivier Bousquet, Thomas Navin Lal, Jason Weston, Bernhard Schoelkopf. Learning with local and global consistency (2004) http://citeseer.ist.psu.edu/viewdoc/summary?doi=10.1.1.115.3219 Examples >>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.semi_supervised import LabelSpreading
>>> label_prop_model = LabelSpreading()
>>> iris = datasets.load_iris()
>>> rng = np.random.RandomState(42)
>>> random_unlabeled_points = rng.rand(len(iris.target)) < 0.3
>>> labels = np.copy(iris.target)
>>> labels[random_unlabeled_points] = -1
>>> label_prop_model.fit(iris.data, labels)
LabelSpreading(...)
 Methods
fit(X, y) Fit a semi-supervised label propagation model based
get_params([deep]) Get parameters for this estimator.
predict(X) Performs inductive inference across the model.
predict_proba(X) Predict probability for each possible outcome.
score(X, y[, sample_weight]) Return the mean accuracy on the given test data and labels.
set_params(**params) Set the parameters of this estimator.
fit(X, y) [source]

Fit a semi-supervised label propagation model based All the input data is provided matrix X (labeled and unlabeled) and corresponding label matrix y with a dedicated marker value for unlabeled samples.  Parameters

Xarray-like of shape (n_samples, n_features)

A matrix of shape (n_samples, n_samples) will be created from this.
yarray-like of shape (n_samples,)

n_labeled_samples (unlabeled points are marked as -1) All unlabeled samples will be transductively assigned labels.    Returns

selfobject

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.

predict(X) [source]

Performs inductive inference across the model.  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

yndarray of shape (n_samples,)

Predictions for input data.

predict_proba(X) [source]

Predict probability for each possible outcome. Compute the probability estimates for each single sample in X and each possible outcome seen during training (categorical distribution).  Parameters

Xarray-like of shape (n_samples, n_features)

The data matrix.    Returns

probabilitiesndarray of shape (n_samples, n_classes)

Normalized probability distributions across class labels.

score(X, y, sample_weight=None) [source]

Return the mean accuracy on the given test data and labels. In multi-label classification, this is the subset accuracy which is a harsh metric since you require for each sample that each label set be correctly predicted.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

True labels for X.
sample_weightarray-like of shape (n_samples,), default=None

Sample weights.    Returns

scorefloat

Mean accuracy of self.predict(X) wrt. y.

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.

 Examples using sklearn.semi_supervised.LabelSpreading

  Label Propagation learning a complex structure

  Label Propagation digits: Demonstrating performance

  Decision boundary of semi-supervised classifiers versus SVM on the Iris dataset

  Semi-supervised Classification on a Text Dataset

  Label Propagation digits active learning


class sklearn.semi_supervised.SelfTrainingClassifier(base_estimator, threshold=0.75, criterion='threshold', k_best=10, max_iter=10, verbose=False) [source]

Self-training classifier. This class allows a given supervised classifier to function as a semi-supervised classifier, allowing it to learn from unlabeled data. It does this by iteratively predicting pseudo-labels for the unlabeled data and adding them to the training set. The classifier will continue iterating until either max_iter is reached, or no pseudo-labels were added to the training set in the previous iteration. Read more in the User Guide.  Parameters

base_estimatorestimator object

An estimator object implementing fit and predict_proba. Invoking the fit method will fit a clone of the passed estimator, which will be stored in the base_estimator_ attribute.
criterion{‘threshold’, ‘k_best’}, default=’threshold’

The selection criterion used to select which labels to add to the training set. If ‘threshold’, pseudo-labels with prediction probabilities above threshold are added to the dataset. If ‘k_best’, the k_best pseudo-labels with highest prediction probabilities are added to the dataset. When using the ‘threshold’ criterion, a well calibrated classifier should be used.
thresholdfloat, default=0.75

The decision threshold for use with criterion='threshold'. Should be in [0, 1). When using the ‘threshold’ criterion, a well calibrated classifier should be used.
k_bestint, default=10

The amount of samples to add in each iteration. Only used when criterion is k_best’.
max_iterint or None, default=10

Maximum number of iterations allowed. Should be greater than or equal to 0. If it is None, the classifier will continue to predict labels until no new pseudo-labels are added, or all unlabeled samples have been labeled.  verbose: bool, default=False

Enable verbose output.    Attributes

base_estimator_estimator object

The fitted estimator.
classes_ndarray or list of ndarray of shape (n_classes,)

Class labels for each output. (Taken from the trained base_estimator_).
transduction_ndarray of shape (n_samples,)

The labels used for the final fit of the classifier, including pseudo-labels added during fit.
labeled_iter_ndarray of shape (n_samples,)

The iteration in which each sample was labeled. When a sample has iteration 0, the sample was already labeled in the original dataset. When a sample has iteration -1, the sample was not labeled in any iteration.
n_iter_int

The number of rounds of self-training, that is the number of times the base estimator is fitted on relabeled variants of the training set.
termination_condition_{‘max_iter’, ‘no_change’, ‘all_labeled’}

The reason that fitting was stopped.  ‘max_iter’: n_iter_ reached max_iter. ‘no_change’: no new labels were predicted. ‘all_labeled’: all unlabeled samples were labeled before max_iter was reached.      References David Yarowsky. 1995. Unsupervised word sense disambiguation rivaling supervised methods. In Proceedings of the 33rd annual meeting on Association for Computational Linguistics (ACL ‘95). Association for Computational Linguistics, Stroudsburg, PA, USA, 189-196. DOI: https://doi.org/10.3115/981658.981684 Examples >>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.semi_supervised import SelfTrainingClassifier
>>> from sklearn.svm import SVC
>>> rng = np.random.RandomState(42)
>>> iris = datasets.load_iris()
>>> random_unlabeled_points = rng.rand(iris.target.shape[0]) < 0.3
>>> iris.target[random_unlabeled_points] = -1
>>> svc = SVC(probability=True, gamma="auto")
>>> self_training_model = SelfTrainingClassifier(svc)
>>> self_training_model.fit(iris.data, iris.target)
SelfTrainingClassifier(...)
 Methods
decision_function(X) Calls decision function of the base_estimator.
fit(X, y) Fits this SelfTrainingClassifier to a dataset.
get_params([deep]) Get parameters for this estimator.
predict(X) Predict the classes of X.
predict_log_proba(X) Predict log probability for each possible outcome.
predict_proba(X) Predict probability for each possible outcome.
score(X, y) Calls score on the base_estimator.
set_params(**params) Set the parameters of this estimator.
decision_function(X) [source]

Calls decision function of the base_estimator.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Result of the decision function of the base_estimator.

fit(X, y) [source]

Fits this SelfTrainingClassifier to a dataset.  Parameters

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

Array representing the data.
y{array-like, sparse matrix} of shape (n_samples,)

Array representing the labels. Unlabeled samples should have the label -1.    Returns

selfobject

Returns an instance of self.

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.

predict(X) [source]

Predict the classes of X.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples,)

Array with predicted labels.

predict_log_proba(X) [source]

Predict log probability for each possible outcome.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Array with log prediction probabilities.

predict_proba(X) [source]

Predict probability for each possible outcome.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Array with prediction probabilities.

score(X, y) [source]

Calls score on the base_estimator.  Parameters

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

Array representing the data.
yarray-like of shape (n_samples,)

Array representing the labels.    Returns

scorefloat

Result of calling score on the base_estimator.

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.


sklearn.semi_supervised.SelfTrainingClassifier
class sklearn.semi_supervised.SelfTrainingClassifier(base_estimator, threshold=0.75, criterion='threshold', k_best=10, max_iter=10, verbose=False) [source]

Self-training classifier. This class allows a given supervised classifier to function as a semi-supervised classifier, allowing it to learn from unlabeled data. It does this by iteratively predicting pseudo-labels for the unlabeled data and adding them to the training set. The classifier will continue iterating until either max_iter is reached, or no pseudo-labels were added to the training set in the previous iteration. Read more in the User Guide.  Parameters

base_estimatorestimator object

An estimator object implementing fit and predict_proba. Invoking the fit method will fit a clone of the passed estimator, which will be stored in the base_estimator_ attribute.
criterion{‘threshold’, ‘k_best’}, default=’threshold’

The selection criterion used to select which labels to add to the training set. If ‘threshold’, pseudo-labels with prediction probabilities above threshold are added to the dataset. If ‘k_best’, the k_best pseudo-labels with highest prediction probabilities are added to the dataset. When using the ‘threshold’ criterion, a well calibrated classifier should be used.
thresholdfloat, default=0.75

The decision threshold for use with criterion='threshold'. Should be in [0, 1). When using the ‘threshold’ criterion, a well calibrated classifier should be used.
k_bestint, default=10

The amount of samples to add in each iteration. Only used when criterion is k_best’.
max_iterint or None, default=10

Maximum number of iterations allowed. Should be greater than or equal to 0. If it is None, the classifier will continue to predict labels until no new pseudo-labels are added, or all unlabeled samples have been labeled.  verbose: bool, default=False

Enable verbose output.    Attributes

base_estimator_estimator object

The fitted estimator.
classes_ndarray or list of ndarray of shape (n_classes,)

Class labels for each output. (Taken from the trained base_estimator_).
transduction_ndarray of shape (n_samples,)

The labels used for the final fit of the classifier, including pseudo-labels added during fit.
labeled_iter_ndarray of shape (n_samples,)

The iteration in which each sample was labeled. When a sample has iteration 0, the sample was already labeled in the original dataset. When a sample has iteration -1, the sample was not labeled in any iteration.
n_iter_int

The number of rounds of self-training, that is the number of times the base estimator is fitted on relabeled variants of the training set.
termination_condition_{‘max_iter’, ‘no_change’, ‘all_labeled’}

The reason that fitting was stopped.  ‘max_iter’: n_iter_ reached max_iter. ‘no_change’: no new labels were predicted. ‘all_labeled’: all unlabeled samples were labeled before max_iter was reached.      References David Yarowsky. 1995. Unsupervised word sense disambiguation rivaling supervised methods. In Proceedings of the 33rd annual meeting on Association for Computational Linguistics (ACL ‘95). Association for Computational Linguistics, Stroudsburg, PA, USA, 189-196. DOI: https://doi.org/10.3115/981658.981684 Examples >>> import numpy as np
>>> from sklearn import datasets
>>> from sklearn.semi_supervised import SelfTrainingClassifier
>>> from sklearn.svm import SVC
>>> rng = np.random.RandomState(42)
>>> iris = datasets.load_iris()
>>> random_unlabeled_points = rng.rand(iris.target.shape[0]) < 0.3
>>> iris.target[random_unlabeled_points] = -1
>>> svc = SVC(probability=True, gamma="auto")
>>> self_training_model = SelfTrainingClassifier(svc)
>>> self_training_model.fit(iris.data, iris.target)
SelfTrainingClassifier(...)
 Methods
decision_function(X) Calls decision function of the base_estimator.
fit(X, y) Fits this SelfTrainingClassifier to a dataset.
get_params([deep]) Get parameters for this estimator.
predict(X) Predict the classes of X.
predict_log_proba(X) Predict log probability for each possible outcome.
predict_proba(X) Predict probability for each possible outcome.
score(X, y) Calls score on the base_estimator.
set_params(**params) Set the parameters of this estimator.
decision_function(X) [source]

Calls decision function of the base_estimator.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Result of the decision function of the base_estimator.

fit(X, y) [source]

Fits this SelfTrainingClassifier to a dataset.  Parameters

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

Array representing the data.
y{array-like, sparse matrix} of shape (n_samples,)

Array representing the labels. Unlabeled samples should have the label -1.    Returns

selfobject

Returns an instance of self.

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.

predict(X) [source]

Predict the classes of X.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples,)

Array with predicted labels.

predict_log_proba(X) [source]

Predict log probability for each possible outcome.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Array with log prediction probabilities.

predict_proba(X) [source]

Predict probability for each possible outcome.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Array with prediction probabilities.

score(X, y) [source]

Calls score on the base_estimator.  Parameters

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

Array representing the data.
yarray-like of shape (n_samples,)

Array representing the labels.    Returns

scorefloat

Result of calling score on the base_estimator.

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.

 Examples using sklearn.semi_supervised.SelfTrainingClassifier

  Release Highlights for scikit-learn 0.24

  Decision boundary of semi-supervised classifiers versus SVM on the Iris dataset

  Effect of varying threshold for self-training

  Semi-supervised Classification on a Text Dataset


decision_function(X) [source]

Calls decision function of the base_estimator.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Result of the decision function of the base_estimator.


fit(X, y) [source]

Fits this SelfTrainingClassifier to a dataset.  Parameters

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

Array representing the data.
y{array-like, sparse matrix} of shape (n_samples,)

Array representing the labels. Unlabeled samples should have the label -1.    Returns

selfobject

Returns an instance of self.


predict(X) [source]

Predict the classes of X.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples,)

Array with predicted labels.


predict_log_proba(X) [source]

Predict log probability for each possible outcome.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Array with log prediction probabilities.


predict_proba(X) [source]

Predict probability for each possible outcome.  Parameters

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

Array representing the data.    Returns

yndarray of shape (n_samples, n_features)

Array with prediction probabilities.


score(X, y) [source]

Calls score on the base_estimator.  Parameters

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

Array representing the data.
yarray-like of shape (n_samples,)

Array representing the labels.    Returns

scorefloat

Result of calling score on the base_estimator.


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.


sklearn.set_config(assume_finite=None, working_memory=None, print_changed_only=None, display=None) [source]

Set global scikit-learn configuration  New in version 0.19.   Parameters

assume_finitebool, default=None

If True, validation for finiteness will be skipped, saving time, but leading to potential crashes. If False, validation for finiteness will be performed, avoiding error. Global default: False.  New in version 0.19.
working_memoryint, default=None

If set, scikit-learn will attempt to limit the size of temporary arrays to this number of MiB (per job when parallelised), often saving both computation time and memory on expensive operations that can be performed in chunks. Global default: 1024.  New in version 0.20.
print_changed_onlybool, default=None

If True, only the parameters that were set to non-default values will be printed when printing an estimator. For example, print(SVC()) while True will only print ‘SVC()’ while the default behaviour would be to print ‘SVC(C=1.0, cache_size=200, …)’ with all the non-changed parameters.  New in version 0.21.
display{‘text’, ‘diagram’}, default=None

If ‘diagram’, estimators will be displayed as a diagram in a Jupyter lab or notebook context. If ‘text’, estimators will be displayed as text. Default is ‘text’.  New in version 0.23.       See also
config_context

Context manager for global scikit-learn configuration.
get_config

Retrieve current values of the global configuration.


sklearn.show_versions() [source]

Print useful debugging information”  New in version 0.20.


sklearn.base.clone
sklearn.base.clone(estimator, *, safe=True) [source]

Constructs a new unfitted estimator with the same parameters. Clone does a deep copy of the model in an estimator without actually copying attached data. It yields a new estimator with the same parameters that has not been fitted on any data. If the estimator’s random_state parameter is an integer (or if the estimator doesn’t have a random_state parameter), an exact clone is returned: the clone and the original estimator will give the exact same results. Otherwise, statistical clone is returned: the clone might yield different results from the original estimator. More details can be found in Controlling randomness.  Parameters

estimator{list, tuple, set} of estimator instance or a single estimator instance

The estimator or group of estimators to be cloned.
safebool, default=True

If safe is False, clone will fall back to a deep copy on objects that are not estimators.


sklearn.base.is_classifier
sklearn.base.is_classifier(estimator) [source]

Return True if the given estimator is (probably) a classifier.  Parameters

estimatorobject

Estimator object to test.    Returns

outbool

True if estimator is a classifier and False otherwise.


sklearn.base.is_regressor
sklearn.base.is_regressor(estimator) [source]

Return True if the given estimator is (probably) a regressor.  Parameters

estimatorestimator instance

Estimator object to test.    Returns

outbool

True if estimator is a regressor and False otherwise.


sklearn.calibration.calibration_curve
sklearn.calibration.calibration_curve(y_true, y_prob, *, normalize=False, n_bins=5, strategy='uniform') [source]

Compute true and predicted probabilities for a calibration curve. The method assumes the inputs come from a binary classifier, and discretize the [0, 1] interval into bins. Calibration curves may also be referred to as reliability diagrams. Read more in the User Guide.  Parameters

y_truearray-like of shape (n_samples,)

True targets.
y_probarray-like of shape (n_samples,)

Probabilities of the positive class.
normalizebool, default=False

Whether y_prob needs to be normalized into the [0, 1] interval, i.e. is not a proper probability. If True, the smallest value in y_prob is linearly mapped onto 0 and the largest one onto 1.
n_binsint, default=5

Number of bins to discretize the [0, 1] interval. A bigger number requires more data. Bins with no samples (i.e. without corresponding values in y_prob) will not be returned, thus the returned arrays may have less than n_bins values.
strategy{‘uniform’, ‘quantile’}, default=’uniform’

Strategy used to define the widths of the bins.  uniform

The bins have identical widths.  quantile

The bins have the same number of samples and depend on y_prob.      Returns

prob_truendarray of shape (n_bins,) or smaller

The proportion of samples whose class is the positive class, in each bin (fraction of positives).
prob_predndarray of shape (n_bins,) or smaller

The mean predicted probability in each bin.     References Alexandru Niculescu-Mizil and Rich Caruana (2005) Predicting Good Probabilities With Supervised Learning, in Proceedings of the 22nd International Conference on Machine Learning (ICML). See section 4 (Qualitative Analysis of Predictions). Examples >>> import numpy as np
>>> from sklearn.calibration import calibration_curve
>>> y_true = np.array([0, 0, 0, 0, 1, 1, 1, 1, 1])
>>> y_pred = np.array([0.1, 0.2, 0.3, 0.4, 0.65, 0.7, 0.8, 0.9,  1.])
>>> prob_true, prob_pred = calibration_curve(y_true, y_pred, n_bins=3)
>>> prob_true
array([0. , 0.5, 1. ])
>>> prob_pred
array([0.2  , 0.525, 0.85 ])

 Examples using sklearn.calibration.calibration_curve

  Comparison of Calibration of Classifiers

  Probability Calibration curves