From 5131bb50ee2e46d9965f21d94a68c4e6557c3f2c Mon Sep 17 00:00:00 2001 From: Heiko Strathmann Date: Sun, 10 Mar 2013 20:39:54 +0000 Subject: [PATCH] made example way better (random input data means non-sense output) and prepared for unit-test --- .../libshogun/classifier_larank.cpp | 87 ++++++++++++++----- 1 file changed, 64 insertions(+), 23 deletions(-) diff --git a/examples/undocumented/libshogun/classifier_larank.cpp b/examples/undocumented/libshogun/classifier_larank.cpp index 2a09e0c223b..315f7beb7f0 100644 --- a/examples/undocumented/libshogun/classifier_larank.cpp +++ b/examples/undocumented/libshogun/classifier_larank.cpp @@ -1,3 +1,12 @@ +/* + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * Written (W) 2013 Heiko Strathmann and others + */ + #include #include #include @@ -6,28 +15,42 @@ using namespace shogun; -void print_message(FILE* target, const char* str) +void test() { - fprintf(target, "%s", str); -} + index_t num_vec=10; + index_t num_feat=3; + index_t num_class=num_feat; // to make data easy + float64_t distance=15; -int main(int argc, char** argv) -{ - init_shogun(&print_message); - index_t num_vec=3; - index_t num_feat=2; - index_t num_class=2; + // create some linearly seperable data + SGMatrix matrix(num_class, num_vec); + SGMatrix matrix_test(num_class, num_vec); + CMulticlassLabels* labels=new CMulticlassLabels(num_vec); + CMulticlassLabels* labels_test=new CMulticlassLabels(num_vec); + for (index_t i=0; iset_label(i, label); + labels_test->set_label(i, label); + } - // create some data - SGMatrix matrix(num_feat, num_vec); - SGVector::range_fill_vector(matrix.matrix, num_feat*num_vec); + /* make sure data is linearly seperable per class */ + matrix(label,i)+=distance; + matrix_test(label,i)+=distance; + } + matrix.display_matrix("matrix"); + labels->get_int_labels().display_vector("labels"); - // create vectors // shogun will now own the matrix created CDenseFeatures* features=new CDenseFeatures(matrix); + CDenseFeatures* features_test= + new CDenseFeatures(matrix_test); // create three labels - CMulticlassLabels* labels=new CMulticlassLabels(num_vec); for (index_t i=0; iset_label(i, i%num_class); @@ -41,21 +64,39 @@ int main(int argc, char** argv) svm->train(); // classify on training examples - CMulticlassLabels* output=CMulticlassLabels::obtain_from_generic(svm->apply()); - SGVector::display_vector(output->get_labels().vector, output->get_num_labels(), - "batch output"); + CMulticlassLabels* output=(CMulticlassLabels*)svm->apply(); + output->get_labels().display_vector("batch output"); /* assert that batch apply and apply(index_t) give same result */ + SGVector single_outputs(output->get_num_labels()); for (index_t i=0; iget_num_labels(); ++i) - { - float64_t label=svm->apply_one(i); - SG_SPRINT("single output[%d]=%f\n", i, label); - ASSERT(output->get_label(i)==label); - } - SG_UNREF(output); + single_outputs[i]=svm->apply_one(i); + + single_outputs.display_vector("single_outputs"); + + for (index_t i=0; iget_num_labels(); ++i) + ASSERT(output->get_label(i)==single_outputs[i]); + + CMulticlassLabels* output_test= + (CMulticlassLabels*)svm->apply(features_test); + labels_test->get_labels().display_vector("labels_test"); + output_test->get_labels().display_vector("output_test"); + + for (index_t i=0; iget_num_labels(); ++i) + ASSERT(labels_test->get_label(i)==output_test->get_label(i)); // free up memory + SG_UNREF(output); + SG_UNREF(labels_test); + SG_UNREF(output_test); SG_UNREF(svm); +} + +int main(int argc, char** argv) +{ + init_shogun_with_defaults(); + + test(); exit_shogun(); return 0;