KernelCenterer (original) (raw)

class sklearn.preprocessing.KernelCenterer[source]#

Center an arbitrary kernel matrix \(K\).

Let define a kernel \(K\) such that:

\[K(X, Y) = \phi(X) . \phi(Y)^{T}\]

\(\phi(X)\) is a function mapping of rows of \(X\) to a Hilbert space and \(K\) is of shape (n_samples, n_samples).

This class allows to compute \(\tilde{K}(X, Y)\) such that:

\[\tilde{K(X, Y)} = \tilde{\phi}(X) . \tilde{\phi}(Y)^{T}\]

\(\tilde{\phi}(X)\) is the centered mapped data in the Hilbert space.

KernelCenterer centers the features without explicitly computing the mapping \(\phi(\cdot)\). Working with centered kernels is sometime expected when dealing with algebra computation such as eigendecomposition for KernelPCA for instance.

Read more in the User Guide.

Attributes:

**K_fit_rows_**ndarray of shape (n_samples,)

Average of each column of kernel matrix.

**K_fit_all_**float

Average of kernel matrix.

**n_features_in_**int

Number of features seen during fit.

Added in version 0.24.

**feature_names_in_**ndarray of shape (n_features_in_,)

Names of features seen during fit. Defined only when Xhas feature names that are all strings.

Added in version 1.0.

References

Examples

from sklearn.preprocessing import KernelCenterer from sklearn.metrics.pairwise import pairwise_kernels X = [[ 1., -2., 2.], ... [ -2., 1., 3.], ... [ 4., 1., -2.]] K = pairwise_kernels(X, metric='linear') K array([[ 9., 2., -2.], [ 2., 14., -13.], [ -2., -13., 21.]]) transformer = KernelCenterer().fit(K) transformer KernelCenterer() transformer.transform(K) array([[ 5., 0., -5.], [ 0., 14., -14.], [ -5., -14., 19.]])

fit(K, y=None)[source]#

Fit KernelCenterer.

Parameters:

Kndarray of shape (n_samples, n_samples)

Kernel matrix.

yNone

Ignored.

Returns:

selfobject

Returns the instance itself.

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

Fit to data, then transform it.

Fits transformer to X and y with optional parameters fit_paramsand returns a transformed version of X.

Parameters:

Xarray-like of shape (n_samples, n_features)

Input samples.

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

Target values (None for unsupervised transformations).

**fit_paramsdict

Additional fit parameters.

Returns:

X_newndarray array of shape (n_samples, n_features_new)

Transformed array.

get_feature_names_out(input_features=None)[source]#

Get output feature names for transformation.

The feature names out will prefixed by the lowercased class name. For example, if the transformer outputs 3 features, then the feature names out are: ["class_name0", "class_name1", "class_name2"].

Parameters:

input_featuresarray-like of str or None, default=None

Only used to validate feature names with the names seen in fit.

Returns:

feature_names_outndarray of str objects

Transformed feature names.

get_metadata_routing()[source]#

Get metadata routing of this object.

Please check User Guide on how the routing mechanism works.

Returns:

routingMetadataRequest

A MetadataRequest encapsulating routing information.

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_output(*, transform=None)[source]#

Set output container.

See Introducing the set_output APIfor an example on how to use the API.

Parameters:

transform{“default”, “pandas”, “polars”}, default=None

Configure output of transform and fit_transform.

Added in version 1.4: "polars" option was added.

Returns:

selfestimator instance

Estimator instance.

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.

set_transform_request(*, copy: bool | None | str = '$UNCHANGED$') → KernelCenterer[source]#

Request metadata passed to the transform method.

Note that this method is only relevant ifenable_metadata_routing=True (see sklearn.set_config). Please see User Guide on how the routing mechanism works.

The options for each parameter are:

The default (sklearn.utils.metadata_routing.UNCHANGED) retains the existing request. This allows you to change the request for some parameters and not others.

Added in version 1.3.

Note

This method is only relevant if this estimator is used as a sub-estimator of a meta-estimator, e.g. used inside aPipeline. Otherwise it has no effect.

Parameters:

copystr, True, False, or None, default=sklearn.utils.metadata_routing.UNCHANGED

Metadata routing for copy parameter in transform.

Returns:

selfobject

The updated object.

transform(K, copy=True)[source]#

Center kernel matrix.

Parameters:

Kndarray of shape (n_samples1, n_samples2)

Kernel matrix.

copybool, default=True

Set to False to perform inplace computation.

Returns:

K_newndarray of shape (n_samples1, n_samples2)

Returns the instance itself.