mojolearn.KernelPCA#

class mojolearn.KernelPCA(n_components=None, *, kernel='linear', gamma=None, degree=3, coef0=1, kernel_params=None, alpha=1.0, fit_inverse_transform=False, eigen_solver='auto', tol=0, max_iter=None, iterated_power='auto', remove_zero_eig=False, random_state=None, copy_X=True, n_jobs=None)[source]#

Bases: _XNeighbors

Kernel principal component analysis.

Non-linear dimensionality reduction: forms the kernel matrix of the training data, centers it, and projects onto its leading eigenvectors, following scikit-learn’s KernelPCA. Negative eigenvalues are set to zero, components are sorted by decreasing eigenvalue, and each eigenvector’s sign is fixed so its largest-magnitude entry is positive. Computation is float32.

With eigen_solver='auto', more than 200 samples and 0 < n_components < 10, the top components come from an iterative (Lanczos) solver; every other case, including 'arpack' and 'randomized', uses an exact dense eigendecomposition.

Parameters:
  • n_components (int or None, default None) – Number of components. None keeps every component with a positive eigenvalue.

  • kernel (str, default ‘linear’) – 'linear', 'poly' (or 'polynomial'), 'rbf', 'sigmoid', 'laplacian', 'cosine', 'chi2', 'additive_chi2' or 'precomputed' (fit then takes the n x n Gram matrix and transform the n_queries x n_train kernel). Callable kernels raise NotImplementedError.

  • gamma (float or None, default None) – Kernel coefficient; None means 1 / n_features.

  • degree (int, default 3) – Degree of the 'poly' kernel.

  • coef0 (float, default 1) – Independent term of the 'poly' and 'sigmoid' kernels.

  • kernel_params (dict or None, default None) – Not supported; a non-empty value raises NotImplementedError.

  • alpha (float, default 1.0) – Accepted for scikit-learn compatibility; unused (no inverse transform).

  • fit_inverse_transform (bool, default False) – Not supported; True raises NotImplementedError in fit.

  • eigen_solver (str, default ‘auto’) – 'auto' may use the iterative solver described above; any other value uses the dense solver.

  • tol, max_iter, iterated_power, random_state, copy_X, n_jobs – Accepted for scikit-learn compatibility; unused.

  • remove_zero_eig (bool, default False) – Drop components with zero eigenvalue even when n_components is set.

  • numeric_mode ({‘identical’, ‘deterministic’, ‘fast’} or None, default None) – Numeric mode for this estimator ('fast' is Apple only). None uses the process default.

Variables:
  • eigenvalues_ (Array of shape (n_components,)) – Eigenvalues of the centered kernel matrix, decreasing.

  • eigenvectors_ (Array of shape (n_samples, n_components)) – Eigenvectors of the centered kernel matrix.

  • n_features_in_ (int) – Number of features seen in fit.

Examples

>>> import numpy as np, mojolearn
>>> X = np.random.default_rng(0).random((1000, 8), dtype=np.float32)
>>> Z = mojolearn.KernelPCA(n_components=2, kernel="rbf").fit_transform(X)
__init__(n_components=None, *, kernel='linear', gamma=None, degree=3, coef0=1, kernel_params=None, alpha=1.0, fit_inverse_transform=False, eigen_solver='auto', tol=0, max_iter=None, iterated_power='auto', remove_zero_eig=False, random_state=None, copy_X=True, n_jobs=None)[source]#

Methods

fit(X[, y])

Fit the model on the training data.

fit_transform(X[, y])

Fit the model and return the projection of the training data.

get_feature_names_out([input_features])

Output feature names: kernelpca0, kernelpca1, ...

get_params([deep])

inverse_transform(X)

Not supported: always raises NotImplementedError.

numeric_mode_used()

The numeric mode this estimator runs in.

set_params(**params)

Set constructor parameters.

transform(X)

Project new samples onto the fitted components.

vendor_used()

The GPU backend this estimator runs on.

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

Fit the model on the training data.

Parameters:
  • X (array-like or scipy.sparse matrix of shape (n_samples, n_features)) – Training data, or the (n_samples, n_samples) Gram matrix for kernel='precomputed'. Sparse input is densified.

  • y (None) – Ignored.

Returns:

self (KernelPCA) – The fitted estimator.

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

Fit the model and return the projection of the training data.

Parameters:
  • X (array-like of shape (n_samples, n_features)) – Training data (or the Gram matrix for kernel='precomputed').

  • y (None) – Ignored.

  • **params – Ignored.

Returns:

X_new (Array of shape (n_samples, n_components), float32)

transform(X)[source]#

Project new samples onto the fitted components.

Parameters:

X (array-like of shape (n_queries, n_features)) – Samples, or for kernel='precomputed' the (n_queries, n_train) kernel against the training rows (a different column count raises ValueError).

Returns:

X_new (Array of shape (n_queries, n_components), float32)

inverse_transform(X)[source]#

Not supported: always raises NotImplementedError.

get_feature_names_out(input_features=None)[source]#

Output feature names: kernelpca0, kernelpca1, …

Parameters:

input_features (None) – Ignored.

Returns:

names (list of str)