KernelCenterer#
- class cuml.preprocessing.KernelCenterer(*args, **kwargs)[source]#
Center a kernel matrix
Let K(x, z) be a kernel defined by phi(x)^T phi(z), where phi is a function mapping x to a Hilbert space. KernelCenterer centers (i.e., normalize to have zero mean) the data without explicitly computing phi(x). It is equivalent to centering phi(x) with cuml.preprocessing.StandardScaler(with_std=False).
- Attributes:
- K_fit_rows_array, shape (n_samples,)
Average of each column of kernel matrix
- K_fit_all_float
Average of kernel matrix
Methods
Examples
>>> import cupy as cp >>> from cuml.preprocessing import KernelCenterer >>> from cuml.metrics import pairwise_kernels >>> X = cp.array([[ 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,
Fit KernelCenterer
- Parameters:
- Knumpy array of shape [n_samples, n_samples]
Kernel matrix.
- Returns:
- selfreturns an instance of self.
- 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{array-like, sparse matrix, dataframe} of shape (n_samples, n_features)
- yndarray of shape (n_samples,), default=None
Target values.
- **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]#
Returns a dict of all params owned by this class. If the child class has appropriately overridden the
_get_param_namesmethod and does not need anything other than what is there in this method, then it doesn’t have to override this method
- set_params(**params)[source]#
Accepts a dict of params and updates the corresponding ones owned by this class. If the child class has appropriately overridden the
_get_param_namesmethod and does not need anything other than what is, there in this method, then it doesn’t have to override this method
- transform(K, copy=True)[source]#
Center kernel matrix.
- Parameters:
- Knumpy array of shape [n_samples1, n_samples2]
Kernel matrix.
- copyboolean, optional, default True
Whether a forced copy will be triggered. If copy=False, a copy might be triggered by a conversion.
- Returns:
- K_newnumpy array of shape [n_samples1, n_samples2]