MulticlassOneVsRestStrategy.cpp

Go to the documentation of this file.
00001 /*
00002  * This program is free software; you can redistribute it and/or modify
00003  * it under the terms of the GNU General Public License as published by
00004  * the Free Software Foundation; either version 3 of the License, or
00005  * (at your option) any later version.
00006  *
00007  * Written (W) 2012 Chiyuan Zhang
00008  * Copyright (C) 2012 Chiyuan Zhang
00009  */
00010 
00011 #include <shogun/multiclass/MulticlassOneVsRestStrategy.h>
00012 #include <shogun/labels/BinaryLabels.h>
00013 #include <shogun/labels/MulticlassLabels.h>
00014 
00015 using namespace shogun;
00016 
00017 CMulticlassOneVsRestStrategy::CMulticlassOneVsRestStrategy()
00018     : CMulticlassStrategy()
00019 {
00020 }
00021 
00022 SGVector<int32_t> CMulticlassOneVsRestStrategy::train_prepare_next()
00023 {
00024     for (int32_t i=0; i < m_orig_labels->get_num_labels(); ++i)
00025     {
00026         if (((CMulticlassLabels*) m_orig_labels)->get_int_label(i)==m_train_iter)
00027             ((CBinaryLabels*) m_train_labels)->set_label(i, +1.0);
00028         else
00029             ((CBinaryLabels*) m_train_labels)->set_label(i, -1.0);
00030     }
00031 
00032     // increase m_train_iter *after* setting labels
00033     CMulticlassStrategy::train_prepare_next();
00034 
00035     return SGVector<int32_t>();
00036 }
00037 
00038 int32_t CMulticlassOneVsRestStrategy::decide_label(SGVector<float64_t> outputs)
00039 {
00040     if (m_rejection_strategy && m_rejection_strategy->reject(outputs))
00041         return CDenseLabels::REJECTION_LABEL;
00042 
00043     return SGVector<float64_t>::arg_max(outputs.vector, 1, outputs.vlen);
00044 }
00045 
00046 SGVector<index_t> CMulticlassOneVsRestStrategy::decide_label_multiple_output(SGVector<float64_t> outputs, int32_t n_outputs)
00047 {
00048     float64_t* outputs_ = SG_MALLOC(float64_t, outputs.vlen);
00049     int32_t* indices_ = SG_MALLOC(int32_t, outputs.vlen);
00050     for (int32_t i=0; i<outputs.vlen; i++)
00051     {
00052         outputs_[i] = outputs[i];
00053         indices_[i] = i;
00054     }
00055     CMath::qsort_backward_index(outputs_,indices_,outputs.vlen);
00056     SGVector<index_t> result(n_outputs);
00057     for (int32_t i=0; i<n_outputs; i++)
00058         result[i] = indices_[i];
00059     SG_FREE(outputs_);
00060     SG_FREE(indices_);
00061     return result;
00062 }
00063 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation