skoot.decomposition.SelectiveKernelPCA

class skoot.decomposition.SelectiveKernelPCA(cols=None, as_df=True, trans_col_name=None, **kwargs)[source][source]

Kernel Principal component analysis (KPCA) (applied to selected columns).

Non-linear dimensionality reduction through the use of kernels (see metrics).

Read more in the User Guide.

This class wraps scikit-learn’s KernelPCA. When a pd.DataFrame is passed to the fit method, the transformation is applied to the selected columns, which are subsequently dropped from the frame. All remaining columns are left alone.

Parameters:

cols : array-like, shape=(n_features,), optional (default=None)

The names of the columns on which to apply the transformation. If no column names are provided, the transformer will be fit on the entire frame. Note that the transformation will also only apply to the specified columns, and any other non-specified columns will still be present after the transformation.

as_df : bool, optional (default=True)

Whether to return a Pandas DataFrame in the transform method. If False, will return a Numpy ndarray instead. Since most skoot transformers depend on explicitly-named DataFrame features, the as_df parameter is True by default.

trans_col_name : str, unicode or iterable, optional

The name or list of names to apply to the transformed column(s). If a string is provided, it is used as a prefix for new columns. If an iterable is provided, its dimensions must match the number of produced columns. If None (default), will use the estimator class name as the prefix.

n_components : int, default=None

Number of components. If None, all non-zero components are kept.

kernel : “linear” | “poly” | “rbf” | “sigmoid” | “cosine” | “precomputed”

Kernel. Default=”linear”.

gamma : float, default=1/n_features

Kernel coefficient for rbf, poly and sigmoid kernels. Ignored by other kernels.

degree : int, default=3

Degree for poly kernels. Ignored by other kernels.

coef0 : float, default=1

Independent term in poly and sigmoid kernels. Ignored by other kernels.

kernel_params : mapping of string to any, default=None

Parameters (keyword arguments) and values for kernel passed as callable object. Ignored by other kernels.

alpha : int, default=1.0

Hyperparameter of the ridge regression that learns the inverse transform (when fit_inverse_transform=True).

fit_inverse_transform : bool, default=False

Learn the inverse transform for non-precomputed kernels. (i.e. learn to find the pre-image of a point)

eigen_solver : string [‘auto’|’dense’|’arpack’], default=’auto’

Select eigensolver to use. If n_components is much less than the number of training samples, arpack may be more efficient than the dense eigensolver.

tol : float, default=0

Convergence tolerance for arpack. If 0, optimal value will be chosen by arpack.

max_iter : int, default=None

Maximum number of iterations for arpack. If None, optimal value will be chosen by arpack.

remove_zero_eig : boolean, default=False

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.

random_state : int, RandomState instance or None, optional (default=None)

If int, random_state is the seed used by the random number generator; If RandomState instance, random_state is the random number generator; If None, the random number generator is the RandomState instance used by np.random. Used when eigen_solver == ‘arpack’.

New in version 0.18.

copy_X : boolean, default=True

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.

New in version 0.18.

n_jobs : int or None, optional (default=None)

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.

New in version 0.18.

Attributes

lambdas_ (array, (n_components,)) Eigenvalues of the centered kernel matrix in decreasing order. If n_components and remove_zero_eig are not set, then all values are stored.
alphas_ (array, (n_samples, n_components)) Eigenvectors of the centered kernel matrix. If n_components and remove_zero_eig are not set, then all components are stored.
dual_coef_ (array, (n_samples, n_features)) Inverse transform matrix. Only available when fit_inverse_transform is True.
X_transformed_fit_ (array, (n_samples, n_components)) Projection of the fitted data on the kernel principal components. Only available when fit_inverse_transform is True.
X_fit_ ((n_samples, n_features)) 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.

References

Kernel PCA was introduced in:
Bernhard Schoelkopf, Alexander J. Smola, and Klaus-Robert Mueller. 1999. Kernel principal component analysis. In Advances in kernel methods, MIT Press, Cambridge, MA, USA 327-352.

Examples

>>> 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')
>>> X_transformed = transformer.fit_transform(X)
>>> X_transformed.shape
(1797, 7)

Methods

fit(X[, y]) Fit the wrapped transformer.
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) Transform a test dataframe.
__init__(cols=None, as_df=True, trans_col_name=None, **kwargs)[source]

Initialize self. See help(type(self)) for accurate signature.

fit(X, y=None, **fit_kwargs)[source]

Fit the wrapped transformer.

This method will fit the wrapped sklearn transformer on the selected columns, leaving other columns alone.

Parameters:

X : pd.DataFrame, shape=(n_samples, n_features)

The Pandas frame to fit. The frame will only be fit on the prescribed cols (see __init__) or all of them if cols is None. Furthermore, X will not be altered in the process of the fit.

y : array-like or None, shape=(n_samples,), optional (default=None)

Pass-through for sklearn.pipeline.Pipeline. Even if explicitly set, will not change behavior of fit.

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:

X : numpy array of shape [n_samples, n_features]

Training set.

y : numpy array of shape [n_samples]

Target values.

Returns:

X_new : numpy array of shape [n_samples, n_features_new]

Transformed array.

get_params(deep=True)[source]

Get parameters for this estimator.

Parameters:

deep : boolean, optional

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

Returns:

params : mapping of string to any

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 pipelines). The latter have parameters of the form <component>__<parameter> so that it’s possible to update each component of a nested object.

Returns:self
transform(X)[source]

Transform a test dataframe.

Parameters:

X : pd.DataFrame, shape=(n_samples, n_features)

The Pandas frame to transform. The operation will be applied to a copy of the input data, and the result will be returned.

Returns:

X : pd.DataFrame, shape=(n_samples, n_features)

The operation is applied to a copy of X, and the result set is returned.