49 using namespace Eigen;
55 initialize_parameters();
60 void CFisherLDA::initialize_parameters()
82 REQUIRE(features,
"Features are not provided!\n")
85 "LDA only works with dense features. you provided %s\n",
89 "LDA only works with real features.\n");
91 REQUIRE(labels,
"Labels for the given features are not specified!\n")
94 "the type MulticlassLabels! you provided %s\n", labels->
get_name());
97 ->get_feature_matrix();
101 int32_t num_vectors=feature_matrix.num_cols;
102 int32_t num_features=feature_matrix.num_rows;
104 REQUIRE(labels_vector.
vlen==num_vectors,
"The number of samples provided (%d)"
105 " must be equal to the number of labels provided(%d)\n",num_vectors,
111 REQUIRE(C>1,
"At least two classes are needed to perform LDA.\n")
131 mean_total=VectorXd::Zero(num_features);
133 vector<VectorXd> mean_class(C);
138 VectorXd num_class=VectorXd::Zero(C);
143 mean_class[i]=VectorXd::Zero(num_features);
144 for (j=0; j<num_vectors; j++)
149 mean_class[i]+=fmatrix.col(j);
153 mean_total+=mean_class[i];
161 for (j=0; j<num_vectors; j++)
163 fmatrix.col(j)-=mean_class[i];
169 vector<MatrixXd> centered_class_i(C);
170 VectorXd temp=num_class;
171 MatrixXd Sw=MatrixXd::Zero(num_features, num_features);
174 centered_class_i[i]=MatrixXd::Zero(num_features, num_class[i]);
175 for (j=0; j<num_vectors; j++)
177 centered_class_i[i].col(num_class[i]-(temp[i]--))
179 Sw+=(centered_class_i[i]*centered_class_i[i].transpose())
180 *num_class[i]/(
float64_t)(num_class[i]-1);
186 Sb.col(i)=sqrt(num_class[i])*(mean_class[i]-mean_total);
191 JacobiSVD<MatrixXd> svd(fmatrix1, ComputeThinU | ComputeThinV);
195 if(num_features>num_vectors)
198 for (i=0;i<num_vectors;i++)
203 Q=svd.matrixU().leftCols(j);
209 Sb=(Q.transpose())*Sb*(Sb.transpose())*Q;
211 Sw=Q.transpose()*Sw*Q;
216 HouseholderQR<MatrixXd> decomposition(Sw.llt().matrixU().transpose());
222 JacobiSVD<MatrixXd> svd2(decomposition.solve
223 (decomposition.solve(Sb).transpose()).transpose(),ComputeThinU);
225 Map<MatrixXd> eigenVectors(m_transformation_matrix.matrix, num_features,
228 eigenVectors=Q*(svd2.matrixU()).leftCols(
m_num_dim);
232 eigenValues=svd2.singularValues().topRows(
m_num_dim);
237 MatrixXd Sw=fmatrix*fmatrix.transpose();
243 Sb.col(i)=mean_class[i];
245 Sb=Sb-mean_total.rowwise().replicate(C);
246 Sb=Sb*Sb.transpose();
254 EigenSolver<MatrixXd> es(Sw.householderQr().solve(Sb));
256 MatrixXd all_eigenvectors=es.eigenvectors().real();
257 VectorXd all_eigenvalues=es.eigenvalues().real();
259 std::vector<pair<float64_t, int32_t> > data(num_features);
260 for (i=0; i<num_features; i++)
262 data[i].first=all_eigenvalues[i];
266 std::sort (data.begin(), data.end());
280 eigenValues[i]=data[num_features-i-1].first;
281 eigenVectors.col(i)=all_eigenvectors.col(data[num_features-i-1].second);
297 "LDA only works with dense features\n");
300 "LDA only works with real features\n");
303 features)->get_feature_matrix();
306 int32_t num_features=m.num_rows;
308 SG_INFO(
"Transforming feature matrix\n")
312 SG_INFO(
"get Feature matrix: %ix%i\n", num_vectors, num_features)
314 Map<MatrixXd> feature_matrix (m.matrix, num_features, num_vectors);
316 feature_matrix.block (0, 0,
m_num_dim, num_vectors)=
317 transform_matrix.transpose()*feature_matrix;
319 SG_INFO(
"Form matrix of target dimension")
320 for (int32_t col=0; col<num_vectors; col++)
322 for (int32_t row=0; row<
m_num_dim; row++)
323 m[col*m_num_dim+row]=feature_matrix(row, col);
326 m.num_cols=num_vectors;
341 resultVec=transformMat.transpose()*inputVec;
virtual const char * get_name() const =0
the class DimensionReductionPreprocessor, a base class for preprocessors used to lower the dimensiona...
virtual ELabelType get_label_type() const =0
virtual SGMatrix< float64_t > apply_to_feature_matrix(CFeatures *features)
SGMatrix< float64_t > get_transformation_matrix()
virtual bool fit(CFeatures *features, CLabels *labels, int32_t num_dimensions=0)
virtual SGVector< float64_t > apply_to_feature_vector(SGVector< float64_t > vector)
The class Labels models labels, i.e. class assignments of objects.
multi-class labels 0,1,...
Multiclass Labels for multi-class classification.
virtual EFeatureClass get_feature_class() const =0
SGMatrix< float64_t > m_transformation_matrix
SGVector< float64_t > m_mean_vector
all of classes and functions are contained in the shogun namespace
The class Features is the base class of all feature objects.
SGVector< float64_t > m_eigenvalues_vector
SGVector< float64_t > get_mean()
virtual EFeatureType get_feature_type() const =0
SGVector< float64_t > get_eigenvalues()