我想在我的训练集上安装dlib的LDA,并将转换应用于训练和测试集。我写了以下最小的例子来重现问题。如果删除使用LDA的部分,则应输出有意义的预测。
#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存储库中找不到任何示例。