Pricinipal Component Analysis: The Java Iteration

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];
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…

~ by mentatjack on September 12, 2009.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: