如何使用dlib的LDA [英] How to use dlib's LDA
问题描述
我想将dlib的LDA应用于我的训练集,并将转换应用于训练集和测试集.我写了下面的最小示例来重现该问题.如果删除使用LDA的部分,则会输出有意义的预测.
I want to fit dlib's LDA on my training set and apply the transformation to both the training and testing set. I wrote following minimal example to reproduce the problem. If you delete the sections that uses LDA, it should output a meaningful prediction.
#include <iostream>
#include <vector>
#include <dlib/svm.h>
int main() {
typedef dlib::matrix<float, 2, 1> sample_type;
typedef dlib::radial_basis_kernel<sample_type> kernel_type;
dlib::svm_c_trainer<kernel_type> trainer;
trainer.set_kernel(kernel_type(0.5f));
trainer.set_c(1.0f);
std::vector<sample_type> samples_train;
std::vector<float> labels_train;
std::vector<sample_type> samples_test;
std::vector<float> labels_test;
sample_type sample;
float label;
label = -1;
sample(0) = -1;
sample(1) = -1;
samples_train.push_back(sample);
labels_train.push_back(label);
label = 1;
sample(0) = 1;
sample(1) = 1;
samples_train.push_back(sample);
labels_train.push_back(label);
label = 1;
sample(0) = 0.5;
sample(1) = 0.5;
samples_test.push_back(sample);
labels_test.push_back(label);
// Fit LDA on training data
dlib::matrix<sample_type> X;
dlib::matrix<sample_type,0,1> mean;
dlib::compute_lda_transform(X, mean, labels_train);
// Apply LDA on train data
for (auto &sample_train : samples_train){
sample_train = X * sample_train;
}
// Apply LDA on test data
for (auto &sample_test : samples_test){
sample_test = X * sample_test;
}
auto predictor = trainer.train(samples_train, labels_train);
std::cout << "Train Sample 1: " << predictor(samples_train[0]) << ", label: " << labels_train[0] << std::endl;
std::cout << "Train Sample 2: " << predictor(samples_train[1]) << ", label: " << labels_train[1] << std::endl;
std::cout << "Test Sample: " << predictor(samples_test[0]) << ", label: " << labels_test[0] << std::endl;
}
错误:
cannot convert 'labels_train' (type 'std::__debug::vector<float>') to type 'const std::__debug::vector<long unsigned int>&'
但是,如果标签与样本的类型不同,则SVM会引发错误.我在dlib的github存储库上找不到任何示例.
But if the labels are not of the same type as the samples, the SVM throws an error. I could not find any example on dlib's github repository.
推荐答案
您应该使用两组标签,一组用于lda,类型为long unsigned,另一种为您的SVM,类型为float
You should use two set of labels, one being of type long unsigned for the lda and another of type float for your SVM
这篇关于如何使用dlib的LDA的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!