26 using namespace shogun;
27 using namespace Eigen;
64 "Transformation matrix (Eigenvectors of covariance matrix).",
67 SG_ADD(&m_eigenvalues_vector,
"eigenvalues_vector",
80 " for determining zero eigenvalues during whitening to avoid numerical issues",
MS_NOT_AVAILABLE);
95 ->get_feature_matrix();
96 int32_t num_vectors = feature_matrix.num_cols;
97 int32_t num_features = feature_matrix.num_rows;
98 SG_INFO(
"num_examples: %ld num_features: %ld \n", num_vectors, num_features)
101 int32_t max_dim_allowed =
CMath::min(num_vectors, num_features);
105 "target dimension should be less or equal to than minimum of N and D")
108 Map<MatrixXd> fmatrix(feature_matrix.matrix, num_features, num_vectors);
111 data_mean = fmatrix.rowwise().
sum()/(
float64_t) num_vectors;
112 fmatrix = fmatrix.colwise()-data_mean;
123 MatrixXd cov_mat(num_features, num_features);
124 cov_mat = fmatrix*fmatrix.transpose();
125 cov_mat /= (num_vectors-1);
127 SG_INFO(
"Computing Eigenvalues ... ")
129 SelfAdjointEigenSolver<MatrixXd> eigenSolve =
130 SelfAdjointEigenSolver<MatrixXd>(cov_mat);
131 eigenValues = eigenSolve.eigenvalues().tail(max_dim_allowed);
144 for (int32_t i=num_features-1; i<-1; i++)
155 for (int32_t i=num_features-1; i<-1; i++)
164 SG_INFO(
"Done\nReducing from %i to %i features..", num_features,
num_dim)
172 transformMatrix = eigenSolve.eigenvectors().block(0,
176 for (int32_t i=0; i<
num_dim; i++)
178 if (CMath::fequals_abs<float64_t>(0.0, eigenValues[i+max_dim_allowed-num_dim],
181 SG_WARNING(
"Covariance matrix has almost zero Eigenvalue (ie "
182 "Eigenvalue within a tolerance of %E around 0) at "
183 "dimension %d. Consider reducing its dimension.",
186 transformMatrix.col(i) = MatrixXd::Zero(num_features,1);
190 transformMatrix.col(i) /=
191 CMath::sqrt(eigenValues[i+max_dim_allowed-num_dim]*(num_vectors-1));
199 JacobiSVD<MatrixXd> svd(fmatrix.transpose(), ComputeThinU | ComputeThinV);
202 eigenValues = svd.singularValues();
203 eigenValues = eigenValues.cwiseProduct(eigenValues)/(num_vectors-1);
216 for (int32_t i=0; i<num_features; i++)
227 for (int32_t i=0; i<num_features; i++)
236 SG_INFO(
"Done\nReducing from %i to %i features..", num_features,
num_dim)
242 transformMatrix = svd.matrixV().block(0, 0, num_features,
num_dim);
245 for (int32_t i=0; i<
num_dim; i++)
247 if (CMath::fequals_abs<float64_t>(0.0, eigenValues[i],
250 SG_WARNING(
"Covariance matrix has almost zero Eigenvalue (ie "
251 "Eigenvalue within a tolerance of %E around 0) at "
252 "dimension %d. Consider reducing its dimension.",
255 transformMatrix.col(i) = MatrixXd::Zero(num_features,1);
259 transformMatrix.col(i) /=
CMath::sqrt(eigenValues[i]*(num_vectors-1));
265 fmatrix = fmatrix.colwise()+data_mean;
288 SG_INFO(
"Transforming feature matrix\n")
296 SG_INFO(
"Preprocessing feature matrix\n")
297 Map<MatrixXd> feature_matrix(m.
matrix, num_features, num_vectors);
298 VectorXd data_mean = feature_matrix.rowwise().sum()/(
float64_t) num_vectors;
299 feature_matrix = feature_matrix.colwise()-data_mean;
301 feature_matrix.block(0,0,
num_dim,num_vectors) =
302 transform_matrix.transpose()*feature_matrix;
304 SG_INFO(
"Form matrix of target dimension")
305 for (int32_t col=0; col<num_vectors; col++)
307 for (int32_t row=0; row<
num_dim; row++)
308 m.
matrix[col*num_dim+row] = feature_matrix(row,col);
323 SG_INFO(
"Preprocessing feature matrix\n")
324 Map<MatrixXd> feature_matrix(m.
matrix, num_features, num_vectors);
325 VectorXd data_mean = feature_matrix.rowwise().sum()/(
float64_t) num_vectors;
326 feature_matrix = feature_matrix.colwise()-data_mean;
328 ret_matrix = transform_matrix.transpose()*feature_matrix;
339 Map<VectorXd> inputVec(vector.
vector, vector.
vlen);
345 inputVec = inputVec-mean;
346 resultVec = transformMat.transpose()*inputVec;
347 inputVec = inputVec+mean;
387 #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
static T sum(T *vec, int32_t len)
return sum(vec)
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
The class Features is the base class of all feature objects.
static T min(T a, T b)
return the minimum of two integers
EPCAMemoryMode get_memory_mode() const
static float32_t sqrt(float32_t x)
x^0.5
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()