SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 }

SHOGUN Machine Learning Toolbox - Documentation