24 using namespace Eigen;
61 "Transformation matrix (Eigenvectors of covariance matrix).",
64 SG_ADD(&m_eigenvalues_vector,
"eigenvalues_vector",
77 " for determining zero eigenvalues during whitening to avoid numerical issues",
MS_NOT_AVAILABLE);
92 ->get_feature_matrix();
93 int32_t num_vectors = feature_matrix.num_cols;
94 int32_t num_features = feature_matrix.num_rows;
95 SG_INFO(
"num_examples: %ld num_features: %ld \n", num_vectors, num_features)
98 int32_t max_dim_allowed =
CMath::min(num_vectors, num_features);
102 "target dimension should be less or equal to than minimum of N and D")
105 Map<MatrixXd> fmatrix(feature_matrix.matrix, num_features, num_vectors);
108 data_mean = fmatrix.rowwise().sum()/(
float64_t) num_vectors;
109 fmatrix = fmatrix.colwise()-data_mean;
120 MatrixXd cov_mat(num_features, num_features);
121 cov_mat = fmatrix*fmatrix.transpose();
122 cov_mat /= (num_vectors-1);
124 SG_INFO(
"Computing Eigenvalues ... ")
126 SelfAdjointEigenSolver<MatrixXd> eigenSolve =
127 SelfAdjointEigenSolver<MatrixXd>(cov_mat);
128 eigenValues = eigenSolve.eigenvalues().tail(max_dim_allowed);
141 for (int32_t i=num_features-1; i<-1; i++)
152 for (int32_t i=num_features-1; i<-1; i++)
161 SG_INFO(
"Done\nReducing from %i to %i features..", num_features,
num_dim)
169 transformMatrix = eigenSolve.eigenvectors().block(0,
173 for (int32_t i=0; i<
num_dim; i++)
175 if (CMath::fequals_abs<float64_t>(0.0, eigenValues[i+max_dim_allowed-num_dim],
178 SG_WARNING(
"Covariance matrix has almost zero Eigenvalue (ie "
179 "Eigenvalue within a tolerance of %E around 0) at "
180 "dimension %d. Consider reducing its dimension.",
183 transformMatrix.col(i) = MatrixXd::Zero(num_features,1);
187 transformMatrix.col(i) /=
188 CMath::sqrt(eigenValues[i+max_dim_allowed-num_dim]*(num_vectors-1));
196 JacobiSVD<MatrixXd> svd(fmatrix.transpose(), ComputeThinU | ComputeThinV);
199 eigenValues = svd.singularValues();
200 eigenValues = eigenValues.cwiseProduct(eigenValues)/(num_vectors-1);
213 for (int32_t i=0; i<num_features; i++)
224 for (int32_t i=0; i<num_features; i++)
233 SG_INFO(
"Done\nReducing from %i to %i features..", num_features,
num_dim)
239 transformMatrix = svd.matrixV().block(0, 0, num_features,
num_dim);
242 for (int32_t i=0; i<
num_dim; i++)
244 if (CMath::fequals_abs<float64_t>(0.0, eigenValues[i],
247 SG_WARNING(
"Covariance matrix has almost zero Eigenvalue (ie "
248 "Eigenvalue within a tolerance of %E around 0) at "
249 "dimension %d. Consider reducing its dimension.",
252 transformMatrix.col(i) = MatrixXd::Zero(num_features,1);
256 transformMatrix.col(i) /=
CMath::sqrt(eigenValues[i]*(num_vectors-1));
262 fmatrix = fmatrix.colwise()+data_mean;
286 SG_INFO(
"Transforming feature matrix\n")
294 SG_INFO(
"Preprocessing feature matrix\n")
296 VectorXd data_mean = feature_matrix.rowwise().sum()/(
float64_t) num_vectors;
297 feature_matrix = feature_matrix.colwise()-data_mean;
299 feature_matrix.block(0,0,
num_dim,num_vectors) =
300 transform_matrix.transpose()*feature_matrix;
302 SG_INFO(
"Form matrix of target dimension")
303 for (int32_t col=0; col<num_vectors; col++)
305 for (int32_t row=0; row<
num_dim; row++)
306 m.
matrix[col*num_dim+row] = feature_matrix(row,col);
321 SG_INFO(
"Preprocessing feature matrix\n")
323 VectorXd data_mean = feature_matrix.rowwise().sum()/(
float64_t) num_vectors;
324 feature_matrix = feature_matrix.colwise()-data_mean;
326 ret_matrix = transform_matrix.transpose()*feature_matrix;
343 inputVec = inputVec-mean;
344 resultVec = transformMat.transpose()*inputVec;
345 inputVec = inputVec+mean;
385 #endif // HAVE_EIGEN3
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()