SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
MulticlassMultipleOutputLabels.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  * Copyright (C) 2012 Sergey Lisitsyn
00008  */
00009 
00010 #include <shogun/labels/MulticlassMultipleOutputLabels.h>
00011 
00012 using namespace shogun;
00013 
00014 CMulticlassMultipleOutputLabels::CMulticlassMultipleOutputLabels()
00015 : CLabels()
00016 {
00017     init();
00018 }
00019 
00020 CMulticlassMultipleOutputLabels::CMulticlassMultipleOutputLabels(int32_t num_labels)
00021 : CLabels()
00022 {
00023     init();
00024     m_labels = SG_MALLOC(SGVector<index_t>, num_labels);
00025     m_n_labels = num_labels;
00026 }
00027 
00028 CMulticlassMultipleOutputLabels::~CMulticlassMultipleOutputLabels()
00029 {
00030     SG_FREE(m_labels);
00031 }
00032 
00033 void CMulticlassMultipleOutputLabels::ensure_valid(const char* context)
00034 {
00035     if ( m_labels == NULL )
00036         SG_ERROR("Non-valid MulticlassMultipleOutputLabels in %s", context)
00037 }
00038 
00039 SGMatrix<index_t> CMulticlassMultipleOutputLabels::get_labels() const
00040 {
00041     if (m_n_labels==0)
00042         return SGMatrix<index_t>();
00043     int n_outputs = m_labels[0].vlen;
00044     SGMatrix<index_t> labels(m_n_labels,n_outputs);
00045     for (int32_t i=0; i<m_n_labels; i++)
00046     {
00047         for (int32_t j=0; j<n_outputs; j++)
00048             labels(i,j) = m_labels[i][j];
00049     }
00050     return labels;
00051 }
00052 
00053 SGVector<index_t> CMulticlassMultipleOutputLabels::get_label(int32_t idx)
00054 {
00055     ensure_valid("CMulticlassMultipleOutputLabels::get_label(int32_t)");
00056     if ( idx < 0 || idx >= get_num_labels() )
00057         SG_ERROR("Index must be inside [0, num_labels-1]\n")
00058 
00059     return m_labels[m_subset_stack->subset_idx_conversion(idx)];
00060 }
00061 
00062 bool CMulticlassMultipleOutputLabels::set_label(int32_t idx, SGVector<index_t> label)
00063 {
00064     int32_t real_idx = m_subset_stack->subset_idx_conversion(idx);
00065 
00066     if (real_idx < get_num_labels())
00067     {
00068         m_labels[real_idx] = label;
00069         return true;
00070     }
00071     else
00072         return false;
00073 }
00074 
00075 int32_t CMulticlassMultipleOutputLabels::get_num_labels() const
00076 {
00077     return m_n_labels;
00078 }
00079 
00080 void CMulticlassMultipleOutputLabels::init()
00081 {
00082     m_labels = NULL;
00083     m_n_labels = 0;
00084 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation