KernelPCA
- class sklearn.decomposition.KernelPCA(n_components=None, *, kernel='linear', gamma=None, degree=3, coef0=1, kernel_params=, , , , , , , , , ,
Kernel Principal component analysis (KPCA).
Non-linear dimensionality reduction through the use of kernels [1], see also Pairwise metrics, Affinities and Kernels.
It uses the scipy.linalg.eigh LAPACK implementation of the full SVD
or the scipy.sparse.linalg.eigsh ARPACK implementation of the
truncated SVD, depending on the shape of the input data and the number of
components to extract. It can also use a randomized truncated SVD by the
method proposed in [3], see eigen_solver.
For a usage example and comparison between Principal Components Analysis (PCA) and its kernelized version (KPCA), see Kernel PCA.
For a usage example in denoising images using KPCA, see Image denoising using kernel PCA.
Read more in the User Guide.
Number of components. If None, all non-zero components are kept.
Kernel used for PCA.
Kernel coefficient for rbf, poly and sigmoid kernels. Ignored by other
kernels. If gamma is None, then it is set to 1/n_features.
Degree for poly kernels. Ignored by other kernels.
>>> from sklearn.datasets import load_digits
>>> from sklearn.decomposition import KernelPCA
>>> X, _ = load_digits(return_X_y=True)
>>> transformer = KernelPCA(n_components=7, kernel='linear'
Fit the model from data in X.
Training vector, where n_samples is the number of samples
and n_features is the number of features.
Not used, present for API consistency by convention.
Fit the model from data in X and transform X.
Training vector, where n_samples is the number of samples
and n_features is the number of features.
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"].
Only used to validate feature names with the names seen in fit.
Get metadata routing of this object.
Please check User Guide on how the routing mechanism works.
A MetadataRequest encapsulating
routing information.
Get parameters for this estimator.
If True, will return the parameters for this estimator and contained subobjects that are estimators.
Parameter names mapped to their values.
Transform X back to original space.
inverse_transform approximates the inverse transformation using
a learned pre-image. The pre-image is learned by kernel ridge
regression of the original data on their low-dimensional representation
vectors.
Set output container.
Refer to the user guide for more details and Introducing the set_output API for an example on how to use the API.
Configure output of transform and .
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.
Estimator parameters.
Transform X.
Training vector, where n_samples is the number of samples
and n_features is the number of features.
Projection of X in the first principal components, where is the number of samples and is the number of the components.
Independent term in poly and sigmoid kernels. Ignored by other kernels.
Parameters (keyword arguments) and values for kernel passed as callable object. Ignored by other kernels.
Hyperparameter of the ridge regression that learns the inverse transform (when fit_inverse_transform=True).
Learn the inverse transform for non-precomputed kernels (i.e. learn to find the pre-image of a point). This method is based on [2].
Select eigensolver to use. If n_components is much
less than the number of training samples, randomized (or arpack to a
smaller extent) may be more efficient than the dense eigensolver.
Randomized SVD is performed according to the method of Halko et al
[3].
the solver is selected by a default policy based on n_samples
(the number of training samples) and n_components:
if the number of components to extract is less than 10 (strict) and
the number of samples is more than 200 (strict), the ‘arpack’
method is enabled. Otherwise the exact full eigenvalue
decomposition is computed and optionally truncated afterwards
(‘dense’ method).
run exact full eigenvalue decomposition calling the standard
LAPACK solver via scipy.linalg.eigh, and select the components
by postprocessing
run SVD truncated to n_components calling ARPACK solver using
scipy.sparse.linalg.eigsh. It requires strictly
0 < n_components < n_samples
run randomized SVD by the method of Halko et al. [3]. The current implementation selects eigenvalues based on their module; therefore using this method can lead to unexpected results if the kernel is not positive semi-definite. See also [4].
Convergence tolerance for arpack. If 0, optimal value will be chosen by arpack.
Maximum number of iterations for arpack. If None, optimal value will be chosen by arpack.
Number of iterations for the power method computed by
svd_solver == ‘randomized’. When ‘auto’, it is set to 7 when
n_components < 0.1 * min(X.shape), other it is set to 4.
If True, then all components with zero eigenvalues are removed, so that the number of components in the output may be < n_components (and sometimes even zero due to numerical instability). When n_components is None, this parameter is ignored and components with zero eigenvalues are removed regardless.
Used when eigen_solver == ‘arpack’ or ‘randomized’. Pass an int
for reproducible results across multiple function calls.
See Glossary.
If True, input X is copied and stored by the model in the X_fit_
attribute. If no further changes will be done to X, setting
copy_X=False saves memory by storing a reference.
The number of parallel jobs to run.
None means 1 unless in a joblib.parallel_backend context.
-1 means using all processors. See Glossary
for more details.
Eigenvalues of the centered kernel matrix in decreasing order.
If n_components and remove_zero_eig are not set,
then all values are stored.
Eigenvectors of the centered kernel matrix. If n_components and
remove_zero_eig are not set, then all components are stored.
Inverse transform matrix. Only available when
fit_inverse_transform is True.
Projection of the fitted data on the kernel principal components.
Only available when fit_inverse_transform is True.
The data used to fit the model. If copy_X=False, then X_fit_ is
a reference. This attribute is used for the calls to transform.
Number of features seen during fit.
n_features_in_,)Names of features seen during fit. Defined only when X
has feature names that are all strings.
Kernel coefficient for rbf, poly and sigmoid kernels. When gamma
is explicitly provided, this is just the same as gamma. When gamma
is None, this is the actual value of kernel coefficient.
PCAPrincipal Component Analysis.
Sparse Principal Component Analysis.
Dimensionality reduction using truncated SVD.
Returns the instance itself.
Not used, present for API consistency by convention.
Parameters (keyword arguments) and values passed to the fit_transform instance.
Transformed values.
Transformed feature names.
inverse_transformTraining vector, where n_samples is the number of samples
and n_features is the number of features.
Original data, where n_samples is the number of samples
and n_features is the number of features.
"default": Default output format of a transformer
"pandas": DataFrame output
"polars": Polars output
None: Transform configuration is unchanged
Estimator instance.
Estimator instance.
n_samplesn_components