Skip to content

Commit

Permalink
Small fix in the PCA class: the pca matrix should not be whitened before
Browse files Browse the repository at this point in the history
saving the pca result in a file.
  • Loading branch information
lefman committed Dec 13, 2013
1 parent f4208bd commit aeb22bc
Showing 1 changed file with 13 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/gr/iti/mklab/visual/dimreduction/PCA.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,8 @@ public class PCA {
/** whether to compute the SVD in compact form, false by default **/
private boolean compact = false;

private boolean isPcaInitialized = false;

/**
* Constructor. Whitening is not applied.
*
Expand Down Expand Up @@ -171,12 +173,6 @@ public void computeBasis() {
// strip off unneeded components and find the basis
V_t.reshape(numComponents, sampleSize, true);

// if whitening is true then whiten the PCA matrix V_t by multiplying it with W
if (doWhitening) {
DenseMatrix64F V_t_w = new DenseMatrix64F(numComponents, sampleSize);
CommonOps.mult(W, V_t, V_t_w);
V_t = V_t_w;
}
}

/**
Expand All @@ -186,8 +182,13 @@ public void computeBasis() {
* @param sampleData
* Sample space vector
* @return Eigen space projected vector
* @throws Exception
*/
public double[] sampleToEigenSpace(double[] sampleData) {
public double[] sampleToEigenSpace(double[] sampleData) throws Exception {
if (!isPcaInitialized) {
// initiallize PCA correctly!
throw new Exception("PCA is not correctly initiallized!");
}
if (sampleData.length != sampleSize) {
throw new IllegalArgumentException("Unexpected vector length!");
}
Expand Down Expand Up @@ -215,6 +216,9 @@ public double[] sampleToEigenSpace(double[] sampleData) {
* @throws Exception
*/
public void savePCAToFile(String PCAFileName) throws Exception {
if (isPcaInitialized) {
throw new Exception("Cannot save, PCA is initialized!");
}
if (V_t == null) {
throw new Exception("Cannot save to file, PCA matrix is null!");
}
Expand Down Expand Up @@ -304,6 +308,8 @@ public void loadPCAFromFile(String PCAFileName) throws Exception {
}

in.close();

isPcaInitialized = true;
}

public void setCompact(boolean compact) {
Expand Down

0 comments on commit aeb22bc

Please sign in to comment.