Skip to content

Commit

Permalink
made example way better (random input data means non-sense output) an…
Browse files Browse the repository at this point in the history
…d prepared for unit-test
  • Loading branch information
karlnapf committed Mar 10, 2013
1 parent 56ce595 commit 5131bb5
Showing 1 changed file with 64 additions and 23 deletions.
87 changes: 64 additions & 23 deletions 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 <shogun/labels/MulticlassLabels.h>
#include <shogun/features/DenseFeatures.h>
#include <shogun/kernel/GaussianKernel.h>
Expand All @@ -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<float64_t> matrix(num_class, num_vec);
SGMatrix<float64_t> matrix_test(num_class, num_vec);
CMulticlassLabels* labels=new CMulticlassLabels(num_vec);
CMulticlassLabels* labels_test=new CMulticlassLabels(num_vec);
for (index_t i=0; i<num_vec; ++i)
{
index_t label=i%num_class;
for (index_t j=0; j<num_feat; ++j)
{
matrix(j,i)=CMath::randn_double();
matrix_test(j,i)=CMath::randn_double();
labels->set_label(i, label);
labels_test->set_label(i, label);
}

// create some data
SGMatrix<float64_t> matrix(num_feat, num_vec);
SGVector<float64_t>::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<float64_t>* features=new CDenseFeatures<float64_t>(matrix);
CDenseFeatures<float64_t>* features_test=
new CDenseFeatures<float64_t>(matrix_test);

// create three labels
CMulticlassLabels* labels=new CMulticlassLabels(num_vec);
for (index_t i=0; i<num_vec; ++i)
labels->set_label(i, i%num_class);

Expand All @@ -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<float64_t>::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<float64_t> single_outputs(output->get_num_labels());
for (index_t i=0; i<output->get_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; i<output->get_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; i<output->get_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;
Expand Down

0 comments on commit 5131bb5

Please sign in to comment.