---
name: "mlpack_kpca_KernelPCA"
section: "3"
platform: "linux"
package: "mlpack-doc"
version: "1.0.10-1"
release: "Debian 8 (Jessie)"
platforms: ["linux"]
url: "https://new.carta.tech/mlpack_kpca_KernelPCA"
---

# mlpack\_kpca\_KernelPCA(3)

This class performs kernel principal components analysis (Kernel PCA), for a given kernel.

## NAME

mlpack::kpca::KernelPCA\< KernelType, KernelRule \> -

This class performs kernel principal components analysis (Kernel PCA), for a given kernel.

## SYNOPSIS

### Public Member Functions

**KernelPCA** (const KernelType **kernel**=KernelType(), const bool **centerTransformedData**=false)\
 *Construct the* **KernelPCA** *object, optionally passing a kernel.*  void **Apply** (const arma::mat &data, arma::mat &transformedData, arma::vec &eigval, arma::mat &eigvec, const size\_t newDimension)\
 *Apply Kernel Principal Components Analysis to the provided data set.*  void **Apply** (const arma::mat &data, arma::mat &transformedData, arma::vec &eigval, arma::mat &eigvec)\
 *Apply Kernel Principal Components Analysis to the provided data set.*  void **Apply** (const arma::mat &data, arma::mat &transformedData, arma::vec &eigval)\
 *Apply Kernel Principal Component Analysis to the provided data set.*  void **Apply** (arma::mat &data, const size\_t newDimension)\
 *Apply dimensionality reduction using Kernel Principal Component Analysis to the provided data set.*  bool **CenterTransformedData** () const \
 *Return whether or not the transformed data is centered.*  bool & **CenterTransformedData** ()\
 *Return whether or not the transformed data is centered.*  const KernelType & **Kernel** () const \
 *Get the kernel.*  KernelType & **Kernel** ()\
 *Modify the kernel.*  std::string **ToString** () const \

### Private Attributes

bool **centerTransformedData**\
 *If true, the data will be scaled (by standard deviation) when* **Apply()** *is run.*  KernelType **kernel**\
 *The instantiated kernel.*

## Detailed Description

### template\<typename KernelType, typename KernelRule = NaiveKernelRule\<KernelType\>\>class mlpack::kpca::KernelPCA\< KernelType, KernelRule \>

This class performs kernel principal components analysis (Kernel PCA), for a given kernel.

This is a standard machine learning technique and is well-documented on the Internet and in standard texts. It is often used as a dimensionality reduction technique, and can also be useful in mapping linearly inseparable classes of points to different spaces where they are linearly separable.

The performance of the method is highly dependent on the kernel choice. There are numerous available kernels in the **mlpack::kernel** namespace (see files in mlpack/core/kernels/) and it is easy to write your own; see other implementations for examples.

Definition at line 50 of file kernel\_pca.hpp.

## Constructor & Destructor Documentation

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::KernelPCA (const KernelTypekernel = `KernelType()`, const boolcenterTransformedData = `false`)

Construct the **KernelPCA** object, optionally passing a kernel. Optionally, the transformed data can be centered about the origin; to do this, pass 'true' for centerTransformedData. This will take slightly longer (but not much).

**Parameters:**

*kernel* Kernel to be used for computation. \
 *centerTransformedData* Center transformed data.

## Member Function Documentation

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> void mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::Apply (const arma::mat &data, arma::mat &transformedData, arma::vec &eigval, arma::mat &eigvec, const size\_tnewDimension)

Apply Kernel Principal Components Analysis to the provided data set.

**Parameters:**

*data* Data matrix. \
 *transformedData* Matrix to output results into. \
 *eigval* KPCA eigenvalues will be written to this vector. \
 *eigvec* KPCA eigenvectors will be written to this matrix. \
 *newDimension* New dimension for the dataset.

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> void mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::Apply (const arma::mat &data, arma::mat &transformedData, arma::vec &eigval, arma::mat &eigvec)

Apply Kernel Principal Components Analysis to the provided data set.

**Parameters:**

*data* Data matrix. \
 *transformedData* Matrix to output results into. \
 *eigval* KPCA eigenvalues will be written to this vector. \
 *eigvec* KPCA eigenvectors will be written to this matrix.

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> void mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::Apply (const arma::mat &data, arma::mat &transformedData, arma::vec &eigval)

Apply Kernel Principal Component Analysis to the provided data set.

**Parameters:**

*data* Data matrix. \
 *transformedData* Matrix to output results into. \
 *eigval* KPCA eigenvalues will be written to this vector.

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> void mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::Apply (arma::mat &data, const size\_tnewDimension)

Apply dimensionality reduction using Kernel Principal Component Analysis to the provided data set. The data matrix will be modified in-place. Note that the dimension can be larger than the existing dimension because KPCA works on the kernel matrix, not the covariance matrix. This means the new dimension can be as large as the number of points (columns) in the dataset. Note that if you specify newDimension to be larger than the current dimension of the data (the number of rows), then it's not really 'dimensionality reduction'...

**Parameters:**

*data* Data matrix. \
 *newDimension* New dimension for the dataset.

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> bool mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::CenterTransformedData () const` [inline]`

Return whether or not the transformed data is centered.

Definition at line 125 of file kernel\_pca.hpp.

References mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::centerTransformedData.

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> bool& mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::CenterTransformedData ()` [inline]`

Return whether or not the transformed data is centered.

Definition at line 127 of file kernel\_pca.hpp.

References mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::centerTransformedData.

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> const KernelType& mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::Kernel () const` [inline]`

Get the kernel.

Definition at line 120 of file kernel\_pca.hpp.

References mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::kernel.

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> KernelType& mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::Kernel ()` [inline]`

Modify the kernel.

Definition at line 122 of file kernel\_pca.hpp.

References mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::kernel.

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> std::string mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::ToString () const

## Member Data Documentation

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> bool mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::centerTransformedData` [private]`

If true, the data will be scaled (by standard deviation) when **Apply()** is run.

Definition at line 137 of file kernel\_pca.hpp.

Referenced by mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::CenterTransformedData().

### template\<typename KernelType , typename KernelRule = NaiveKernelRule\<KernelType\>\> KernelType mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::kernel` [private]`

The instantiated kernel.

Definition at line 134 of file kernel\_pca.hpp.

Referenced by mlpack::kpca::KernelPCA\< KernelType, KernelRule \>::Kernel().

## Author

Generated automatically by Doxygen for MLPACK from the source code.
