23 using namespace Eigen;
60 "Transformation matrix (Eigenvectors of covariance matrix).",
63 SG_ADD(&m_eigenvalues_vector,
"eigenvalues_vector",
76 " for determining zero eigenvalues during whitening to avoid numerical issues",
MS_NOT_AVAILABLE);
91 ->get_feature_matrix();
92 int32_t num_vectors = feature_matrix.num_cols;
93 int32_t num_features = feature_matrix.num_rows;
94 SG_INFO(
"num_examples: %ld num_features: %ld \n", num_vectors, num_features)
97 int32_t max_dim_allowed =
CMath::min(num_vectors, num_features);
101 "target dimension should be less or equal to than minimum of N and D")
104 Map<MatrixXd> fmatrix(feature_matrix.matrix, num_features, num_vectors);
107 data_mean = fmatrix.rowwise().sum()/(
float64_t) num_vectors;
108 fmatrix = fmatrix.colwise()-data_mean;
119 MatrixXd cov_mat(num_features, num_features);
120 cov_mat = fmatrix*fmatrix.transpose();
121 cov_mat /= (num_vectors-1);
123 SG_INFO(
"Computing Eigenvalues ... ")
125 SelfAdjointEigenSolver<MatrixXd> eigenSolve =
126 SelfAdjointEigenSolver<MatrixXd>(cov_mat);
127 eigenValues = eigenSolve.eigenvalues().tail(max_dim_allowed);
140 for (int32_t i=num_features-1; i<-1; i++)
151 for (int32_t i=num_features-1; i<-1; i++)
160 SG_INFO(
"Done\nReducing from %i to %i features..", num_features,
num_dim)
168 transformMatrix = eigenSolve.eigenvectors().block(0,
172 for (int32_t i=0; i<
num_dim; i++)
174 if (CMath::fequals_abs<float64_t>(0.0, eigenValues[i+max_dim_allowed-num_dim],
177 SG_WARNING(
"Covariance matrix has almost zero Eigenvalue (ie "
178 "Eigenvalue within a tolerance of %E around 0) at "
179 "dimension %d. Consider reducing its dimension.",
182 transformMatrix.col(i) = MatrixXd::Zero(num_features,1);
186 transformMatrix.col(i) /=
187 CMath::sqrt(eigenValues[i+max_dim_allowed-num_dim]*(num_vectors-1));
195 JacobiSVD<MatrixXd> svd(fmatrix.transpose(), ComputeThinU | ComputeThinV);
198 eigenValues = svd.singularValues();
199 eigenValues = eigenValues.cwiseProduct(eigenValues)/(num_vectors-1);
212 for (int32_t i=0; i<num_features; i++)
223 for (int32_t i=0; i<num_features; i++)
232 SG_INFO(
"Done\nReducing from %i to %i features..", num_features,
num_dim)
238 transformMatrix = svd.matrixV().block(0, 0, num_features,
num_dim);
241 for (int32_t i=0; i<
num_dim; i++)
243 if (CMath::fequals_abs<float64_t>(0.0, eigenValues[i],
246 SG_WARNING(
"Covariance matrix has almost zero Eigenvalue (ie "
247 "Eigenvalue within a tolerance of %E around 0) at "
248 "dimension %d. Consider reducing its dimension.",
251 transformMatrix.col(i) = MatrixXd::Zero(num_features,1);
255 transformMatrix.col(i) /=
CMath::sqrt(eigenValues[i]*(num_vectors-1));
261 fmatrix = fmatrix.colwise()+data_mean;
285 SG_INFO(
"Transforming feature matrix\n")
293 SG_INFO(
"Preprocessing feature matrix\n")
295 VectorXd data_mean = feature_matrix.rowwise().sum()/(
float64_t) num_vectors;
296 feature_matrix = feature_matrix.colwise()-data_mean;
298 feature_matrix.block(0,0,
num_dim,num_vectors) =
299 transform_matrix.transpose()*feature_matrix;
301 SG_INFO(
"Form matrix of target dimension")
302 for (int32_t col=0; col<num_vectors; col++)
304 for (int32_t row=0; row<
num_dim; row++)
305 m.
matrix[col*num_dim+row] = feature_matrix(row,col);
320 SG_INFO(
"Preprocessing feature matrix\n")
322 VectorXd data_mean = feature_matrix.rowwise().sum()/(
float64_t) num_vectors;
323 feature_matrix = feature_matrix.colwise()-data_mean;
325 ret_matrix = transform_matrix.transpose()*feature_matrix;
342 inputVec = inputVec-mean;
343 resultVec = transformMat.transpose()*inputVec;
344 inputVec = inputVec+mean;
void set_memory_mode(EPCAMemoryMode e)
the class DimensionReductionPreprocessor, a base class for preprocessors used to lower the dimensiona...
EPCAMemoryMode m_mem_mode
SGVector< float64_t > m_mean_vector
void set_eigenvalue_zero_tolerance(float64_t eigenvalue_zero_tolerance=1e-15)
SGVector< float64_t > m_eigenvalues_vector
float64_t get_eigenvalue_zero_tolerance() const
virtual SGVector< float64_t > apply_to_feature_vector(SGVector< float64_t > vector)
CPCA(bool do_whitening=false, EPCAMode mode=FIXED_NUMBER, float64_t thresh=1e-6, EPCAMethod method=AUTO, EPCAMemoryMode mem_mode=MEM_REALLOCATE)
SGMatrix< float64_t > m_transformation_matrix
SGVector< float64_t > get_mean()
SGVector< float64_t > get_eigenvalues()
virtual EFeatureClass get_feature_class() const =0
all of classes and functions are contained in the shogun namespace
The class Features is the base class of all feature objects.
EPCAMemoryMode get_memory_mode() const
static float32_t sqrt(float32_t x)
virtual SGMatrix< float64_t > apply_to_feature_matrix(CFeatures *features)
float64_t m_eigenvalue_zero_tolerance
virtual EFeatureType get_feature_type() const =0
SGMatrix< float64_t > get_transformation_matrix()