KernelPrincipalComponentAnalysis Class |
Namespace: Accord.Statistics.Analysis
[SerializableAttribute] public class KernelPrincipalComponentAnalysis : BasePrincipalComponentAnalysis, ITransform<double[], double[]>, ICovariantTransform<double[], double[]>, ITransform, IUnsupervisedLearning<MultivariateKernelRegression, double[], double[]>, IMultivariateAnalysis, IAnalysis, IProjectionAnalysis
The KernelPrincipalComponentAnalysis type exposes the following members.
Name | Description | |
---|---|---|
KernelPrincipalComponentAnalysis |
Constructs the Kernel Principal Component Analysis.
| |
KernelPrincipalComponentAnalysis(Double, IKernel) | Obsolete. Constructs the Kernel Principal Component Analysis. | |
KernelPrincipalComponentAnalysis(Double, IKernel) | Obsolete. Constructs the Kernel Principal Component Analysis. | |
KernelPrincipalComponentAnalysis(Double, IKernel, AnalysisMethod) | Obsolete.
Constructs the Kernel Principal Component Analysis.
| |
KernelPrincipalComponentAnalysis(Double, IKernel, AnalysisMethod) | Obsolete.
Constructs the Kernel Principal Component Analysis.
| |
KernelPrincipalComponentAnalysis(Double, IKernel, AnalysisMethod, Boolean) | Obsolete.
Constructs the Kernel Principal Component Analysis.
| |
KernelPrincipalComponentAnalysis(Double, IKernel, AnalysisMethod, Boolean) | Obsolete.
Constructs the Kernel Principal Component Analysis.
| |
KernelPrincipalComponentAnalysis(IKernel, PrincipalComponentMethod, Boolean, Int32, Boolean) |
Constructs the Kernel Principal Component Analysis.
|
Name | Description | |
---|---|---|
AllowReversion |
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.
| |
Center |
Gets or sets whether the points should be centered in feature space.
| |
ComponentMatrix | Obsolete.
Gets a matrix whose columns contain the principal components. Also known as the Eigenvectors or loadings matrix.
(Inherited from BasePrincipalComponentAnalysis.) | |
ComponentProportions |
The respective role each component plays in the data set.
(Inherited from BasePrincipalComponentAnalysis.) | |
Components |
Gets the Principal Components in a object-oriented structure.
(Inherited from BasePrincipalComponentAnalysis.) | |
ComponentVectors |
Gets a matrix whose columns contain the principal components. Also known as the Eigenvectors or loadings matrix.
(Inherited from BasePrincipalComponentAnalysis.) | |
CumulativeProportions |
The cumulative distribution of the components proportion role. Also known
as the cumulative energy of the principal components.
(Inherited from BasePrincipalComponentAnalysis.) | |
Eigenvalues |
Provides access to the Eigenvalues stored during the analysis.
(Inherited from BasePrincipalComponentAnalysis.) | |
ExplainedVariance |
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.) | |
Kernel |
Gets or sets the Kernel used in the analysis.
| |
MaximumNumberOfOutputs |
Gets the maximum number of outputs (dimensionality of the output vectors)
that can be generated by this model.
(Inherited from BasePrincipalComponentAnalysis.) | |
Means |
Gets the column mean of the source data given at method construction.
(Inherited from BasePrincipalComponentAnalysis.) | |
Method |
Gets or sets the method used by this analysis.
(Inherited from BasePrincipalComponentAnalysis.) | |
NumberOfInputs |
Gets the number of inputs accepted by the model.
(Inherited from TransformBaseTInput, TOutput.) | |
NumberOfOutputs |
Gets or sets the number of outputs (dimensionality of the output vectors)
that should be generated by this model.
(Inherited from BasePrincipalComponentAnalysis.) | |
Overwrite |
Gets or sets whether calculations will be performed overwriting
data in the original source matrix, using less memory.
(Inherited from BasePrincipalComponentAnalysis.) | |
Result | 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.) | |
SingularValues |
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.) | |
Source | Obsolete.
Returns the original data supplied to the analysis.
(Inherited from BasePrincipalComponentAnalysis.) | |
StandardDeviations |
Gets the column standard deviations of the source data given at method construction.
(Inherited from BasePrincipalComponentAnalysis.) | |
Threshold | 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).
| |
Token |
Gets or sets a cancellation token that can be used
to cancel the algorithm while it is running.
(Inherited from BasePrincipalComponentAnalysis.) | |
Whiten |
Gets or sets whether the transformation result should be whitened
(have unit standard deviation) before it is returned.
(Inherited from BasePrincipalComponentAnalysis.) |
Name | Description | |
---|---|---|
Compute | Obsolete.
Computes the Kernel Principal Component Analysis algorithm.
| |
Compute(Int32) | Obsolete.
Obsolete.
| |
CreateComponents |
Creates additional information about principal components.
(Inherited from BasePrincipalComponentAnalysis.) | |
Equals | Determines whether the specified object is equal to the current object. (Inherited from Object.) | |
Finalize | Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.) | |
GetHashCode | Serves as the default hash function. (Inherited from Object.) | |
GetNumberOfComponents |
Returns the minimal number of principal components
required to represent a given percentile of the data.
(Inherited from BasePrincipalComponentAnalysis.) | |
GetType | Gets the Type of the current instance. (Inherited from Object.) | |
Learn(Double) | Obsolete.
Learns a model that can map the given inputs to the desired outputs.
| |
Learn(Double, Double) |
Learns a model that can map the given inputs to the desired outputs.
| |
MemberwiseClone | Creates a shallow copy of the current Object. (Inherited from Object.) | |
Revert(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.
| |
Revert(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.
| |
Revert(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.
| |
ToString | Returns a string that represents the current object. (Inherited from Object.) | |
Transform(TInput) |
Applies the transformation to a set of input vectors,
producing an associated set of output vectors.
(Inherited from MultipleTransformBaseTInput, TOutput.) | |
Transform(Double) | Obsolete.
Obsolete.
(Inherited from BasePrincipalComponentAnalysis.) | |
Transform(Double) |
Applies the transformation to an input, producing an associated output.
(Inherited from BasePrincipalComponentAnalysis.) | |
Transform(Double, Double) |
Projects a given matrix into principal component space.
(Overrides MultipleTransformBaseTInput, TOutputTransform(TInput, TOutput).) | |
Transform(Double, Int32) | Obsolete.
Projects a given matrix into principal component space.
(Inherited from BasePrincipalComponentAnalysis.) | |
Transform(Double, Double) |
Applies the transformation to an input, producing an associated output.
(Inherited from BasePrincipalComponentAnalysis.) | |
Transform(Double, Int32) | Obsolete.
Projects a given matrix into principal component space.
(Inherited from BasePrincipalComponentAnalysis.) | |
Transform(Double, Int32) | Obsolete.
Projects a given matrix into principal component space.
(Inherited from BasePrincipalComponentAnalysis.) |
Name | Description | |
---|---|---|
array | Obsolete (Inherited from BasePrincipalComponentAnalysis.) | |
covarianceMatrix | Obsolete (Inherited from BasePrincipalComponentAnalysis.) | |
onlyCovarianceMatrixAvailable | Obsolete (Inherited from BasePrincipalComponentAnalysis.) | |
result | Obsolete (Inherited from BasePrincipalComponentAnalysis.) | |
saveResult | Obsolete (Inherited from BasePrincipalComponentAnalysis.) | |
source | Obsolete (Inherited from BasePrincipalComponentAnalysis.) |
Name | Description | |
---|---|---|
HasMethod |
Checks whether an object implements a method with the given name.
(Defined by ExtensionMethods.) | |
IsEqual |
Compares two objects for equality, performing an elementwise
comparison if the elements are vectors or matrices.
(Defined by Matrix.) | |
To(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.) | |
ToT | 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.) |
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:
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);