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!" ) ; } BufferedWriter out = new BufferedWriter ( new FileWriter ( PCAFileName ) ) ; // the first line of the file contains the training sample means per component for ( int i = 0 ; i < sampleSize - 1 ; i ++ ) { out . write ( means . get ( i ) + " " ) ; } out . write ( means . get ( sampleSize - 1 ) + "\n" ) ; // the second line of the file contains the eigenvalues in descending order for ( int i = 0 ; i < numComponents - 1 ; i ++ ) { out . write ( W . get ( i , i ) + " " ) ; } out . write ( W . get ( numComponents - 1 , numComponents - 1 ) + "\n" ) ; // the next lines of the file contain the eigenvectors in descending eigenvalue order for ( int i = 0 ; i < numComponents ; i ++ ) { for ( int j = 0 ; j < sampleSize - 1 ; j ++ ) { out . write ( V_t . get ( i , j ) + " " ) ; } out . write ( V_t . get ( i , sampleSize - 1 ) + "\n" ) ; } out . close ( ) ; }
Writes the means the eigenvalues and the PCA matrix to a text file . The 1st row of the file contains the training sample means per component the 2nd row contains the eigenvalues in descending order and subsequent rows contain contain the eigenvectors in descending eigenvalue order .