SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
BinaryLabels.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  * Written (W) 1999-2009 Soeren Sonnenburg
8  * Written (W) 1999-2008 Gunnar Raetsch
9  * Written (W) 2011-2012 Heiko Strathmann
10  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
11  */
12 
16 #include <shogun/lib/SGVector.h>
17 
18 using namespace shogun;
19 
21 {
22 }
23 
24 CBinaryLabels::CBinaryLabels(int32_t num_labels) : CDenseLabels(num_labels)
25 {
26 }
27 
28 #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
30 {
31  SGVector<float64_t> values(src.vlen);
32  for (int32_t i = 0; i < values.vlen; i++)
33  {
34  values[i] = src[i];
35  }
36  set_int_labels(src);
37  set_values(values);
38 }
39 
41 {
42  SGVector<float64_t> values(src.vlen);
43  for (int32_t i = 0; i < values.vlen; i++)
44  {
45  values[i] = src[i];
46  }
47  set_int_labels(src);
48  set_values(values);
49 }
50 #endif
51 
53 {
54  SGVector<float64_t> labels(src.vlen);
55  for (int32_t i = 0; i < labels.vlen; i++)
56  {
57  labels[i] = src[i] + threshold >= 0 ? +1.0 : -1.0;
58  }
59  set_labels(labels);
60  set_values(src);
61 }
62 
64 {
65 }
66 
67 void CBinaryLabels::ensure_valid(const char * context)
68 {
70  bool found_plus_one = false;
71  bool found_minus_one = false;
72 
73  int32_t subset_size = get_num_labels();
74  for (int32_t i = 0; i < subset_size; i++)
75  {
76  int32_t real_i = m_subset_stack->subset_idx_conversion(i);
77  if (m_labels[real_i] == +1.0)
78  {
79  found_plus_one = true;
80  }
81  else if (m_labels[real_i] == -1.0)
82  {
83  found_minus_one = true;
84  }
85  else
86  {
87  SG_ERROR(
88  "%s%s%s::ensure_valid(): Not a two class labeling label[%d]=%f (only +1/-1 "
89  "allowed)\n", context ? context : "",
90  context ? ": " : "", get_name(), i, m_labels[real_i]);
91  }
92  }
93 
94  if (!found_plus_one)
95  {
96  SG_WARNING(
97  "%s%s%s::ensure_valid(): Not a two class labeling - no positively labeled examples found\n",
98  context ? context : "", context ? ": " : "", get_name());
99  }
100 
101  if (!found_minus_one)
102  {
103  SG_WARNING(
104  "%s%s%s::ensure_valid): Not a two class labeling - no negatively labeled examples found\n",
105  context ? context : "", context ? ": " : "", get_name());
106  }
107 }
108 
110 {
111  return LT_BINARY;
112 }
113 
115 {
116  SG_DEBUG("entering CBinaryLabels::scores_to_probabilities()\n")
117 
118  REQUIRE(m_current_values.vector, "%s::scores_to_probabilities() requires "
119  "values vector!\n", get_name());
120 
121  if (a == 0 && b == 0)
122  {
125  a = params.a;
126  b = params.b;
127  }
128 
129  SG_DEBUG("using sigmoid: a=%f, b=%f\n", a, b)
130 
131  /* now the sigmoid is fitted, convert all values to probabilities */
132  for (index_t i = 0; i < m_current_values.vlen; ++i)
133  {
134  float64_t fApB = m_current_values[i] * a + b;
135  m_current_values[i] = fApB >= 0 ? CMath::exp(-fApB) / (1.0 + CMath::exp(-fApB)) :
136  1.0 / (1 + CMath::exp(fApB));
137  }
138 
139  SG_DEBUG("leaving CBinaryLabels::scores_to_probabilities()\n")
140 }
141 
143 {
144  CLabels* shallow_copy_labels=NULL;
145  SGVector<float64_t> shallow_copy_vector(m_labels);
146  shallow_copy_labels=new CBinaryLabels(m_labels.size());
147  SG_REF(shallow_copy_labels);
148 
149  ((CDenseLabels*) shallow_copy_labels)->set_labels(shallow_copy_vector);
151  shallow_copy_labels->add_subset(m_subset_stack->get_last_subset()->get_subset_idx());
152 
153  return shallow_copy_labels;
154 }
SGVector< index_t > get_subset_idx() const
Definition: Subset.h:48
SGVector< float64_t > m_labels
Definition: DenseLabels.h:274
binary labels +1/-1
Definition: LabelTypes.h:18
void set_int_labels(SGVector< int32_t > labels)
virtual int32_t get_num_labels() const
int32_t index_t
Definition: common.h:62
The class Labels models labels, i.e. class assignments of objects.
Definition: Labels.h:43
#define SG_ERROR(...)
Definition: SGIO.h:129
#define REQUIRE(x,...)
Definition: SGIO.h:206
CSubsetStack * m_subset_stack
Definition: Labels.h:140
#define SG_REF(x)
Definition: SGObject.h:54
CSubset * get_last_subset() const
Definition: SubsetStack.h:98
int32_t size() const
Definition: SGVector.h:113
static SigmoidParamters fit_sigmoid(SGVector< float64_t > scores)
Definition: Statistics.cpp:821
index_t vlen
Definition: SGVector.h:492
double float64_t
Definition: common.h:50
A File access base class.
Definition: File.h:34
virtual void add_subset(SGVector< index_t > subset)
Definition: Labels.cpp:39
index_t subset_idx_conversion(index_t idx) const
Definition: SubsetStack.h:105
virtual void ensure_valid(const char *context=NULL)
virtual const char * get_name() const
Definition: BinaryLabels.h:115
virtual CLabels * shallow_subset_copy()
#define SG_DEBUG(...)
Definition: SGIO.h:107
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
virtual void ensure_valid(const char *context=NULL)
static float64_t exp(float64_t x)
Definition: Math.h:621
void scores_to_probabilities(float64_t a=0, float64_t b=0)
virtual bool has_subsets() const
Definition: SubsetStack.h:89
SGVector< float64_t > m_current_values
Definition: Labels.h:143
#define SG_WARNING(...)
Definition: SGIO.h:128
Dense integer or floating point labels.
Definition: DenseLabels.h:35
virtual void set_values(SGVector< float64_t > values)
Definition: Labels.cpp:78
void set_labels(SGVector< float64_t > v)
Definition: DenseLabels.cpp:74
ELabelType
Definition: LabelTypes.h:15
virtual ELabelType get_label_type() const

SHOGUN Machine Learning Toolbox - Documentation