Much more satisfying than my earlier attempt to do this by hand, I’m actually building the tools that will eventually power TagShadow. I’m step by step implementing wikipedia’s instructions on Computing PCA using the covariance method. I fully understand the folly of trusting wikipedia this closely, but I promise to compare against other sources.
package matrixtest;
import Jama.*;
/**
*
* @author MentatJack
*/
public class PCA {
public static Matrix meanVector(Matrix X){
Matrix u = new Matrix(X.getRowDimension(),1);
double temp;
double x[][] = X.getArray();
for(int i = 0; i < x.length;i++){
temp = 0;
for (int j=0; j < x[0].length;j++){
temp += x[i][j];
}
u.set(i,0,temp/x.length);
}
return u;
}
public static Matrix getCovariance(Matrix M){
Matrix h = new Matrix(1,M.getColumnDimension(),1);
Matrix u = meanVector(M);
Matrix B = M.minus(u.times(h));
return B.times(B.transpose());
}
}
Next I have to sort the eigenvectors…
Posted in misc Tagged: jama, java, matrix, PCA, tagShadow