LDA

The Dlib library also has an implementation of the linear discriminant analysis algorithm, which can be used for dimensionality reduction. It's a supervised algorithm, so it needs labeled data. This algorithm is implemented with the Dlib::compute_lda_transform() function, which takes four parameters. The first one is the input/output parameter – as input, it is used to pass input training data (in matrix form) and as output, it receives the LDA transformation matrix. The second parameter is the output for the mean values. The third parameter is the labels for the input data, while the fourth one is the desired number of target dimensions. The following code shows an example of how to use LDA for dimension reduction with the Dlib library:

 void LDAReduction(const Matrix &data, 
const std::vector<unsigned long> &labels,
unsigned long target_dim) {
Dlib::matrix<DataType, 0, 1> mean;
Matrix transform = data;
Dlib::compute_lda_transform(transform, mean, labels, target_dim);

for (long r = 0; r < data.nr(); ++r) {
Matrix row = transform * Dlib::trans(Dlib::rowm(data, r)) - mean;
double x = row(0, 0);
double y = row(1, 0);
}
}

To perform an actual LDA transform after the algorithm has been trained, we multiply our samples with the LDA matrix. In our case, we also transposed them. The following code shows the essential part of this example:

  transform * Dlib::trans(Dlib::rowm(data, r))

The following graph shows the result of using LDA reduction on two components:

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.16.81.33