{"body":"/*\n * This program is free software; you can redistribute it and/or modify\n * it under the terms of the GNU General Public License as published by\n * the Free Software Foundation; either version 3 of the License, or\n * (at your option) any later version.\n *\n * Written (W) 2011 Shashwat Lal Das\n * Copyright (C) 2011 Berlin Institute of Technology and Max-Planck-Society\n *\n * This example demonstrates use of the Vowpal Wabbit learning algorithm.\n */\n\n#include <shogun/base/init.h>\n#include <shogun/lib/common.h>\n\n#include <shogun/io/streaming/StreamingAsciiFile.h>\n#include <shogun/features/streaming/StreamingDenseFeatures.h>\n#include <shogun/multiclass/tree/BalancedConditionalProbabilityTree.h>\n\nusing namespace shogun;\n\nint main(int argc, char **argv)\n{\n\tinit_shogun_with_defaults();\n\n\tconst char* train_file_name = \"../data/7class_example4_train.dense\";\n\tconst char* test_file_name = \"../data/7class_example4_test.dense\";\n\tCStreamingAsciiFile* train_file = new CStreamingAsciiFile(train_file_name);\n\tSG_REF(train_file);\n\n\tCStreamingDenseFeatures<float32_t>* train_features = new CStreamingDenseFeatures<float32_t>(train_file, true, 1024);\n\tSG_REF(train_features);\n\n\tCBalancedConditionalProbabilityTree *cpt = new CBalancedConditionalProbabilityTree();\n\tcpt->set_num_passes(1);\n\tcpt->set_features(train_features);\n\n\tif (argc > 1)\n\t{\n\t\tfloat64_t alpha = 0.5;\n\t\tsscanf(argv[1], \"%lf\", &alpha);\n\t\tSG_SPRINT(\"Setting alpha to %.2lf\\n\", alpha);\n\t\tcpt->set_alpha(alpha);\n\t}\n\n\tcpt->train();\n\tcpt->print_tree();\n\n\tCStreamingAsciiFile* test_file = new CStreamingAsciiFile(test_file_name);\n\tSG_REF(test_file);\n\tCStreamingDenseFeatures<float32_t>* test_features = new CStreamingDenseFeatures<float32_t>(test_file, true, 1024);\n\tSG_REF(test_features);\n\n\tCMulticlassLabels *pred = cpt->apply_multiclass(test_features);\n\ttest_features->reset_stream();\n\tSG_SPRINT(\"num_labels = %d\\n\", pred->get_num_labels());\n\n\tSG_UNREF(test_features);\n\tSG_UNREF(test_file);\n\ttest_file = new CStreamingAsciiFile(test_file_name);\n\tSG_REF(test_file);\n\ttest_features = new CStreamingDenseFeatures<float32_t>(test_file, true, 1024);\n\tSG_REF(test_features);\n\n\tCMulticlassLabels *gnd = new CMulticlassLabels(pred->get_num_labels());\n\tSG_REF(gnd);\n\ttest_features->start_parser();\n\tfor (int32_t i=0; i < pred->get_num_labels(); ++i)\n\t{\n\t\ttest_features->get_next_example();\n\t\tgnd->set_int_label(i, test_features->get_label());\n\t\ttest_features->release_example();\n\t}\n\ttest_features->end_parser();\n\n\tint32_t n_correct = 0;\n\tfor (index_t i=0; i < pred->get_num_labels(); ++i)\n\t{\n\t\tif (pred->get_int_label(i) == gnd->get_int_label(i))\n\t\t\tn_correct++;\n\t\t//SG_SPRINT(\"%d-%d \", pred->get_int_label(i), gnd->get_int_label(i));\n\t}\n\tSG_SPRINT(\"\\n\");\n\n\tSG_SPRINT(\"Multiclass Accuracy = %.2f%%\\n\", 100.0*n_correct / gnd->get_num_labels());\n\n\tSG_UNREF(gnd);\n\tSG_UNREF(train_features);\n\tSG_UNREF(test_features);\n\tSG_UNREF(train_file);\n\tSG_UNREF(test_file);\n\tSG_UNREF(cpt);\n\tSG_UNREF(pred);\n\n\texit_shogun();\n\n\treturn 0;\n}","name":"","extension":"txt","url":"https://www.irccloud.com/pastebin/S5Wx3Wqn","modified":1427448946,"id":"S5Wx3Wqn","size":2972,"lines":98,"own_paste":false,"theme":"","date":1427448946}