Changeset 829
- Timestamp:
- Mar 24, 2007, 3:30:28 PM (17 years ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/pca_test.cc
r680 r829 38 38 for( size_t j = 0; j < 4; ++j ) 39 39 A(i,j)= sin( static_cast<double>(i+j+2+(i+1)*(j+1)) ); 40 40 A.transpose(); 41 41 utility::PCA pca(A); 42 42 43 // Print only matrix that is row-centered as PCA ( process() )44 // will do that before calculation45 pca.process_transposed_problem();46 43 return 0; 47 44 } -
trunk/yat/utility/PCA.cc
r792 r829 36 36 37 37 PCA::PCA(const utility::matrix& A) 38 : A_(A) , process_(false), explained_calc_(false)38 : A_(A) 39 39 { 40 process(); 40 41 } 41 42 42 43 43 utility::vector PCA::get_eigenvector(size_t i) const44 const utility::vector& PCA::eigenvalues(void) const 44 45 { 45 return utility::vector(eigenvectors_,i);46 return eigenvalues_; 46 47 } 47 48 48 49 49 double PCA::get_eigenvalue(size_t i) const50 const utility::matrix& PCA::eigenvectors(void) const 50 51 { 51 return eigenv alues_[i];52 return eigenvectors_; 52 53 } 53 54 … … 55 56 void PCA::process(void) 56 57 { 57 process_ = true;58 58 // Row-center the data matrix 59 59 utility::matrix A_center( A_.rows(), A_.columns() ); … … 89 89 90 90 91 /* 91 92 void PCA::process_transposed_problem(void) 92 93 { 93 process_ = true;94 94 // Row-center the data matrix 95 95 utility::matrix A_center( A_.rows(), A_.columns() ); … … 132 132 } 133 133 } 134 135 136 // This function will row-center the matrix A_, 137 // that is, A_ = A_ - M, where M is a matrix 138 // with the meanvalues of each row 139 void PCA::row_center(utility::matrix& A_center) 140 { 141 meanvalues_.clone(utility::vector(A_.rows())); 142 utility::vector A_row_sum(A_.rows()); 143 for (size_t i=0; i<A_row_sum.size(); ++i) 144 A_row_sum(i)=utility::sum(utility::vector(A_,i)); 145 for( size_t i = 0; i < A_center.rows(); ++i ) { 146 meanvalues_[i] = A_row_sum(i) / static_cast<double>(A_.columns()); 147 for( size_t j = 0; j < A_center.columns(); ++j ) 148 A_center(i,j) = A_(i,j) - meanvalues_(i); 149 } 150 } 134 */ 151 135 152 136 … … 172 156 173 157 158 /* 174 159 utility::matrix 175 160 PCA::projection_transposed(const utility::matrix& samples) const … … 192 177 return projs; 193 178 } 179 */ 194 180 195 181 196 void PCA::calculate_explained_intensity(void) 182 // This function will row-center the matrix A_, 183 // that is, A_ = A_ - M, where M is a matrix 184 // with the meanvalues of each row 185 void PCA::row_center(utility::matrix& A_center) 197 186 { 198 size_t DIM = eigenvalues_.size(); 199 explained_intensity_.clone(utility::vector( DIM )); 200 double sum = 0; 201 for( size_t i = 0; i < DIM; ++i ) 202 sum += eigenvalues_[ i ]; 203 double exp_sum = 0; 204 for( size_t i = 0; i < DIM; ++i ) { 205 exp_sum += eigenvalues_[ i ]; 206 explained_intensity_[ i ] = exp_sum / sum ; 187 meanvalues_.clone(utility::vector(A_.rows())); 188 utility::vector A_row_sum(A_.rows()); 189 for (size_t i=0; i<A_row_sum.size(); ++i) 190 A_row_sum(i)=utility::sum(utility::vector(A_,i)); 191 for( size_t i = 0; i < A_center.rows(); ++i ) { 192 meanvalues_[i] = A_row_sum(i) / static_cast<double>(A_.columns()); 193 for( size_t j = 0; j < A_center.columns(); ++j ) 194 A_center(i,j) = A_(i,j) - meanvalues_(i); 207 195 } 208 196 } 209 197 210 211 double PCA::get_explained_intensity( const size_t& k )212 {213 if( !explained_calc_ ) {214 this->calculate_explained_intensity();215 explained_calc_ = true;216 }217 return explained_intensity_[ k ];218 }219 220 221 198 }}} // of namespace utility, yat, and theplu -
trunk/yat/utility/PCA.h
r767 r829 55 55 56 56 /** 57 Will perform PCA according to the following scheme: \n58 1: Rowcenter A \n59 2: SVD(A) --> USV' \n60 3: Calculate eigenvalues according to \n61 \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n62 4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n63 */64 void process(void);65 66 /**67 57 If M<N use this method instead. Using the same format as before 68 58 where rows in the matrix corresponds to the dimensional coordinate. … … 71 61 projection_transposed() method. 72 62 */ 73 void process_transposed_problem(void);63 // void process_transposed_problem(void); 74 64 75 65 /** 76 @return Eigenvector \a i. 66 \brief Returns eigenvalues in a utility::vector. 67 68 \return A const reference to the internal vector containing all 69 eigenvalues. 77 70 */ 78 utility::vector get_eigenvector(size_t i) const;71 const utility::vector& eigenvalues(void) const; 79 72 80 73 /** 81 Returns eigenvalues to covariance matrix 82 \f$ C = \frac{1}{N^2}A^TA \f$ 74 \brief Get all eigenvectors in a utility::matrix. 75 76 \return A const reference to the internal matrix containing all 77 eigenvectors. 83 78 */ 84 double get_eigenvalue(size_t i) const; 85 86 /** 87 Returns the explained intensity of component \a K \n 88 \f$I = \frac{ \sum^{K}_{i=1} \lambda_i }{ \sum^{N}_{j=1} \lambda_j }\f$ \n 89 where \f$N\f$ is the dimension 90 */ 91 double get_explained_intensity( const size_t& k ); 79 const utility::matrix& eigenvectors(void) const; 92 80 93 81 /** … … 104 92 process_transposed_problem(). 105 93 */ 106 utility::matrix projection_transposed( const utility::matrix& ) const;94 // utility::matrix projection_transposed( const utility::matrix& ) const; 107 95 108 96 109 97 private: 110 utility::matrix A_; 111 utility::matrix eigenvectors_; 112 utility::vector eigenvalues_; 113 utility::vector explained_intensity_; 114 utility::vector meanvalues_; 115 bool process_, explained_calc_; 116 98 99 /** 100 Will perform PCA according to the following scheme: \n 101 1: Rowcenter A \n 102 2: SVD(A) --> USV' \n 103 3: Calculate eigenvalues according to \n 104 \f$ \lambda_{ii} = s_{ii}/N_{rows} \f$ \n 105 4: Sort eigenvectors (from matrix V) according to descending eigenvalues\n 106 */ 107 void process(void); 108 117 109 /** 118 110 Private function that will row-center the matrix A, … … 122 114 void row_center( utility::matrix& A_center ); 123 115 124 /** 125 Private function that will calculate the explained 126 intensity 127 */ 128 void calculate_explained_intensity(void); 129 }; // class PCA 130 116 utility::matrix A_; 117 utility::vector eigenvalues_; 118 utility::matrix eigenvectors_; 119 utility::vector meanvalues_; 120 }; 131 121 132 122 }}} // of namespace utility, yat, and theplu
Note: See TracChangeset
for help on using the changeset viewer.