/
classifier_multiclass_prob_heuristics.cpp
77 lines (58 loc) · 2.3 KB
/
classifier_multiclass_prob_heuristics.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <shogun/io/CSVFile.h>
#include <shogun/labels/MulticlassLabels.h>
#include <shogun/io/SGIO.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/multiclass/MulticlassStrategy.h>
#include <shogun/multiclass/MulticlassOneVsOneStrategy.h>
#include <shogun/multiclass/MulticlassOneVsRestStrategy.h>
#include <shogun/machine/LinearMulticlassMachine.h>
#include <shogun/classifier/svm/LibLinear.h>
#define EPSILON 1e-5
using namespace shogun;
/* file data */
const char fname_feats[]="../data/fm_train_real.dat";
const char fname_labels[]="../data/label_train_multiclass.dat";
void test()
{
/* dense features from matrix */
auto feature_file = std::make_shared<CSVFile>(fname_feats);
SGMatrix<float64_t> mat=SGMatrix<float64_t>();
mat.load(feature_file);
auto features=std::make_shared<DenseFeatures<float64_t>>(mat);
/* labels from vector */
auto label_file = std::make_shared<CSVFile>(fname_labels);
SGVector<float64_t> label_vec;
label_vec.load(label_file);
auto labels=std::make_shared<MulticlassLabels>(label_vec);
// Create liblinear svm classifier with L2-regularized L2-loss
auto svm = std::make_shared<LibLinear>(L2R_L2LOSS_SVC);
// Add some configuration to the svm
svm->set_epsilon(EPSILON);
svm->set_bias_enabled(true);
// Create a multiclass svm classifier that consists of several of the previous one
// There are several heuristics are implemented:
// OVA_NORM, OVA_SOFTMAX
// OVO_PRICE, OVO_HASTIE, OVO_HAMAMURA
auto mc_svm = std::make_shared<LinearMulticlassMachine>(
std::make_shared<MulticlassOneVsOneStrategy>(OVO_HASTIE), features->as<DotFeatures>(), svm, labels);
// Train the multiclass machine using the data passed in the constructor
mc_svm->train();
// Classify the training examples and show the results
auto output = mc_svm->apply()->as<MulticlassLabels>();
SGVector< int32_t > out_labels = output->get_int_labels();
SGVector<int32_t>::display_vector(out_labels.vector, out_labels.vlen);
for (int32_t i=0; i<output->get_num_labels(); i++)
{
SG_SPRINT("out_values[%d] = ", i);
SGVector<float64_t> out_values = output->get_multiclass_confidences(i);
SGVector<float64_t>::display_vector(out_values.vector, out_values.vlen);
SG_SPRINT("\n");
}
//Free resources
}
int main(int argc, char** argv)
{
//env()->io()->set_loglevel(MSG_DEBUG);
test();
return 0;
}