Changeset 14


Ignore:
Timestamp:
Jun 25, 2003, 10:56:33 AM (18 years ago)
Author:
daniel
Message:

One correction in the projection-method.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/PCA.cc

    r13 r14  
    9393       {
    9494  centered[ i ] = temp.get( i, 0 ) - meanvalues_[ i ];
     95  //cout << temp.get( i, 0 ) << " " << meanvalues_[ i ] << " " << centered[ i ] << endl;
    9596       }
    9697
    9798     thep_gsl_api::vector proj( eigenvectors_ * centered );
    98       for( size_t i = 0; i < samples.cols(); ++i )
     99      for( size_t i = 0; i < Ncol; ++i )
    99100       {
    100101  projs.set( i, j,  proj[ i ] );
     
    136137{
    137138  cout << "1. Print original matrix A = \n";//Testing row-centering of matrix\n";
    138   thep_gsl_api::matrix A( 3, 3 );
    139   for( size_t i = 0; i < 3; ++i )
     139  thep_gsl_api::matrix A( 4, 3 );
     140  for( size_t i = 0; i < 4; ++i )
    140141    {
    141142     for( size_t j = 0; j < 3; ++j )
     
    163164  // The eigenvalues and eigenvectors are
    164165  // from matlab using eig(A'A)/3
    165   double answer_lambda[ 3 ] = { 1.8908, 0.0327, 0 };
    166   double answer_eigenvec[ 3 ][ 3 ] =
    167     {
    168       { 0.4763, -0.6738, 0.5649  },
    169       { 0.8770, 0.3182,  -0.3599 },
    170       { 0.0627, 0.6669 , 0.7425  },
    171     };
    172 
    173   double ERROR_TOL = 0.0001;
    174 
    175   // error eigenvalues
    176   cout << "abs( lambda(i) - lambda_matlab(i) ) ) = \n( ";
    177   double max_err = 0;
    178   for( size_t i = 0; i < 3; ++i )
    179     {
    180      double err = fabs( eigenvalues_[ i ] - answer_lambda[ i ] );
    181      if( err > max_err ) max_err = err;
    182      cout << err << " ";
    183     }
    184   cout << ")\n";
    185 
    186   cerr << "max_error = " << max_err << ", to be compared with ERROR_TOL = "
    187        << ERROR_TOL << endl;
    188   if( max_err < ERROR_TOL )
    189     {
    190      cout << "ok, compared with precision in given matlab-references!\n";
    191     }
    192   else
    193     {
    194      cerr << "Not ok, error to big!\n";
    195      return false;
    196     }
    197 
    198   // error eigenvectors
    199   max_err = 0;
    200   for( size_t i = 0; i < 3; ++i )
    201     {
    202      cout << "err_eigvec( " << i << " ) = \n";
    203      for( size_t j = 0; j < 3; ++j )
    204        {
    205   double err = fabs( fabs( eigenvectors_.get( i, j ) ) - fabs( answer_eigenvec[ i ][ j ] ) );
    206   if( err > max_err ) max_err = err;       
    207   cout << err << endl;
    208        }
    209      cout << ")\n\n";
    210     }
    211 
    212   cerr << "max_error = " << max_err << ", to be compared with ERROR_TOL = "
    213        << ERROR_TOL << endl;
    214   if( max_err < ERROR_TOL )
    215     {
    216      cout << "ok, compared with precision in given matlab-references!\n";
    217     }
    218   else
    219     {
    220      cerr << "Not ok, error to big!\n";
    221      return false;
    222     }
     166//   double answer_lambda[ 3 ] = { 1.8908, 0.0327, 0 };
     167//   double answer_eigenvec[ 3 ][ 3 ] =
     168//     {
     169//       { 0.4763, -0.6738, 0.5649  },
     170//       { 0.8770, 0.3182,  -0.3599 },
     171//       { 0.0627, 0.6669 , 0.7425  },
     172//     };
     173
     174//   double ERROR_TOL = 0.0001;
     175
     176//   // error eigenvalues
     177//   cout << "abs( lambda(i) - lambda_matlab(i) ) ) = \n( ";
     178//   double max_err = 0;
     179//   for( size_t i = 0; i < 3; ++i )
     180//     {
     181//      double err = fabs( eigenvalues_[ i ] - answer_lambda[ i ] );
     182//      if( err > max_err ) max_err = err;
     183//      cout << err << " ";
     184//     }
     185//   cout << ")\n";
     186
     187//   cerr << "max_error = " << max_err << ", to be compared with ERROR_TOL = "
     188//        << ERROR_TOL << endl;
     189//   if( max_err < ERROR_TOL )
     190//     {
     191//      cout << "ok, compared with precision in given matlab-references!\n";
     192//     }
     193//   else
     194//     {
     195//      cerr << "Not ok, error to big!\n";
     196//      return false;
     197//     }
     198
     199//   // error eigenvectors
     200//   max_err = 0;
     201//   for( size_t i = 0; i < 3; ++i )
     202//     {
     203//      cout << "err_eigvec( " << i << " ) = \n";
     204//      for( size_t j = 0; j < 3; ++j )
     205//        {
     206// double err = fabs( fabs( eigenvectors_.get( i, j ) ) - fabs( answer_eigenvec[ i ][ j ] ) );
     207// if( err > max_err ) max_err = err;       
     208// cout << err << endl;
     209//        }
     210//      cout << ")\n\n";
     211//     }
     212
     213//   cerr << "max_error = " << max_err << ", to be compared with ERROR_TOL = "
     214//        << ERROR_TOL << endl;
     215//   if( max_err < ERROR_TOL )
     216//     {
     217//      cout << "ok, compared with precision in given matlab-references!\n";
     218//     }
     219//   else
     220//     {
     221//      cerr << "Not ok, error to big!\n";
     222//      return false;
     223//     }
    223224   
    224225  cout << "\nThe following projection is obtained:\n";
    225   cout << this->projection( A ) << endl;
    226 
     226  cout << this->projection( A_ ) << endl;
     227 
    227228
    228229  return true;
  • trunk/src/PCA.h

    r13 r14  
    1919{
    2020  /**
    21      Class performing PCA using SVD.
     21     Class performing PCA using SVD. This class assumes that
     22     the columns corresponds to the dimenension of the problem.
     23     That means if data has dimension NxM (M=columns) the number
     24     of principal-axes will equal M-1. When projecting data into
     25     this space, all Nx1 vectors will have dimension Mx1. Hence
     26     the projection will have dimension MxM where each column is
     27     a point in the new space. Also, it assumes that M>N. The
     28     opposite problem is yet not implemented. However, could easily
     29     be done by using that, if A=input-data=MxN, then SVM(A)->A=USV,
     30     and SVM(A')->A'=VSU. So by changing eigenvectors=U to
     31     eigenvectors=V and rewrite the projection-method in terms of columns
     32     instead of rows ...
    2233  */
    2334 
Note: See TracChangeset for help on using the changeset viewer.