SHOGUN
v2.0.0
Main Page
Related Pages
Modules
Classes
Files
File List
File Members
All
Classes
Namespaces
Files
Functions
Variables
Typedefs
Enumerations
Enumerator
Friends
Macros
Groups
Pages
src
shogun
labels
MulticlassMultipleOutputLabels.cpp
Go to the documentation of this file.
1
/*
2
* This program is free software; you can redistribute it and/or modify
3
* it under the terms of the GNU General Public License as published by
4
* the Free Software Foundation; either version 3 of the License, or
5
* (at your option) any later version.
6
*
7
* Copyright (C) 2012 Sergey Lisitsyn
8
*/
9
10
#include <
shogun/labels/MulticlassMultipleOutputLabels.h
>
11
12
using namespace
shogun;
13
14
CMulticlassMultipleOutputLabels::CMulticlassMultipleOutputLabels
()
15
:
CLabels
()
16
{
17
init();
18
}
19
20
CMulticlassMultipleOutputLabels::CMulticlassMultipleOutputLabels
(int32_t num_labels)
21
:
CLabels
()
22
{
23
init();
24
m_labels
=
SG_MALLOC
(
SGVector<index_t>
, num_labels);
25
m_n_labels
= num_labels;
26
for
(int32_t i=0; i<
m_n_labels
; i++)
27
new
(&
m_labels
[i])
SGVector<index_t>
();
28
}
29
30
CMulticlassMultipleOutputLabels::~CMulticlassMultipleOutputLabels
()
31
{
32
for
(int32_t i=0; i<
m_n_labels
; i++)
33
m_labels
[i].~
SGVector<index_t>
();
34
SG_FREE
(
m_labels
);
35
}
36
37
CMulticlassMultipleOutputLabels
*
CMulticlassMultipleOutputLabels::obtain_from_generic
(
CLabels
* base_labels)
38
{
39
if
(base_labels->
get_label_type
() ==
LT_MULTICLASS_MULTIPLE_OUTPUT
)
40
return
(
CMulticlassMultipleOutputLabels
*) base_labels;
41
else
42
SG_SERROR
(
"base_labels must be of dynamic type CMulticlassMultipleOutputLabels\n"
);
43
44
return
NULL;
45
}
46
47
void
CMulticlassMultipleOutputLabels::ensure_valid
(
const
char
* context)
48
{
49
if
(
m_labels
== NULL )
50
SG_ERROR
(
"Non-valid MulticlassMultipleOutputLabels in %s"
, context);
51
}
52
53
SGMatrix<index_t>
CMulticlassMultipleOutputLabels::get_labels
()
const
54
{
55
if
(
m_n_labels
==0)
56
return
SGMatrix<index_t>
();
57
int
n_outputs =
m_labels
[0].
vlen
;
58
SGMatrix<index_t>
labels(
m_n_labels
,n_outputs);
59
for
(int32_t i=0; i<
m_n_labels
; i++)
60
{
61
for
(int32_t j=0; j<n_outputs; j++)
62
labels(i,j) =
m_labels
[i][j];
63
}
64
return
labels;
65
}
66
67
SGVector<index_t>
CMulticlassMultipleOutputLabels::get_label
(int32_t idx)
68
{
69
ensure_valid
(
"CMulticlassMultipleOutputLabels::get_label(int32_t)"
);
70
if
( idx < 0 || idx >=
get_num_labels
() )
71
SG_ERROR
(
"Index must be inside [0, num_labels-1]\n"
);
72
73
return
m_labels
[
m_subset_stack
->
subset_idx_conversion
(idx)];
74
}
75
76
bool
CMulticlassMultipleOutputLabels::set_label
(int32_t idx,
SGVector<index_t>
label)
77
{
78
int32_t real_idx =
m_subset_stack
->
subset_idx_conversion
(idx);
79
80
if
(real_idx <
get_num_labels
())
81
{
82
m_labels
[real_idx] = label;
83
return
true
;
84
}
85
else
86
return
false
;
87
}
88
89
int32_t
CMulticlassMultipleOutputLabels::get_num_labels
()
90
{
91
return
m_n_labels
;
92
}
93
94
void
CMulticlassMultipleOutputLabels::init()
95
{
96
m_labels
= NULL;
97
m_n_labels
= 0;
98
}
SHOGUN
Machine Learning Toolbox - Documentation