org.openimaj.math.matrix.algorithm.pca

Class PrincipalComponentAnalysis

• java.lang.Object
• org.openimaj.math.matrix.algorithm.pca.PrincipalComponentAnalysis
• Field Detail

• basis

protected Jama.Matrix basis
• mean

protected double[] mean
• eigenvalues

protected double[] eigenvalues
• Method Detail

• learnBasis

public void learnBasis(List<double[]> data)
Learn the principal components of the given data list. Each item corresponds to an observation with the number of dimensions equal to the length of the array.
Parameters:
data - the data
• learnBasis

public void learnBasis(double[][] data)
Learn the principal components of the given data array. Each row corresponds to an observation with the number of dimensions equal to the number of columns.
Parameters:
data - the data
• learnBasis

public void learnBasis(Jama.Matrix data)
Learn the principal components of the given data matrix. Each row corresponds to an observation with the number of dimensions equal to the number of columns.
Parameters:
data - the data
• learnBasisNorm

protected abstract void learnBasisNorm(Jama.Matrix norm)
Learn the PCA basis from the centered data provided. Each row corresponds to an observation with the number of dimensions equal to the number of columns.
Parameters:
norm -
• buildNormalisedDataMatrix

protected Jama.Matrix buildNormalisedDataMatrix(Jama.Matrix m)
Zero-centre the data matrix and return a copy
Parameters:
data - the data matrix
Returns:
the normalised data
• getBasis

public Jama.Matrix getBasis()
Get the principal components. The principal components are the column vectors of the returned matrix.
Returns:
the principal components
• getPrincipalComponent

public double[] getPrincipalComponent(int index)
Get a specific principle component vector as a double array. The returned array contains a copy of the data.
Parameters:
index - the index of the principle component
Returns:
the principle component
• getEigenVectors

public Jama.Matrix getEigenVectors()
Get the principal components. The principal components are the column vectors of the returned matrix. Syntactic sugar for getBasis()
Returns:
the principal components
• getEigenValues

public double[] getEigenValues()
Returns:
the eigen values corresponding to the principal components
• getEigenValue

public double getEigenValue(int i)
Get the eigen value corresponding to the ith principal component.
Parameters:
i - the index of the component
Returns:
the eigen value corresponding to the principal component
• getCumulativeEnergy

public double getCumulativeEnergy(int i)
Get the cumulative energy of the ith principal component
Parameters:
i - the index of the component
Returns:
the cumulative energy of the component
• getCumulativeEnergies

public double[] getCumulativeEnergies()
Get the cumulative energies of each principal component
Returns:
the cumulative energies of the components
• getMean

public double[] getMean()
Returns:
The mean values
• selectSubset

public void selectSubset(int n)
Select a subset of the principal components. Calling this method throws away any extra basis vectors and eigenvalues.
Parameters:
n -
• selectSubsetEnergyThreshold

public void selectSubsetEnergyThreshold(double threshold)
Select a subset of the principal components such that all remaining components have a cumulative energy less than the given value. Calling this method throws away any extra basis vectors and eigenvalues.
Parameters:
threshold - threshold on the cumulative energy.
• selectSubsetPercentageEnergy

public void selectSubsetPercentageEnergy(double percentage)
Select a subset of the principal components such that all remaining components have a certain percentage cumulative energy of the total. The percentage is calculated relative to the total energy of the eigenvalues. Bear in mind that if not all the eigenvalues were calculated, or if some have previously been removed through selectSubset(int), selectSubsetEnergyThreshold(double) or selectSubsetPercentageEnergy(double), then the percentage calculation only factors in the remaining eigenvalues that are available to it. Calling this method throws away any extra basis vectors and eigenvalues.
Parameters:
percentage - percentage of the total cumulative energy to retain [0..1].
• generate

public double[] generate(double[] scalings)
Generate a new "observation" as a linear combination of the principal components (PC): mean + PC * scaling. If the scaling vector is shorter than the number of components, it will be zero-padded. If it is longer, it will be truncated.
Parameters:
scalings - the weighting for each PC
Returns:
generated observation
• project

public Jama.Matrix project(Jama.Matrix m)
Project a matrix of row vectors by the basis. The vectors are normalised by subtracting the mean and then multiplied by the basis. The returned matrix has a row for each vector.
Parameters:
m - the vector to project
Returns:
projected vectors
• project

public double[] project(double[] vector)
Project a vector by the basis. The vector is normalised by subtracting the mean and then multiplied by the basis.
Parameters:
vector - the vector to project
Returns:
projected vector
• getStandardDeviations

public double[] getStandardDeviations()
Get the standard deviations (sqrt of eigenvalues) of the principal components.
Returns:
vector of standard deviations
• getStandardDeviations

public double[] getStandardDeviations(int n)
Get the standard deviations (sqrt of eigenvalues) of the n top principal components.
Parameters:
n - number of principal components
Returns:
vector of standard deviations