sklearn.kernel_approximation.AdditiveChi2Sampler
class sklearn.kernel_approximation.AdditiveChi2Sampler(*, sample_steps=2, sample_interval=None) [source]

Approximate feature map for additive chi2 kernel. Uses sampling the fourier transform of the kernel characteristic at regular intervals. Since the kernel that is to be approximated is additive, the components of the input vectors can be treated separately. Each entry in the original space is transformed into 2*sample_steps+1 features, where sample_steps is a parameter of the method. Typical values of sample_steps include 1, 2 and 3. Optimal choices for the sampling interval for certain data ranges can be computed (see the reference). The default values should be reasonable. Read more in the User Guide.  Parameters

sample_stepsint, default=2

Gives the number of (complex) sampling points.
sample_intervalfloat, default=None

Sampling interval. Must be specified when sample_steps not in {1,2,3}.    Attributes

sample_interval_float

Stored sampling interval. Specified as a parameter if sample_steps not in {1,2,3}.      See also
SkewedChi2Sampler

A Fourier-approximation to a non-additive variant of the chi squared kernel.
sklearn.metrics.pairwise.chi2_kernel

The exact chi squared kernel.
sklearn.metrics.pairwise.additive_chi2_kernel

The exact additive chi squared kernel.    Notes This estimator approximates a slightly different version of the additive chi squared kernel then metric.additive_chi2 computes. References See “Efficient additive kernels via explicit feature maps” A. Vedaldi and A. Zisserman, Pattern Analysis and Machine Intelligence, 2011 Examples >>> from sklearn.datasets import load_digits
>>> from sklearn.linear_model import SGDClassifier
>>> from sklearn.kernel_approximation import AdditiveChi2Sampler
>>> X, y = load_digits(return_X_y=True)
>>> chi2sampler = AdditiveChi2Sampler(sample_steps=2)
>>> X_transformed = chi2sampler.fit_transform(X, y)
>>> clf = SGDClassifier(max_iter=5, random_state=0, tol=1e-3)
>>> clf.fit(X_transformed, y)
SGDClassifier(max_iter=5, random_state=0)
>>> clf.score(X_transformed, y)
0.9499...
 Methods
fit(X[, y]) Set the parameters
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) Apply approximate feature map to X.
fit(X, y=None) [source]

Set the parameters  Parameters

Xarray-like, shape (n_samples, n_features)

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.

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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Apply approximate feature map to X.  Parameters

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

X_new{ndarray, sparse matrix}, shape = (n_samples, n_features * (2*sample_steps + 1))

Whether the return value is an array of sparse matrix depends on the type of the input X.


fit(X, y=None) [source]

Set the parameters  Parameters

Xarray-like, shape (n_samples, n_features)

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.


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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.


set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


transform(X) [source]

Apply approximate feature map to X.  Parameters

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

X_new{ndarray, sparse matrix}, shape = (n_samples, n_features * (2*sample_steps + 1))

Whether the return value is an array of sparse matrix depends on the type of the input X.


class sklearn.kernel_approximation.Nystroem(kernel='rbf', *, gamma=None, coef0=None, degree=None, kernel_params=None, n_components=100, random_state=None, n_jobs=None) [source]

Approximate a kernel map using a subset of the training data. Constructs an approximate feature map for an arbitrary kernel using a subset of the data as basis. Read more in the User Guide.  New in version 0.13.   Parameters

kernelstring or callable, default=’rbf’

Kernel map to be approximated. A callable should accept two arguments and the keyword arguments passed to this object as kernel_params, and should return a floating point number.
gammafloat, default=None

Gamma parameter for the RBF, laplacian, polynomial, exponential chi2 and sigmoid kernels. Interpretation of the default value is left to the kernel; see the documentation for sklearn.metrics.pairwise. Ignored by other kernels.
coef0float, default=None

Zero coefficient for polynomial and sigmoid kernels. Ignored by other kernels.
degreefloat, default=None

Degree of the polynomial kernel. Ignored by other kernels.
kernel_paramsdict, default=None

Additional parameters (keyword arguments) for kernel function passed as callable object.
n_componentsint, default=100

Number of features to construct. How many data points will be used to construct the mapping.
random_stateint, RandomState instance or None, default=None

Pseudo-random number generator to control the uniform sampling without replacement of n_components of the training data to construct the basis kernel. Pass an int for reproducible output across multiple function calls. See Glossary.
n_jobsint, default=None

The number of jobs to use for the computation. This works by breaking down the kernel matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  New in version 0.24.     Attributes

components_ndarray of shape (n_components, n_features)

Subset of training points used to construct the feature map.
component_indices_ndarray of shape (n_components)

Indices of components_ in the training set.
normalization_ndarray of shape (n_components, n_components)

Normalization matrix needed for embedding. Square root of the kernel matrix on components_.      See also
RBFSampler

An approximation to the RBF kernel using random Fourier features.
sklearn.metrics.pairwise.kernel_metrics

List of built-in kernels.    References  Williams, C.K.I. and Seeger, M. “Using the Nystroem method to speed up kernel machines”, Advances in neural information processing systems 2001 T. Yang, Y. Li, M. Mahdavi, R. Jin and Z. Zhou “Nystroem Method vs Random Fourier Features: A Theoretical and Empirical Comparison”, Advances in Neural Information Processing Systems 2012  Examples >>> from sklearn import datasets, svm
>>> from sklearn.kernel_approximation import Nystroem
>>> X, y = datasets.load_digits(n_class=9, return_X_y=True)
>>> data = X / 16.
>>> clf = svm.LinearSVC()
>>> feature_map_nystroem = Nystroem(gamma=.2,
...                                 random_state=1,
...                                 n_components=300)
>>> data_transformed = feature_map_nystroem.fit_transform(data)
>>> clf.fit(data_transformed, y)
LinearSVC()
>>> clf.score(data_transformed, y)
0.9987...
 Methods
fit(X[, y]) Fit estimator to data.
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) Apply feature map to X.
fit(X, y=None) [source]

Fit estimator to data. Samples a subset of training points, computes kernel on these and computes normalization matrix.  Parameters

Xarray-like of shape (n_samples, n_features)

Training data.

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]

Apply feature map to X. Computes an approximate feature map using the kernel between some training points and X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data to transform.    Returns

X_transformedndarray of shape (n_samples, n_components)

Transformed data.


sklearn.kernel_approximation.Nystroem
class sklearn.kernel_approximation.Nystroem(kernel='rbf', *, gamma=None, coef0=None, degree=None, kernel_params=None, n_components=100, random_state=None, n_jobs=None) [source]

Approximate a kernel map using a subset of the training data. Constructs an approximate feature map for an arbitrary kernel using a subset of the data as basis. Read more in the User Guide.  New in version 0.13.   Parameters

kernelstring or callable, default=’rbf’

Kernel map to be approximated. A callable should accept two arguments and the keyword arguments passed to this object as kernel_params, and should return a floating point number.
gammafloat, default=None

Gamma parameter for the RBF, laplacian, polynomial, exponential chi2 and sigmoid kernels. Interpretation of the default value is left to the kernel; see the documentation for sklearn.metrics.pairwise. Ignored by other kernels.
coef0float, default=None

Zero coefficient for polynomial and sigmoid kernels. Ignored by other kernels.
degreefloat, default=None

Degree of the polynomial kernel. Ignored by other kernels.
kernel_paramsdict, default=None

Additional parameters (keyword arguments) for kernel function passed as callable object.
n_componentsint, default=100

Number of features to construct. How many data points will be used to construct the mapping.
random_stateint, RandomState instance or None, default=None

Pseudo-random number generator to control the uniform sampling without replacement of n_components of the training data to construct the basis kernel. Pass an int for reproducible output across multiple function calls. See Glossary.
n_jobsint, default=None

The number of jobs to use for the computation. This works by breaking down the kernel matrix into n_jobs even slices and computing them in parallel. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. See Glossary for more details.  New in version 0.24.     Attributes

components_ndarray of shape (n_components, n_features)

Subset of training points used to construct the feature map.
component_indices_ndarray of shape (n_components)

Indices of components_ in the training set.
normalization_ndarray of shape (n_components, n_components)

Normalization matrix needed for embedding. Square root of the kernel matrix on components_.      See also
RBFSampler

An approximation to the RBF kernel using random Fourier features.
sklearn.metrics.pairwise.kernel_metrics

List of built-in kernels.    References  Williams, C.K.I. and Seeger, M. “Using the Nystroem method to speed up kernel machines”, Advances in neural information processing systems 2001 T. Yang, Y. Li, M. Mahdavi, R. Jin and Z. Zhou “Nystroem Method vs Random Fourier Features: A Theoretical and Empirical Comparison”, Advances in Neural Information Processing Systems 2012  Examples >>> from sklearn import datasets, svm
>>> from sklearn.kernel_approximation import Nystroem
>>> X, y = datasets.load_digits(n_class=9, return_X_y=True)
>>> data = X / 16.
>>> clf = svm.LinearSVC()
>>> feature_map_nystroem = Nystroem(gamma=.2,
...                                 random_state=1,
...                                 n_components=300)
>>> data_transformed = feature_map_nystroem.fit_transform(data)
>>> clf.fit(data_transformed, y)
LinearSVC()
>>> clf.score(data_transformed, y)
0.9987...
 Methods
fit(X[, y]) Fit estimator to data.
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) Apply feature map to X.
fit(X, y=None) [source]

Fit estimator to data. Samples a subset of training points, computes kernel on these and computes normalization matrix.  Parameters

Xarray-like of shape (n_samples, n_features)

Training data.

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]

Apply feature map to X. Computes an approximate feature map using the kernel between some training points and X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data to transform.    Returns

X_transformedndarray of shape (n_samples, n_components)

Transformed data.

 Examples using sklearn.kernel_approximation.Nystroem

  Explicit feature map approximation for RBF kernels


fit(X, y=None) [source]

Fit estimator to data. Samples a subset of training points, computes kernel on these and computes normalization matrix.  Parameters

Xarray-like of shape (n_samples, n_features)

Training data.


transform(X) [source]

Apply feature map to X. Computes an approximate feature map using the kernel between some training points and X.  Parameters

Xarray-like of shape (n_samples, n_features)

Data to transform.    Returns

X_transformedndarray of shape (n_samples, n_components)

Transformed data.


class sklearn.kernel_approximation.PolynomialCountSketch(*, gamma=1.0, degree=2, coef0=0, n_components=100, random_state=None) [source]

Polynomial kernel approximation via Tensor Sketch. Implements Tensor Sketch, which approximates the feature map of the polynomial kernel: K(X, Y) = (gamma * <X, Y> + coef0)^degree
 by efficiently computing a Count Sketch of the outer product of a vector with itself using Fast Fourier Transforms (FFT). Read more in the User Guide.  New in version 0.24.   Parameters

gammafloat, default=1.0

Parameter of the polynomial kernel whose feature map will be approximated.
degreeint, default=2

Degree of the polynomial kernel whose feature map will be approximated.
coef0int, default=0

Constant term of the polynomial kernel whose feature map will be approximated.
n_componentsint, default=100

Dimensionality of the output feature space. Usually, n_components should be greater than the number of features in input samples in order to achieve good performance. The optimal score / run time balance is typically achieved around n_components = 10 * n_features, but this depends on the specific dataset being used.
random_stateint, RandomState instance, default=None

Determines random number generation for indexHash and bitHash initialization. Pass an int for reproducible results across multiple function calls. See Glossary.    Attributes

indexHash_ndarray of shape (degree, n_features), dtype=int64

Array of indexes in range [0, n_components) used to represent the 2-wise independent hash functions for Count Sketch computation.
bitHash_ndarray of shape (degree, n_features), dtype=float32

Array with random entries in {+1, -1}, used to represent the 2-wise independent hash functions for Count Sketch computation.     Examples >>> from sklearn.kernel_approximation import PolynomialCountSketch
>>> from sklearn.linear_model import SGDClassifier
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
>>> y = [0, 0, 1, 1]
>>> ps = PolynomialCountSketch(degree=3, random_state=1)
>>> X_features = ps.fit_transform(X)
>>> clf = SGDClassifier(max_iter=10, tol=1e-3)
>>> clf.fit(X_features, y)
SGDClassifier(max_iter=10)
>>> clf.score(X_features, y)
1.0
 Methods
fit(X[, y]) Fit the model with X.
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) Generate the feature map approximation for X.
fit(X, y=None) [source]

Fit the model with X. Initializes the internal variables. The method needs no information about the distribution of data, so we only care about n_features in X.  Parameters

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

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.

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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Generate the feature map approximation for X.  Parameters

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

New data, where n_samples in the number of samples and n_features is the number of features.    Returns

X_newarray-like, shape (n_samples, n_components)


sklearn.kernel_approximation.PolynomialCountSketch
class sklearn.kernel_approximation.PolynomialCountSketch(*, gamma=1.0, degree=2, coef0=0, n_components=100, random_state=None) [source]

Polynomial kernel approximation via Tensor Sketch. Implements Tensor Sketch, which approximates the feature map of the polynomial kernel: K(X, Y) = (gamma * <X, Y> + coef0)^degree
 by efficiently computing a Count Sketch of the outer product of a vector with itself using Fast Fourier Transforms (FFT). Read more in the User Guide.  New in version 0.24.   Parameters

gammafloat, default=1.0

Parameter of the polynomial kernel whose feature map will be approximated.
degreeint, default=2

Degree of the polynomial kernel whose feature map will be approximated.
coef0int, default=0

Constant term of the polynomial kernel whose feature map will be approximated.
n_componentsint, default=100

Dimensionality of the output feature space. Usually, n_components should be greater than the number of features in input samples in order to achieve good performance. The optimal score / run time balance is typically achieved around n_components = 10 * n_features, but this depends on the specific dataset being used.
random_stateint, RandomState instance, default=None

Determines random number generation for indexHash and bitHash initialization. Pass an int for reproducible results across multiple function calls. See Glossary.    Attributes

indexHash_ndarray of shape (degree, n_features), dtype=int64

Array of indexes in range [0, n_components) used to represent the 2-wise independent hash functions for Count Sketch computation.
bitHash_ndarray of shape (degree, n_features), dtype=float32

Array with random entries in {+1, -1}, used to represent the 2-wise independent hash functions for Count Sketch computation.     Examples >>> from sklearn.kernel_approximation import PolynomialCountSketch
>>> from sklearn.linear_model import SGDClassifier
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
>>> y = [0, 0, 1, 1]
>>> ps = PolynomialCountSketch(degree=3, random_state=1)
>>> X_features = ps.fit_transform(X)
>>> clf = SGDClassifier(max_iter=10, tol=1e-3)
>>> clf.fit(X_features, y)
SGDClassifier(max_iter=10)
>>> clf.score(X_features, y)
1.0
 Methods
fit(X[, y]) Fit the model with X.
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) Generate the feature map approximation for X.
fit(X, y=None) [source]

Fit the model with X. Initializes the internal variables. The method needs no information about the distribution of data, so we only care about n_features in X.  Parameters

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

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.

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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Generate the feature map approximation for X.  Parameters

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

New data, where n_samples in the number of samples and n_features is the number of features.    Returns

X_newarray-like, shape (n_samples, n_components)

 Examples using sklearn.kernel_approximation.PolynomialCountSketch

  Release Highlights for scikit-learn 0.24

  Scalable learning with polynomial kernel aproximation


fit(X, y=None) [source]

Fit the model with X. Initializes the internal variables. The method needs no information about the distribution of data, so we only care about n_features in X.  Parameters

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

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.


transform(X) [source]

Generate the feature map approximation for X.  Parameters

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

New data, where n_samples in the number of samples and n_features is the number of features.    Returns

X_newarray-like, shape (n_samples, n_components)


class sklearn.kernel_approximation.RBFSampler(*, gamma=1.0, n_components=100, random_state=None) [source]

Approximates feature map of an RBF kernel by Monte Carlo approximation of its Fourier transform. It implements a variant of Random Kitchen Sinks.[1] Read more in the User Guide.  Parameters

gammafloat, default=1.0

Parameter of RBF kernel: exp(-gamma * x^2)
n_componentsint, default=100

Number of Monte Carlo samples per original feature. Equals the dimensionality of the computed feature space.
random_stateint, RandomState instance or None, default=None

Pseudo-random number generator to control the generation of the random weights and random offset when fitting the training data. Pass an int for reproducible output across multiple function calls. See Glossary.    Attributes

random_offset_ndarray of shape (n_components,), dtype=float64

Random offset used to compute the projection in the n_components dimensions of the feature space.
random_weights_ndarray of shape (n_features, n_components), dtype=float64

Random projection directions drawn from the Fourier transform of the RBF kernel.     Notes See “Random Features for Large-Scale Kernel Machines” by A. Rahimi and Benjamin Recht. [1] “Weighted Sums of Random Kitchen Sinks: Replacing minimization with randomization in learning” by A. Rahimi and Benjamin Recht. (https://people.eecs.berkeley.edu/~brecht/papers/08.rah.rec.nips.pdf) Examples >>> from sklearn.kernel_approximation import RBFSampler
>>> from sklearn.linear_model import SGDClassifier
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
>>> y = [0, 0, 1, 1]
>>> rbf_feature = RBFSampler(gamma=1, random_state=1)
>>> X_features = rbf_feature.fit_transform(X)
>>> clf = SGDClassifier(max_iter=5, tol=1e-3)
>>> clf.fit(X_features, y)
SGDClassifier(max_iter=5)
>>> clf.score(X_features, y)
1.0
 Methods
fit(X[, y]) Fit the model with X.
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) Apply the approximate feature map to X.
fit(X, y=None) [source]

Fit the model with X. Samples random projection according to n_features.  Parameters

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

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.

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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Apply the approximate feature map to X.  Parameters

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

New data, where n_samples in the number of samples and n_features is the number of features.    Returns

X_newarray-like, shape (n_samples, n_components)


sklearn.kernel_approximation.RBFSampler
class sklearn.kernel_approximation.RBFSampler(*, gamma=1.0, n_components=100, random_state=None) [source]

Approximates feature map of an RBF kernel by Monte Carlo approximation of its Fourier transform. It implements a variant of Random Kitchen Sinks.[1] Read more in the User Guide.  Parameters

gammafloat, default=1.0

Parameter of RBF kernel: exp(-gamma * x^2)
n_componentsint, default=100

Number of Monte Carlo samples per original feature. Equals the dimensionality of the computed feature space.
random_stateint, RandomState instance or None, default=None

Pseudo-random number generator to control the generation of the random weights and random offset when fitting the training data. Pass an int for reproducible output across multiple function calls. See Glossary.    Attributes

random_offset_ndarray of shape (n_components,), dtype=float64

Random offset used to compute the projection in the n_components dimensions of the feature space.
random_weights_ndarray of shape (n_features, n_components), dtype=float64

Random projection directions drawn from the Fourier transform of the RBF kernel.     Notes See “Random Features for Large-Scale Kernel Machines” by A. Rahimi and Benjamin Recht. [1] “Weighted Sums of Random Kitchen Sinks: Replacing minimization with randomization in learning” by A. Rahimi and Benjamin Recht. (https://people.eecs.berkeley.edu/~brecht/papers/08.rah.rec.nips.pdf) Examples >>> from sklearn.kernel_approximation import RBFSampler
>>> from sklearn.linear_model import SGDClassifier
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
>>> y = [0, 0, 1, 1]
>>> rbf_feature = RBFSampler(gamma=1, random_state=1)
>>> X_features = rbf_feature.fit_transform(X)
>>> clf = SGDClassifier(max_iter=5, tol=1e-3)
>>> clf.fit(X_features, y)
SGDClassifier(max_iter=5)
>>> clf.score(X_features, y)
1.0
 Methods
fit(X[, y]) Fit the model with X.
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) Apply the approximate feature map to X.
fit(X, y=None) [source]

Fit the model with X. Samples random projection according to n_features.  Parameters

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

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.

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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Apply the approximate feature map to X.  Parameters

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

New data, where n_samples in the number of samples and n_features is the number of features.    Returns

X_newarray-like, shape (n_samples, n_components)

 Examples using sklearn.kernel_approximation.RBFSampler

  Explicit feature map approximation for RBF kernels


fit(X, y=None) [source]

Fit the model with X. Samples random projection according to n_features.  Parameters

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

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.


transform(X) [source]

Apply the approximate feature map to X.  Parameters

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

New data, where n_samples in the number of samples and n_features is the number of features.    Returns

X_newarray-like, shape (n_samples, n_components)


class sklearn.kernel_approximation.SkewedChi2Sampler(*, skewedness=1.0, n_components=100, random_state=None) [source]

Approximates feature map of the “skewed chi-squared” kernel by Monte Carlo approximation of its Fourier transform. Read more in the User Guide.  Parameters

skewednessfloat, default=1.0

“skewedness” parameter of the kernel. Needs to be cross-validated.
n_componentsint, default=100

number of Monte Carlo samples per original feature. Equals the dimensionality of the computed feature space.
random_stateint, RandomState instance or None, default=None

Pseudo-random number generator to control the generation of the random weights and random offset when fitting the training data. Pass an int for reproducible output across multiple function calls. See Glossary.    Attributes

random_weights_ndarray of shape (n_features, n_components)

Weight array, sampled from a secant hyperbolic distribution, which will be used to linearly transform the log of the data.
random_offset_ndarray of shape (n_features, n_components)

Bias term, which will be added to the data. It is uniformly distributed between 0 and 2*pi.      See also
AdditiveChi2Sampler

A different approach for approximating an additive variant of the chi squared kernel.
sklearn.metrics.pairwise.chi2_kernel

The exact chi squared kernel.    References See “Random Fourier Approximations for Skewed Multiplicative Histogram Kernels” by Fuxin Li, Catalin Ionescu and Cristian Sminchisescu. Examples >>> from sklearn.kernel_approximation import SkewedChi2Sampler
>>> from sklearn.linear_model import SGDClassifier
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
>>> y = [0, 0, 1, 1]
>>> chi2_feature = SkewedChi2Sampler(skewedness=.01,
...                                  n_components=10,
...                                  random_state=0)
>>> X_features = chi2_feature.fit_transform(X, y)
>>> clf = SGDClassifier(max_iter=10, tol=1e-3)
>>> clf.fit(X_features, y)
SGDClassifier(max_iter=10)
>>> clf.score(X_features, y)
1.0
 Methods
fit(X[, y]) Fit the model with X.
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) Apply the approximate feature map to X.
fit(X, y=None) [source]

Fit the model with X. Samples random projection according to n_features.  Parameters

Xarray-like, shape (n_samples, n_features)

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.

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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Apply the approximate feature map to X.  Parameters

Xarray-like, shape (n_samples, n_features)

New data, where n_samples in the number of samples and n_features is the number of features. All values of X must be strictly greater than “-skewedness”.    Returns

X_newarray-like, shape (n_samples, n_components)


sklearn.kernel_approximation.SkewedChi2Sampler
class sklearn.kernel_approximation.SkewedChi2Sampler(*, skewedness=1.0, n_components=100, random_state=None) [source]

Approximates feature map of the “skewed chi-squared” kernel by Monte Carlo approximation of its Fourier transform. Read more in the User Guide.  Parameters

skewednessfloat, default=1.0

“skewedness” parameter of the kernel. Needs to be cross-validated.
n_componentsint, default=100

number of Monte Carlo samples per original feature. Equals the dimensionality of the computed feature space.
random_stateint, RandomState instance or None, default=None

Pseudo-random number generator to control the generation of the random weights and random offset when fitting the training data. Pass an int for reproducible output across multiple function calls. See Glossary.    Attributes

random_weights_ndarray of shape (n_features, n_components)

Weight array, sampled from a secant hyperbolic distribution, which will be used to linearly transform the log of the data.
random_offset_ndarray of shape (n_features, n_components)

Bias term, which will be added to the data. It is uniformly distributed between 0 and 2*pi.      See also
AdditiveChi2Sampler

A different approach for approximating an additive variant of the chi squared kernel.
sklearn.metrics.pairwise.chi2_kernel

The exact chi squared kernel.    References See “Random Fourier Approximations for Skewed Multiplicative Histogram Kernels” by Fuxin Li, Catalin Ionescu and Cristian Sminchisescu. Examples >>> from sklearn.kernel_approximation import SkewedChi2Sampler
>>> from sklearn.linear_model import SGDClassifier
>>> X = [[0, 0], [1, 1], [1, 0], [0, 1]]
>>> y = [0, 0, 1, 1]
>>> chi2_feature = SkewedChi2Sampler(skewedness=.01,
...                                  n_components=10,
...                                  random_state=0)
>>> X_features = chi2_feature.fit_transform(X, y)
>>> clf = SGDClassifier(max_iter=10, tol=1e-3)
>>> clf.fit(X_features, y)
SGDClassifier(max_iter=10)
>>> clf.score(X_features, y)
1.0
 Methods
fit(X[, y]) Fit the model with X.
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) Apply the approximate feature map to X.
fit(X, y=None) [source]

Fit the model with X. Samples random projection according to n_features.  Parameters

Xarray-like, shape (n_samples, n_features)

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.

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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.

set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.

transform(X) [source]

Apply the approximate feature map to X.  Parameters

Xarray-like, shape (n_samples, n_features)

New data, where n_samples in the number of samples and n_features is the number of features. All values of X must be strictly greater than “-skewedness”.    Returns

X_newarray-like, shape (n_samples, n_components)


fit(X, y=None) [source]

Fit the model with X. Samples random projection according to n_features.  Parameters

Xarray-like, shape (n_samples, n_features)

Training data, where n_samples in the number of samples and n_features is the number of features.    Returns

selfobject

Returns the transformer.


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

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

Xarray-like of shape (n_samples, n_features)

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

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

Additional fit parameters.    Returns

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.


get_params(deep=True) [source]

Get parameters for this estimator.  Parameters

deepbool, default=True

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

paramsdict

Parameter names mapped to their values.


set_params(**params) [source]

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

**paramsdict

Estimator parameters.    Returns

selfestimator instance

Estimator instance.


transform(X) [source]

Apply the approximate feature map to X.  Parameters

Xarray-like, shape (n_samples, n_features)

New data, where n_samples in the number of samples and n_features is the number of features. All values of X must be strictly greater than “-skewedness”.    Returns

X_newarray-like, shape (n_samples, n_components)


class sklearn.kernel_ridge.KernelRidge(alpha=1, *, kernel='linear', gamma=None, degree=3, coef0=1, kernel_params=None) [source]

Kernel ridge regression. Kernel ridge regression (KRR) combines ridge regression (linear least squares with l2-norm regularization) with the kernel trick. It thus learns a linear function in the space induced by the respective kernel and the data. For non-linear kernels, this corresponds to a non-linear function in the original space. The form of the model learned by KRR is identical to support vector regression (SVR). However, different loss functions are used: KRR uses squared error loss while support vector regression uses epsilon-insensitive loss, both combined with l2 regularization. In contrast to SVR, fitting a KRR model can be done in closed-form and is typically faster for medium-sized datasets. On the other hand, the learned model is non-sparse and thus slower than SVR, which learns a sparse model for epsilon > 0, at prediction-time. This estimator has built-in support for multi-variate regression (i.e., when y is a 2d-array of shape [n_samples, n_targets]). Read more in the User Guide.  Parameters

alphafloat or array-like of shape (n_targets,), default=1.0

Regularization strength; must be a positive float. Regularization improves the conditioning of the problem and reduces the variance of the estimates. Larger values specify stronger regularization. Alpha corresponds to 1 / (2C) in other linear models such as LogisticRegression or LinearSVC. If an array is passed, penalties are assumed to be specific to the targets. Hence they must correspond in number. See Ridge regression and classification for formula.
kernelstring or callable, default=”linear”

Kernel mapping used internally. This parameter is directly passed to pairwise_kernel. If kernel is a string, it must be one of the metrics in pairwise.PAIRWISE_KERNEL_FUNCTIONS. If kernel is “precomputed”, X is assumed to be a kernel matrix. Alternatively, if kernel is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two rows from X as input and return the corresponding kernel value as a single number. This means that callables from sklearn.metrics.pairwise are not allowed, as they operate on matrices, not single samples. Use the string identifying the kernel instead.
gammafloat, default=None

Gamma parameter for the RBF, laplacian, polynomial, exponential chi2 and sigmoid kernels. Interpretation of the default value is left to the kernel; see the documentation for sklearn.metrics.pairwise. Ignored by other kernels.
degreefloat, default=3

Degree of the polynomial kernel. Ignored by other kernels.
coef0float, default=1

Zero coefficient for polynomial and sigmoid kernels. Ignored by other kernels.
kernel_paramsmapping of string to any, default=None

Additional parameters (keyword arguments) for kernel function passed as callable object.    Attributes

dual_coef_ndarray of shape (n_samples,) or (n_samples, n_targets)

Representation of weight vector(s) in kernel space
X_fit_{ndarray, sparse matrix} of shape (n_samples, n_features)

Training data, which is also required for prediction. If kernel == “precomputed” this is instead the precomputed training matrix, of shape (n_samples, n_samples).      See also
sklearn.linear_model.Ridge

Linear ridge regression.
sklearn.svm.SVR

Support Vector Regression implemented using libsvm.    References  Kevin P. Murphy “Machine Learning: A Probabilistic Perspective”, The MIT Press chapter 14.4.3, pp. 492-493  Examples >>> from sklearn.kernel_ridge import KernelRidge
>>> import numpy as np
>>> n_samples, n_features = 10, 5
>>> rng = np.random.RandomState(0)
>>> y = rng.randn(n_samples)
>>> X = rng.randn(n_samples, n_features)
>>> clf = KernelRidge(alpha=1.0)
>>> clf.fit(X, y)
KernelRidge(alpha=1.0)
 Methods
fit(X, y[, sample_weight]) Fit Kernel Ridge regression model
get_params([deep]) Get parameters for this estimator.
predict(X) Predict using the kernel ridge model
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y, sample_weight=None) [source]

Fit Kernel Ridge regression model  Parameters

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

Training data. If kernel == “precomputed” this is instead a precomputed kernel matrix, of shape (n_samples, n_samples).
yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target values
sample_weightfloat or array-like of shape (n_samples,), default=None

Individual weights for each sample, ignored if None is passed.    Returns

selfreturns 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 using the kernel ridge model  Parameters

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

Samples. If kernel == “precomputed” this is instead a precomputed kernel matrix, shape = [n_samples, n_samples_fitted], where n_samples_fitted is the number of samples used in the fitting for this estimator.    Returns

Cndarray of shape (n_samples,) or (n_samples, n_targets)

Returns predicted values.

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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

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

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

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.kernel_ridge.KernelRidge
class sklearn.kernel_ridge.KernelRidge(alpha=1, *, kernel='linear', gamma=None, degree=3, coef0=1, kernel_params=None) [source]

Kernel ridge regression. Kernel ridge regression (KRR) combines ridge regression (linear least squares with l2-norm regularization) with the kernel trick. It thus learns a linear function in the space induced by the respective kernel and the data. For non-linear kernels, this corresponds to a non-linear function in the original space. The form of the model learned by KRR is identical to support vector regression (SVR). However, different loss functions are used: KRR uses squared error loss while support vector regression uses epsilon-insensitive loss, both combined with l2 regularization. In contrast to SVR, fitting a KRR model can be done in closed-form and is typically faster for medium-sized datasets. On the other hand, the learned model is non-sparse and thus slower than SVR, which learns a sparse model for epsilon > 0, at prediction-time. This estimator has built-in support for multi-variate regression (i.e., when y is a 2d-array of shape [n_samples, n_targets]). Read more in the User Guide.  Parameters

alphafloat or array-like of shape (n_targets,), default=1.0

Regularization strength; must be a positive float. Regularization improves the conditioning of the problem and reduces the variance of the estimates. Larger values specify stronger regularization. Alpha corresponds to 1 / (2C) in other linear models such as LogisticRegression or LinearSVC. If an array is passed, penalties are assumed to be specific to the targets. Hence they must correspond in number. See Ridge regression and classification for formula.
kernelstring or callable, default=”linear”

Kernel mapping used internally. This parameter is directly passed to pairwise_kernel. If kernel is a string, it must be one of the metrics in pairwise.PAIRWISE_KERNEL_FUNCTIONS. If kernel is “precomputed”, X is assumed to be a kernel matrix. Alternatively, if kernel is a callable function, it is called on each pair of instances (rows) and the resulting value recorded. The callable should take two rows from X as input and return the corresponding kernel value as a single number. This means that callables from sklearn.metrics.pairwise are not allowed, as they operate on matrices, not single samples. Use the string identifying the kernel instead.
gammafloat, default=None

Gamma parameter for the RBF, laplacian, polynomial, exponential chi2 and sigmoid kernels. Interpretation of the default value is left to the kernel; see the documentation for sklearn.metrics.pairwise. Ignored by other kernels.
degreefloat, default=3

Degree of the polynomial kernel. Ignored by other kernels.
coef0float, default=1

Zero coefficient for polynomial and sigmoid kernels. Ignored by other kernels.
kernel_paramsmapping of string to any, default=None

Additional parameters (keyword arguments) for kernel function passed as callable object.    Attributes

dual_coef_ndarray of shape (n_samples,) or (n_samples, n_targets)

Representation of weight vector(s) in kernel space
X_fit_{ndarray, sparse matrix} of shape (n_samples, n_features)

Training data, which is also required for prediction. If kernel == “precomputed” this is instead the precomputed training matrix, of shape (n_samples, n_samples).      See also
sklearn.linear_model.Ridge

Linear ridge regression.
sklearn.svm.SVR

Support Vector Regression implemented using libsvm.    References  Kevin P. Murphy “Machine Learning: A Probabilistic Perspective”, The MIT Press chapter 14.4.3, pp. 492-493  Examples >>> from sklearn.kernel_ridge import KernelRidge
>>> import numpy as np
>>> n_samples, n_features = 10, 5
>>> rng = np.random.RandomState(0)
>>> y = rng.randn(n_samples)
>>> X = rng.randn(n_samples, n_features)
>>> clf = KernelRidge(alpha=1.0)
>>> clf.fit(X, y)
KernelRidge(alpha=1.0)
 Methods
fit(X, y[, sample_weight]) Fit Kernel Ridge regression model
get_params([deep]) Get parameters for this estimator.
predict(X) Predict using the kernel ridge model
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y, sample_weight=None) [source]

Fit Kernel Ridge regression model  Parameters

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

Training data. If kernel == “precomputed” this is instead a precomputed kernel matrix, of shape (n_samples, n_samples).
yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target values
sample_weightfloat or array-like of shape (n_samples,), default=None

Individual weights for each sample, ignored if None is passed.    Returns

selfreturns 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 using the kernel ridge model  Parameters

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

Samples. If kernel == “precomputed” this is instead a precomputed kernel matrix, shape = [n_samples, n_samples_fitted], where n_samples_fitted is the number of samples used in the fitting for this estimator.    Returns

Cndarray of shape (n_samples,) or (n_samples, n_targets)

Returns predicted values.

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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

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

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

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.kernel_ridge.KernelRidge

  Comparison of kernel ridge and Gaussian process regression

  Comparison of kernel ridge regression and SVR


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

Fit Kernel Ridge regression model  Parameters

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

Training data. If kernel == “precomputed” this is instead a precomputed kernel matrix, of shape (n_samples, n_samples).
yarray-like of shape (n_samples,) or (n_samples, n_targets)

Target values
sample_weightfloat or array-like of shape (n_samples,), default=None

Individual weights for each sample, ignored if None is passed.    Returns

selfreturns an instance of self.


predict(X) [source]

Predict using the kernel ridge model  Parameters

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

Samples. If kernel == “precomputed” this is instead a precomputed kernel matrix, shape = [n_samples, n_samples_fitted], where n_samples_fitted is the number of samples used in the fitting for this estimator.    Returns

Cndarray of shape (n_samples,) or (n_samples, n_targets)

Returns predicted values.


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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

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

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).


class sklearn.linear_model.ARDRegression(*, n_iter=300, tol=0.001, alpha_1=1e-06, alpha_2=1e-06, lambda_1=1e-06, lambda_2=1e-06, compute_score=False, threshold_lambda=10000.0, fit_intercept=True, normalize=False, copy_X=True, verbose=False) [source]

Bayesian ARD regression. Fit the weights of a regression model, using an ARD prior. The weights of the regression model are assumed to be in Gaussian distributions. Also estimate the parameters lambda (precisions of the distributions of the weights) and alpha (precision of the distribution of the noise). The estimation is done by an iterative procedures (Evidence Maximization) Read more in the User Guide.  Parameters

n_iterint, default=300

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

Stop the algorithm if w has converged.
alpha_1float, default=1e-6

Hyper-parameter : shape parameter for the Gamma distribution prior over the alpha parameter.
alpha_2float, default=1e-6

Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the alpha parameter.
lambda_1float, default=1e-6

Hyper-parameter : shape parameter for the Gamma distribution prior over the lambda parameter.
lambda_2float, default=1e-6

Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the lambda parameter.
compute_scorebool, default=False

If True, compute the objective function at each step of the model.
threshold_lambdafloat, default=10 000

threshold for removing (pruning) weights with high precision from the computation.
fit_interceptbool, default=True

whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be centered).
normalizebool, default=False

This parameter is ignored when fit_intercept is set to False. If True, the regressors X will be normalized before regression by subtracting the mean and dividing by the l2-norm. If you wish to standardize, please use StandardScaler before calling fit on an estimator with normalize=False.
copy_Xbool, default=True

If True, X will be copied; else, it may be overwritten.
verbosebool, default=False

Verbose mode when fitting the model.    Attributes

coef_array-like of shape (n_features,)

Coefficients of the regression model (mean of distribution)
alpha_float

estimated precision of the noise.
lambda_array-like of shape (n_features,)

estimated precisions of the weights.
sigma_array-like of shape (n_features, n_features)

estimated variance-covariance matrix of the weights
scores_float

if computed, value of the objective function (to be maximized)
intercept_float

Independent term in decision function. Set to 0.0 if fit_intercept = False.
X_offset_float

If normalize=True, offset subtracted for centering data to a zero mean.
X_scale_float

If normalize=True, parameter used to scale data to a unit standard deviation.     Notes For an example, see examples/linear_model/plot_ard.py. References D. J. C. MacKay, Bayesian nonlinear modeling for the prediction competition, ASHRAE Transactions, 1994. R. Salakhutdinov, Lecture notes on Statistical Machine Learning, http://www.utstat.toronto.edu/~rsalakhu/sta4273/notes/Lecture2.pdf#page=15 Their beta is our self.alpha_ Their alpha is our self.lambda_ ARD is a little different than the slide: only dimensions/features for which self.lambda_ < self.threshold_lambda are kept and the rest are discarded. Examples >>> from sklearn import linear_model
>>> clf = linear_model.ARDRegression()
>>> clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])
ARDRegression()
>>> clf.predict([[1, 1]])
array([1.])
 Methods
fit(X, y) Fit the ARDRegression model according to the given training data and parameters.
get_params([deep]) Get parameters for this estimator.
predict(X[, return_std]) Predict using the linear model.
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y) [source]

Fit the ARDRegression model according to the given training data and parameters. Iterative procedure to maximize the evidence  Parameters

Xarray-like of shape (n_samples, n_features)

Training vector, where n_samples in the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values (integers). Will be cast to X’s dtype if necessary    Returns

selfreturns 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, return_std=False) [source]

Predict using the linear model. In addition to the mean of the predictive distribution, also its standard deviation can be returned.  Parameters

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

Samples.
return_stdbool, default=False

Whether to return the standard deviation of posterior prediction.    Returns

y_meanarray-like of shape (n_samples,)

Mean of predictive distribution of query points.
y_stdarray-like of shape (n_samples,)

Standard deviation of predictive distribution of query points.

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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

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

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

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.linear_model.ARDRegression
class sklearn.linear_model.ARDRegression(*, n_iter=300, tol=0.001, alpha_1=1e-06, alpha_2=1e-06, lambda_1=1e-06, lambda_2=1e-06, compute_score=False, threshold_lambda=10000.0, fit_intercept=True, normalize=False, copy_X=True, verbose=False) [source]

Bayesian ARD regression. Fit the weights of a regression model, using an ARD prior. The weights of the regression model are assumed to be in Gaussian distributions. Also estimate the parameters lambda (precisions of the distributions of the weights) and alpha (precision of the distribution of the noise). The estimation is done by an iterative procedures (Evidence Maximization) Read more in the User Guide.  Parameters

n_iterint, default=300

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

Stop the algorithm if w has converged.
alpha_1float, default=1e-6

Hyper-parameter : shape parameter for the Gamma distribution prior over the alpha parameter.
alpha_2float, default=1e-6

Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the alpha parameter.
lambda_1float, default=1e-6

Hyper-parameter : shape parameter for the Gamma distribution prior over the lambda parameter.
lambda_2float, default=1e-6

Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the lambda parameter.
compute_scorebool, default=False

If True, compute the objective function at each step of the model.
threshold_lambdafloat, default=10 000

threshold for removing (pruning) weights with high precision from the computation.
fit_interceptbool, default=True

whether to calculate the intercept for this model. If set to false, no intercept will be used in calculations (i.e. data is expected to be centered).
normalizebool, default=False

This parameter is ignored when fit_intercept is set to False. If True, the regressors X will be normalized before regression by subtracting the mean and dividing by the l2-norm. If you wish to standardize, please use StandardScaler before calling fit on an estimator with normalize=False.
copy_Xbool, default=True

If True, X will be copied; else, it may be overwritten.
verbosebool, default=False

Verbose mode when fitting the model.    Attributes

coef_array-like of shape (n_features,)

Coefficients of the regression model (mean of distribution)
alpha_float

estimated precision of the noise.
lambda_array-like of shape (n_features,)

estimated precisions of the weights.
sigma_array-like of shape (n_features, n_features)

estimated variance-covariance matrix of the weights
scores_float

if computed, value of the objective function (to be maximized)
intercept_float

Independent term in decision function. Set to 0.0 if fit_intercept = False.
X_offset_float

If normalize=True, offset subtracted for centering data to a zero mean.
X_scale_float

If normalize=True, parameter used to scale data to a unit standard deviation.     Notes For an example, see examples/linear_model/plot_ard.py. References D. J. C. MacKay, Bayesian nonlinear modeling for the prediction competition, ASHRAE Transactions, 1994. R. Salakhutdinov, Lecture notes on Statistical Machine Learning, http://www.utstat.toronto.edu/~rsalakhu/sta4273/notes/Lecture2.pdf#page=15 Their beta is our self.alpha_ Their alpha is our self.lambda_ ARD is a little different than the slide: only dimensions/features for which self.lambda_ < self.threshold_lambda are kept and the rest are discarded. Examples >>> from sklearn import linear_model
>>> clf = linear_model.ARDRegression()
>>> clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])
ARDRegression()
>>> clf.predict([[1, 1]])
array([1.])
 Methods
fit(X, y) Fit the ARDRegression model according to the given training data and parameters.
get_params([deep]) Get parameters for this estimator.
predict(X[, return_std]) Predict using the linear model.
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y) [source]

Fit the ARDRegression model according to the given training data and parameters. Iterative procedure to maximize the evidence  Parameters

Xarray-like of shape (n_samples, n_features)

Training vector, where n_samples in the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values (integers). Will be cast to X’s dtype if necessary    Returns

selfreturns 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, return_std=False) [source]

Predict using the linear model. In addition to the mean of the predictive distribution, also its standard deviation can be returned.  Parameters

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

Samples.
return_stdbool, default=False

Whether to return the standard deviation of posterior prediction.    Returns

y_meanarray-like of shape (n_samples,)

Mean of predictive distribution of query points.
y_stdarray-like of shape (n_samples,)

Standard deviation of predictive distribution of query points.

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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

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

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

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.linear_model.ARDRegression

  Automatic Relevance Determination Regression (ARD)


fit(X, y) [source]

Fit the ARDRegression model according to the given training data and parameters. Iterative procedure to maximize the evidence  Parameters

Xarray-like of shape (n_samples, n_features)

Training vector, where n_samples in the number of samples and n_features is the number of features.
yarray-like of shape (n_samples,)

Target values (integers). Will be cast to X’s dtype if necessary    Returns

selfreturns an instance of self.


predict(X, return_std=False) [source]

Predict using the linear model. In addition to the mean of the predictive distribution, also its standard deviation can be returned.  Parameters

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

Samples.
return_stdbool, default=False

Whether to return the standard deviation of posterior prediction.    Returns

y_meanarray-like of shape (n_samples,)

Mean of predictive distribution of query points.
y_stdarray-like of shape (n_samples,)

Standard deviation of predictive distribution of query points.


class sklearn.linear_model.BayesianRidge(*, n_iter=300, tol=0.001, alpha_1=1e-06, alpha_2=1e-06, lambda_1=1e-06, lambda_2=1e-06, alpha_init=None, lambda_init=None, compute_score=False, fit_intercept=True, normalize=False, copy_X=True, verbose=False) [source]

Bayesian ridge regression. Fit a Bayesian ridge model. See the Notes section for details on this implementation and the optimization of the regularization parameters lambda (precision of the weights) and alpha (precision of the noise). Read more in the User Guide.  Parameters

n_iterint, default=300

Maximum number of iterations. Should be greater than or equal to 1.
tolfloat, default=1e-3

Stop the algorithm if w has converged.
alpha_1float, default=1e-6

Hyper-parameter : shape parameter for the Gamma distribution prior over the alpha parameter.
alpha_2float, default=1e-6

Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the alpha parameter.
lambda_1float, default=1e-6

Hyper-parameter : shape parameter for the Gamma distribution prior over the lambda parameter.
lambda_2float, default=1e-6

Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the lambda parameter.
alpha_initfloat, default=None

Initial value for alpha (precision of the noise). If not set, alpha_init is 1/Var(y).  New in version 0.22.
lambda_initfloat, default=None

Initial value for lambda (precision of the weights). If not set, lambda_init is 1.  New in version 0.22.
compute_scorebool, default=False

If True, compute the log marginal likelihood at each iteration of the optimization.
fit_interceptbool, default=True

Whether to calculate the intercept for this model. The intercept is not treated as a probabilistic parameter and thus has no associated variance. If set to False, no intercept will be used in calculations (i.e. data is expected to be centered).
normalizebool, default=False

This parameter is ignored when fit_intercept is set to False. If True, the regressors X will be normalized before regression by subtracting the mean and dividing by the l2-norm. If you wish to standardize, please use StandardScaler before calling fit on an estimator with normalize=False.
copy_Xbool, default=True

If True, X will be copied; else, it may be overwritten.
verbosebool, default=False

Verbose mode when fitting the model.    Attributes

coef_array-like of shape (n_features,)

Coefficients of the regression model (mean of distribution)
intercept_float

Independent term in decision function. Set to 0.0 if fit_intercept = False.
alpha_float

Estimated precision of the noise.
lambda_float

Estimated precision of the weights.
sigma_array-like of shape (n_features, n_features)

Estimated variance-covariance matrix of the weights
scores_array-like of shape (n_iter_+1,)

If computed_score is True, value of the log marginal likelihood (to be maximized) at each iteration of the optimization. The array starts with the value of the log marginal likelihood obtained for the initial values of alpha and lambda and ends with the value obtained for the estimated alpha and lambda.
n_iter_int

The actual number of iterations to reach the stopping criterion.
X_offset_float

If normalize=True, offset subtracted for centering data to a zero mean.
X_scale_float

If normalize=True, parameter used to scale data to a unit standard deviation.     Notes There exist several strategies to perform Bayesian ridge regression. This implementation is based on the algorithm described in Appendix A of (Tipping, 2001) where updates of the regularization parameters are done as suggested in (MacKay, 1992). Note that according to A New View of Automatic Relevance Determination (Wipf and Nagarajan, 2008) these update rules do not guarantee that the marginal likelihood is increasing between two consecutive iterations of the optimization. References D. J. C. MacKay, Bayesian Interpolation, Computation and Neural Systems, Vol. 4, No. 3, 1992. M. E. Tipping, Sparse Bayesian Learning and the Relevance Vector Machine, Journal of Machine Learning Research, Vol. 1, 2001. Examples >>> from sklearn import linear_model
>>> clf = linear_model.BayesianRidge()
>>> clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])
BayesianRidge()
>>> clf.predict([[1, 1]])
array([1.])
 Methods
fit(X, y[, sample_weight]) Fit the model
get_params([deep]) Get parameters for this estimator.
predict(X[, return_std]) Predict using the linear model.
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y, sample_weight=None) [source]

Fit the model  Parameters

Xndarray of shape (n_samples, n_features)

Training data
yndarray of shape (n_samples,)

Target values. Will be cast to X’s dtype if necessary
sample_weightndarray of shape (n_samples,), default=None

Individual weights for each sample  New in version 0.20: parameter sample_weight support to BayesianRidge.     Returns

selfreturns 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, return_std=False) [source]

Predict using the linear model. In addition to the mean of the predictive distribution, also its standard deviation can be returned.  Parameters

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

Samples.
return_stdbool, default=False

Whether to return the standard deviation of posterior prediction.    Returns

y_meanarray-like of shape (n_samples,)

Mean of predictive distribution of query points.
y_stdarray-like of shape (n_samples,)

Standard deviation of predictive distribution of query points.

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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

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

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

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.linear_model.BayesianRidge
class sklearn.linear_model.BayesianRidge(*, n_iter=300, tol=0.001, alpha_1=1e-06, alpha_2=1e-06, lambda_1=1e-06, lambda_2=1e-06, alpha_init=None, lambda_init=None, compute_score=False, fit_intercept=True, normalize=False, copy_X=True, verbose=False) [source]

Bayesian ridge regression. Fit a Bayesian ridge model. See the Notes section for details on this implementation and the optimization of the regularization parameters lambda (precision of the weights) and alpha (precision of the noise). Read more in the User Guide.  Parameters

n_iterint, default=300

Maximum number of iterations. Should be greater than or equal to 1.
tolfloat, default=1e-3

Stop the algorithm if w has converged.
alpha_1float, default=1e-6

Hyper-parameter : shape parameter for the Gamma distribution prior over the alpha parameter.
alpha_2float, default=1e-6

Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the alpha parameter.
lambda_1float, default=1e-6

Hyper-parameter : shape parameter for the Gamma distribution prior over the lambda parameter.
lambda_2float, default=1e-6

Hyper-parameter : inverse scale parameter (rate parameter) for the Gamma distribution prior over the lambda parameter.
alpha_initfloat, default=None

Initial value for alpha (precision of the noise). If not set, alpha_init is 1/Var(y).  New in version 0.22.
lambda_initfloat, default=None

Initial value for lambda (precision of the weights). If not set, lambda_init is 1.  New in version 0.22.
compute_scorebool, default=False

If True, compute the log marginal likelihood at each iteration of the optimization.
fit_interceptbool, default=True

Whether to calculate the intercept for this model. The intercept is not treated as a probabilistic parameter and thus has no associated variance. If set to False, no intercept will be used in calculations (i.e. data is expected to be centered).
normalizebool, default=False

This parameter is ignored when fit_intercept is set to False. If True, the regressors X will be normalized before regression by subtracting the mean and dividing by the l2-norm. If you wish to standardize, please use StandardScaler before calling fit on an estimator with normalize=False.
copy_Xbool, default=True

If True, X will be copied; else, it may be overwritten.
verbosebool, default=False

Verbose mode when fitting the model.    Attributes

coef_array-like of shape (n_features,)

Coefficients of the regression model (mean of distribution)
intercept_float

Independent term in decision function. Set to 0.0 if fit_intercept = False.
alpha_float

Estimated precision of the noise.
lambda_float

Estimated precision of the weights.
sigma_array-like of shape (n_features, n_features)

Estimated variance-covariance matrix of the weights
scores_array-like of shape (n_iter_+1,)

If computed_score is True, value of the log marginal likelihood (to be maximized) at each iteration of the optimization. The array starts with the value of the log marginal likelihood obtained for the initial values of alpha and lambda and ends with the value obtained for the estimated alpha and lambda.
n_iter_int

The actual number of iterations to reach the stopping criterion.
X_offset_float

If normalize=True, offset subtracted for centering data to a zero mean.
X_scale_float

If normalize=True, parameter used to scale data to a unit standard deviation.     Notes There exist several strategies to perform Bayesian ridge regression. This implementation is based on the algorithm described in Appendix A of (Tipping, 2001) where updates of the regularization parameters are done as suggested in (MacKay, 1992). Note that according to A New View of Automatic Relevance Determination (Wipf and Nagarajan, 2008) these update rules do not guarantee that the marginal likelihood is increasing between two consecutive iterations of the optimization. References D. J. C. MacKay, Bayesian Interpolation, Computation and Neural Systems, Vol. 4, No. 3, 1992. M. E. Tipping, Sparse Bayesian Learning and the Relevance Vector Machine, Journal of Machine Learning Research, Vol. 1, 2001. Examples >>> from sklearn import linear_model
>>> clf = linear_model.BayesianRidge()
>>> clf.fit([[0,0], [1, 1], [2, 2]], [0, 1, 2])
BayesianRidge()
>>> clf.predict([[1, 1]])
array([1.])
 Methods
fit(X, y[, sample_weight]) Fit the model
get_params([deep]) Get parameters for this estimator.
predict(X[, return_std]) Predict using the linear model.
score(X, y[, sample_weight]) Return the coefficient of determination \(R^2\) of the prediction.
set_params(**params) Set the parameters of this estimator.
fit(X, y, sample_weight=None) [source]

Fit the model  Parameters

Xndarray of shape (n_samples, n_features)

Training data
yndarray of shape (n_samples,)

Target values. Will be cast to X’s dtype if necessary
sample_weightndarray of shape (n_samples,), default=None

Individual weights for each sample  New in version 0.20: parameter sample_weight support to BayesianRidge.     Returns

selfreturns 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, return_std=False) [source]

Predict using the linear model. In addition to the mean of the predictive distribution, also its standard deviation can be returned.  Parameters

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

Samples.
return_stdbool, default=False

Whether to return the standard deviation of posterior prediction.    Returns

y_meanarray-like of shape (n_samples,)

Mean of predictive distribution of query points.
y_stdarray-like of shape (n_samples,)

Standard deviation of predictive distribution of query points.

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

Return the coefficient of determination \(R^2\) of the prediction. The coefficient \(R^2\) is defined as \((1 - \frac{u}{v})\), where \(u\) is the residual sum of squares ((y_true - y_pred)
** 2).sum() and \(v\) is the total sum of squares ((y_true -
y_true.mean()) ** 2).sum(). The best possible score is 1.0 and it can be negative (because the model can be arbitrarily worse). A constant model that always predicts the expected value of y, disregarding the input features, would get a \(R^2\) score of 0.0.  Parameters

Xarray-like of shape (n_samples, n_features)

Test samples. For some estimators this may be a precomputed kernel matrix or a list of generic objects instead with shape (n_samples, n_samples_fitted), where n_samples_fitted is the number of samples used in the fitting for the estimator.
yarray-like of shape (n_samples,) or (n_samples, n_outputs)

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

Sample weights.    Returns

scorefloat

\(R^2\) of self.predict(X) wrt. y.     Notes The \(R^2\) score used when calling score on a regressor uses multioutput='uniform_average' from version 0.23 to keep consistent with default value of r2_score. This influences the score method of all the multioutput regressors (except for MultiOutputRegressor).

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.linear_model.BayesianRidge

  Feature agglomeration vs. univariate selection

  Curve Fitting with Bayesian Ridge Regression

  Bayesian Ridge Regression

  Imputing missing values with variants of IterativeImputer


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

Fit the model  Parameters

Xndarray of shape (n_samples, n_features)

Training data
yndarray of shape (n_samples,)

Target values. Will be cast to X’s dtype if necessary
sample_weightndarray of shape (n_samples,), default=None

Individual weights for each sample  New in version 0.20: parameter sample_weight support to BayesianRidge.     Returns

selfreturns an instance of self.