public static void main ( String [ ] args ) throws Exception { String indexLocation = args [ 0 ] ; int numTrainVectors = Integer . parseInt ( args [ 1 ] ) ; int vectorLength = Integer . parseInt ( args [ 2 ] ) ; int numPrincipalComponents = Integer . parseInt ( args [ 3 ] ) ; boolean whitening = true ; boolean compact = false ; PCA pca = new PCA ( numPrincipalComponents , numTrainVectors , vectorLength , whitening ) ; pca . setCompact ( compact ) ; // load the vectors into the PCA class Linear vladArray = new Linear ( vectorLength , numTrainVectors , true , indexLocation , false , true , 0 ) ; for ( int i = 0 ; i < numTrainVectors ; i ++ ) { pca . addSample ( vladArray . getVector ( i ) ) ; } // now we are able to perform SVD and compute the eigenvectors System . out . println ( "PCA computation started!" ) ; long start = System . currentTimeMillis ( ) ; pca . computeBasis ( ) ; long end = System . currentTimeMillis ( ) ; System . out . println ( "PCA computation completed in " + ( end - start ) + " ms" ) ; // now we can save the PCA matrix in a file String PCAfile = indexLocation + "pca_" + numTrainVectors + "_" + numPrincipalComponents + "_" + ( end - start ) + "ms.txt" ; pca . savePCAToFile ( PCAfile ) ; }
This method can be used to learn a PCA projection matrix .