Click or drag to resize
Accord.NET (logo)

KernelPrincipalComponentAnalysis Class

Kernel Principal Component Analysis.
Inheritance Hierarchy
SystemObject
  Accord.MachineLearningTransformBaseDouble, Double
    Accord.MachineLearningMultipleTransformBaseDouble, Double
      Accord.Statistics.Analysis.BaseBasePrincipalComponentAnalysis
        Accord.Statistics.AnalysisKernelPrincipalComponentAnalysis

Namespace:  Accord.Statistics.Analysis
Assembly:  Accord.Statistics (in Accord.Statistics.dll) Version: 3.8.0
Syntax
[SerializableAttribute]
public class KernelPrincipalComponentAnalysis : BasePrincipalComponentAnalysis, 
	ITransform<double[], double[]>, ICovariantTransform<double[], double[]>, 
	ITransform, IUnsupervisedLearning<MultivariateKernelRegression, double[], double[]>, 
	IMultivariateAnalysis, IAnalysis, IProjectionAnalysis
Request Example View Source

The KernelPrincipalComponentAnalysis type exposes the following members.

Constructors
  NameDescription
Public methodKernelPrincipalComponentAnalysis
Constructs the Kernel Principal Component Analysis.
Public methodKernelPrincipalComponentAnalysis(Double, IKernel) Obsolete.
Constructs the Kernel Principal Component Analysis.
Public methodKernelPrincipalComponentAnalysis(Double, IKernel) Obsolete.
Constructs the Kernel Principal Component Analysis.
Public methodKernelPrincipalComponentAnalysis(Double, IKernel, AnalysisMethod) Obsolete.
Constructs the Kernel Principal Component Analysis.
Public methodKernelPrincipalComponentAnalysis(Double, IKernel, AnalysisMethod) Obsolete.
Constructs the Kernel Principal Component Analysis.
Public methodKernelPrincipalComponentAnalysis(Double, IKernel, AnalysisMethod, Boolean) Obsolete.
Constructs the Kernel Principal Component Analysis.
Public methodKernelPrincipalComponentAnalysis(Double, IKernel, AnalysisMethod, Boolean) Obsolete.
Constructs the Kernel Principal Component Analysis.
Public methodKernelPrincipalComponentAnalysis(IKernel, PrincipalComponentMethod, Boolean, Int32, Boolean)
Constructs the Kernel Principal Component Analysis.
Top
Properties
  NameDescription
Public propertyAllowReversion
Gets or sets a boolean value indicating whether this analysis should store enough information to allow the reversion of the transformation to be computed. Set this to no in case you would like to store the analysis object to disk and you do not need to reverse a transformation after it has been computed.
Public propertyCenter
Gets or sets whether the points should be centered in feature space.
Public propertyComponentMatrix Obsolete.
Gets a matrix whose columns contain the principal components. Also known as the Eigenvectors or loadings matrix.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyComponentProportions
The respective role each component plays in the data set.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyComponents
Gets the Principal Components in a object-oriented structure.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyComponentVectors
Gets a matrix whose columns contain the principal components. Also known as the Eigenvectors or loadings matrix.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyCumulativeProportions
The cumulative distribution of the components proportion role. Also known as the cumulative energy of the principal components.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyEigenvalues
Provides access to the Eigenvalues stored during the analysis.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyExplainedVariance
Gets or sets the amount of explained variance that should be generated by this model. This value will alter the NumberOfOutputs that can be generated by this model.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyKernel
Gets or sets the Kernel used in the analysis.
Public propertyMaximumNumberOfOutputs
Gets the maximum number of outputs (dimensionality of the output vectors) that can be generated by this model.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyMeans
Gets the column mean of the source data given at method construction.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyMethod
Gets or sets the method used by this analysis.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyNumberOfInputs
Gets the number of inputs accepted by the model.
(Inherited from TransformBaseTInput, TOutput.)
Public propertyNumberOfOutputs
Gets or sets the number of outputs (dimensionality of the output vectors) that should be generated by this model.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyOverwrite
Gets or sets whether calculations will be performed overwriting data in the original source matrix, using less memory.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyResult Obsolete.
Gets the resulting projection of the source data given on the creation of the analysis into the space spawned by principal components.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertySingularValues
Provides access to the Singular Values stored during the analysis. If a covariance method is chosen, then it will contain an empty vector.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertySource Obsolete.
Returns the original data supplied to the analysis.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyStandardDeviations
Gets the column standard deviations of the source data given at method construction.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyThreshold Obsolete.
Gets or sets the minimum variance proportion needed to keep a principal component. If set to zero, all components will be kept. Default is 0.001 (all components which contribute less than 0.001 to the variance in the data will be discarded).
Public propertyToken
Gets or sets a cancellation token that can be used to cancel the algorithm while it is running.
(Inherited from BasePrincipalComponentAnalysis.)
Public propertyWhiten
Gets or sets whether the transformation result should be whitened (have unit standard deviation) before it is returned.
(Inherited from BasePrincipalComponentAnalysis.)
Top
Methods
  NameDescription
Public methodCompute Obsolete.
Computes the Kernel Principal Component Analysis algorithm.
Public methodCompute(Int32) Obsolete.
Obsolete.
Protected methodCreateComponents
Creates additional information about principal components.
(Inherited from BasePrincipalComponentAnalysis.)
Public methodEquals
Determines whether the specified object is equal to the current object.
(Inherited from Object.)
Protected methodFinalize
Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.
(Inherited from Object.)
Public methodGetHashCode
Serves as the default hash function.
(Inherited from Object.)
Public methodGetNumberOfComponents
Returns the minimal number of principal components required to represent a given percentile of the data.
(Inherited from BasePrincipalComponentAnalysis.)
Public methodGetType
Gets the Type of the current instance.
(Inherited from Object.)
Public methodLearn(Double) Obsolete.
Learns a model that can map the given inputs to the desired outputs.
Public methodLearn(Double, Double)
Learns a model that can map the given inputs to the desired outputs.
Protected methodMemberwiseClone
Creates a shallow copy of the current Object.
(Inherited from Object.)
Public methodRevert(Double) Obsolete.
Reverts a set of projected data into it's original form. Complete reverse transformation is not always possible and is not even guaranteed to exist.
Public methodRevert(Double, Int32) Obsolete.
Reverts a set of projected data into it's original form. Complete reverse transformation is not always possible and is not even guaranteed to exist.
Public methodRevert(Double, Int32)
Reverts a set of projected data into it's original form. Complete reverse transformation is not always possible and is not even guaranteed to exist.
Public methodToString
Returns a string that represents the current object.
(Inherited from Object.)
Public methodTransform(TInput)
Applies the transformation to a set of input vectors, producing an associated set of output vectors.
(Inherited from MultipleTransformBaseTInput, TOutput.)
Public methodTransform(Double) Obsolete.
Obsolete.
(Inherited from BasePrincipalComponentAnalysis.)
Public methodTransform(Double)
Applies the transformation to an input, producing an associated output.
(Inherited from BasePrincipalComponentAnalysis.)
Public methodTransform(Double, Double)
Projects a given matrix into principal component space.
(Overrides MultipleTransformBaseTInput, TOutputTransform(TInput, TOutput).)
Public methodTransform(Double, Int32) Obsolete.
Projects a given matrix into principal component space.
(Inherited from BasePrincipalComponentAnalysis.)
Public methodTransform(Double, Double)
Applies the transformation to an input, producing an associated output.
(Inherited from BasePrincipalComponentAnalysis.)
Public methodTransform(Double, Int32) Obsolete.
Projects a given matrix into principal component space.
(Inherited from BasePrincipalComponentAnalysis.)
Public methodTransform(Double, Int32) Obsolete.
Projects a given matrix into principal component space.
(Inherited from BasePrincipalComponentAnalysis.)
Top
Fields
  NameDescription
Protected fieldarray
Obsolete
(Inherited from BasePrincipalComponentAnalysis.)
Protected fieldcovarianceMatrix
Obsolete
(Inherited from BasePrincipalComponentAnalysis.)
Protected fieldonlyCovarianceMatrixAvailable
Obsolete
(Inherited from BasePrincipalComponentAnalysis.)
Protected fieldresult
Obsolete
(Inherited from BasePrincipalComponentAnalysis.)
Protected fieldsaveResult
Obsolete
(Inherited from BasePrincipalComponentAnalysis.)
Protected fieldsource
Obsolete
(Inherited from BasePrincipalComponentAnalysis.)
Top
Extension Methods
  NameDescription
Public Extension MethodHasMethod
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.)
Public Extension MethodIsEqual
Compares two objects for equality, performing an elementwise comparison if the elements are vectors or matrices.
(Defined by Matrix.)
Public Extension MethodTo(Type)Overloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Public Extension MethodToTOverloaded.
Converts an object into another type, irrespective of whether the conversion can be done at compile time or not. This can be used to convert generic types to numeric types during runtime.
(Defined by ExtensionMethods.)
Top
Remarks

Kernel principal component analysis (kernel PCA) is an extension of principal component analysis (PCA) using techniques of kernel methods. Using a kernel, the originally linear operations of PCA are done in a reproducing kernel Hilbert space with a non-linear mapping.

This class can also be bound to standard controls such as the DataGridView by setting their DataSource property to the analysis' Components property.

References:

Examples

The example below shows a typical usage of the analysis. We will be replicating the exact same example which can be found on the PrincipalComponentAnalysis documentation page. However, while we will be using a Linear kernel, any other kernel function could have been used.

// Reproducing Lindsay Smith's "Tutorial on Principal Component Analysis"
// using the framework's default method. The tutorial can be found online
// at http://www.sccg.sk/~haladova/principal_components.pdf

// Step 1. Get some data
// ---------------------

double[][] data =
{
    new double[] { 2.5,  2.4 },
    new double[] { 0.5,  0.7 },
    new double[] { 2.2,  2.9 },
    new double[] { 1.9,  2.2 },
    new double[] { 3.1,  3.0 },
    new double[] { 2.3,  2.7 },
    new double[] { 2.0,  1.6 },
    new double[] { 1.0,  1.1 },
    new double[] { 1.5,  1.6 },
    new double[] { 1.1,  0.9 }
};


// Step 2. Subtract the mean
// -------------------------
//   Note: The framework does this automatically. By default, the framework
//   uses the "Center" method, which only subtracts the mean. However, it is
//   also possible to remove the mean *and* divide by the standard deviation
//   (thus performing the correlation method) by specifying "Standardize"
//   instead of "Center" as the AnalysisMethod.

var method = PrincipalComponentMethod.Center; // PrincipalComponentMethod.Standardize


// Step 3. Compute the covariance matrix
// -------------------------------------
//   Note: Accord.NET does not need to compute the covariance
//   matrix in order to compute PCA. The framework uses the SVD
//   method which is more numerically stable, but may require
//   more processing or memory. In order to replicate the tutorial
//   using covariance matrices, please see the next unit test.

// Create the analysis using the selected method
var pca = new KernelPrincipalComponentAnalysis(new Linear(), method);

// Compute it
pca.Learn(data);


// Step 4. Compute the eigenvectors and eigenvalues of the covariance matrix
// -------------------------------------------------------------------------
//   Note: Since Accord.NET uses the SVD method rather than the Eigendecomposition
//   method, the Eigenvalues are computed from the singular values. However, it is
//   not the Eigenvalues themselves which are important, but rather their proportion:

// Those are the expected eigenvalues, in descending order:
double[] eigenvalues = { 1.28402771, 0.0490833989 };

// And this will be their proportion:
double[] proportion = eigenvalues.Divide(eigenvalues.Sum());


Assert.IsTrue(proportion.IsEqual(pca.ComponentProportions, rtol: 1e-9));
Assert.IsTrue(eigenvalues.IsEqual(pca.Eigenvalues.Divide(data.GetLength(0) - 1), rtol: 1e-5));

// Step 5. Deriving the new data set
// ---------------------------------

double[][] actual = pca.Transform(data);

// transformedData shown in pg. 18
double[,] expected = new double[,]
{
    {  0.827970186, -0.175115307 },
    { -1.77758033,   0.142857227 },
    {  0.992197494,  0.384374989 },
    {  0.274210416,  0.130417207 },
    {  1.67580142,  -0.209498461 },
    {  0.912949103,  0.175282444 },
    { -0.099109437, -0.349824698 },
    { -1.14457216,   0.046417258 },
    { -0.438046137,  0.017764629 },
    { -1.22382056,  -0.162675287 },
}.Multiply(-1);

// Everything is correct (up to 8 decimal places)
Assert.IsTrue(expected.IsEqual(actual, atol: 1e-8));

// Finally, we can project all the data
double[][] output1 = pca.Transform(data);

// Or just its first components by setting 
// NumberOfOutputs to the desired components:
pca.NumberOfOutputs = 1;

// And then calling transform again:
double[][] output2 = pca.Transform(data);

// We can also limit to 80% of explained variance:
pca.ExplainedVariance = 0.8;

// And then call transform again:
double[][] output3 = pca.Transform(data);

It is also possible to create a KPCA from a kernel matrix that already exists. The example below shows how this could be accomplished.

// This example shows how to compute KPCA from an already existing kernel matrix. Note that, 
// in most cases, you can just pass your data samples and a choice of kernel function to KPCA 
// and it will compute the kernel matrix internally for you. However, if you have already computed 
// the kernel matrix, you can use the method shown below to avoid computing it twice.

// Let's say those were our original data points
double[][] data =
{
    new double[] { 2.5,  2.4 },
    new double[] { 0.5,  0.7 },
    new double[] { 2.2,  2.9 },
    new double[] { 1.9,  2.2 },
    new double[] { 3.1,  3.0 },
    new double[] { 2.3,  2.7 },
    new double[] { 2.0,  1.6 },
    new double[] { 1.0,  1.1 },
    new double[] { 1.5,  1.6 },
    new double[] { 1.1,  0.9 }
};

// Let's say we have already computed their kernel 
// matrix as part of some previous computation:
double[] mean = data.Mean(dimension: 0);
double[][] x = data.Subtract(mean, dimension: 0);

Linear kernel = new Linear();
double[][] K = kernel.ToJagged(x);


// Assuming that we already have the kernel matrix K at this point, we can create the KPCA as
var pca = new KernelPrincipalComponentAnalysis(kernel, PrincipalComponentMethod.KernelMatrix);

// Compute it
pca.Learn(K); // note: we pass the kernel matrix instead of the data points

// Those are the expected eigenvalues, in descending order:
double[] eigenvalues = pca.Eigenvalues.Divide(data.GetLength(0) - 1); //  { 1.28402771, 0.0490833989 };

// And this will be their proportion:
double[] proportions = pca.ComponentProportions; // { 0.963181314348646, 0.03681868565135403 };

// We can transform the inputs using
double[][] actual = pca.Transform(K);

// The output should be similar to
double[,] expected = new double[,]
{
    {  0.827970186, -0.175115307 },
    { -1.77758033,   0.142857227 },
    {  0.992197494,  0.384374989 },
    {  0.274210416,  0.130417207 },
    {  1.67580142,  -0.209498461 },
    {  0.912949103,  0.175282444 },
    { -0.099109437, -0.349824698 },
    { -1.14457216,   0.046417258 },
    { -0.438046137,  0.017764629 },
    { -1.22382056,  -0.162675287 },
}.Multiply(-1);



// Now we can transform new data using KPCA by 
// again feeding a kernel matrix manually:
double[][] newData =
{
    new double[] { 2.2,  2.7 },
    new double[] { 1.2,  4.9 },
    new double[] { 1.8,  0.2 },
};

// Subtract the mean before computing a kernel matrix
double[][] y = newData.Subtract(mean, dimension: 0);

// Create the kernel matrix for new data
double[][] newK = kernel.ToJagged2(y, x);

// Transform using the new kernel matrix
double[][] output = pca.Transform(newK);

// Output will be similar to
// output = 
// {
//     new double[] { -0.845161763306007, -0.24880030917481 },
//     new double[] { -1.78468140697569,  -2.47530044148084 },
//     new double[] {  1.26393423496622,   1.15181172492746 }
// };

// We can project to just its first components by 
// setting NumberOfOutputs to the desired components:

pca.NumberOfOutputs = 1;

// And then calling transform again:
double[][] output2 = pca.Transform(newK);

// We can also limit to 80% of explained variance:
pca.ExplainedVariance = 0.8;

// And then call transform again:
double[][] output3 = pca.Transform(newK);
See Also