sklearn.preprocessing
.KernelCenterer¶
- 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 forKernelPCA
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.
New in version 0.24.
- feature_names_in_ndarray of shape (
n_features_in_
,) Names of features seen during fit. Defined only when
X
has feature names that are all strings.New in version 1.0.
See also
sklearn.kernel_approximation.Nystroem
Approximate a kernel map using a subset of the training data.
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.]])
Methods
fit
(K[, y])Fit KernelCenterer.
fit_transform
(X[, y])Fit to data, then transform it.
get_feature_names_out
([input_features])Get output feature names for transformation.
get_params
([deep])Get parameters for this estimator.
set_output
(*[, transform])Set output container.
set_params
(**params)Set the parameters of this estimator.
transform
(K[, copy])Center kernel matrix.
- 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
andy
with optional parametersfit_params
and returns a transformed version ofX
.- 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_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 API for an example on how to use the API.
- Parameters:
- transform{“default”, “pandas”}, default=None
Configure output of
transform
andfit_transform
."default"
: Default output format of a transformer"pandas"
: DataFrame outputNone
: Transform configuration is unchanged
- 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.