SHOGUN  v3.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
BinaryLabels.cpp
Go to the documentation of this file.
00001 #include <shogun/labels/DenseLabels.h>
00002 #include <shogun/labels/BinaryLabels.h>
00003 #include <shogun/mathematics/Statistics.h>
00004 
00005 using namespace shogun;
00006 
00007 CBinaryLabels::CBinaryLabels() : CDenseLabels()
00008 {
00009 }
00010 
00011 CBinaryLabels::CBinaryLabels(int32_t num_labels) : CDenseLabels(num_labels)
00012 {
00013 }
00014 
00015 #if !defined(SWIGJAVA) && !defined(SWIGCSHARP)
00016 CBinaryLabels::CBinaryLabels(SGVector<int32_t> src) : CDenseLabels()
00017 {
00018     SGVector<float64_t> values(src.vlen);
00019     for (int32_t i=0; i<values.vlen; i++)
00020         values[i] = src[i];
00021     set_int_labels(src);
00022     set_values(values);
00023 }
00024 
00025 CBinaryLabels::CBinaryLabels(SGVector<int64_t> src) : CDenseLabels()
00026 {
00027     SGVector<float64_t> values(src.vlen);
00028     for (int32_t i=0; i<values.vlen; i++)
00029         values[i] = src[i];
00030     set_int_labels(src);
00031     set_values(values);
00032 }
00033 #endif
00034 
00035 CBinaryLabels::CBinaryLabels(SGVector<float64_t> src, float64_t threshold) : CDenseLabels()
00036 {
00037     SGVector<float64_t> labels(src.vlen);
00038     for (int32_t i=0; i<labels.vlen; i++)
00039         labels[i] = src[i]+threshold>=0 ? +1.0 : -1.0;
00040     set_labels(labels);
00041     set_values(src);
00042 }
00043 
00044 CBinaryLabels::CBinaryLabels(CFile* loader) : CDenseLabels(loader)
00045 {
00046 }
00047 
00048 void CBinaryLabels::ensure_valid(const char* context)
00049 {
00050     CDenseLabels::ensure_valid(context);
00051     bool found_plus_one=false;
00052     bool found_minus_one=false;
00053 
00054     int32_t subset_size=get_num_labels();
00055     for (int32_t i=0; i<subset_size; i++)
00056     {
00057         int32_t real_i=m_subset_stack->subset_idx_conversion(i);
00058         if (m_labels[real_i]==+1.0)
00059             found_plus_one=true;
00060         else if (m_labels[real_i]==-1.0)
00061             found_minus_one=true;
00062         else
00063         {
00064             SG_ERROR(
00065                     "%s%s%s::ensure_valid(): Not a two class labeling label[%d]=%f (only +1/-1 "
00066                             "allowed)\n", context ? context : "",
00067                     context ? ": " : "", get_name(), i, m_labels[real_i]);
00068         }
00069     }
00070 
00071     if (!found_plus_one)
00072     {
00073         SG_WARNING(
00074                 "%s%s%s::ensure_valid(): Not a two class labeling - no positively labeled examples found\n",
00075                 context ? context : "", context ? ": " : "", get_name());
00076     }
00077 
00078     if (!found_minus_one)
00079     {
00080         SG_WARNING(
00081                 "%s%s%s::ensure_valid): Not a two class labeling - no negatively labeled examples found\n",
00082                 context ? context : "", context ? ": " : "", get_name());
00083     }
00084 }
00085 
00086 ELabelType CBinaryLabels::get_label_type() const
00087 {
00088     return LT_BINARY;
00089 }
00090 
00091 void CBinaryLabels::scores_to_probabilities(float64_t a, float64_t b)
00092 {
00093     SG_DEBUG("entering CBinaryLabels::scores_to_probabilities()\n")
00094 
00095     REQUIRE(m_current_values.vector, "%s::scores_to_probabilities() requires "
00096             "values vector!\n", get_name());
00097 
00098     if (a==0 && b==0)
00099     {
00100         CStatistics::SigmoidParamters params=
00101                 CStatistics::fit_sigmoid(m_current_values);
00102         a=params.a;
00103         b=params.b;
00104     }
00105 
00106     SG_DEBUG("using sigmoid: a=%f, b=%f\n", a, b)
00107 
00108     /* now the sigmoid is fitted, convert all values to probabilities */
00109     for (index_t i=0; i<m_current_values.vlen; ++i)
00110     {
00111         float64_t fApB=m_current_values[i]*a+b;
00112         m_current_values[i]=fApB>=0 ? CMath::exp(-fApB)/(1.0+CMath::exp(-fApB)) :
00113                 1.0/(1+CMath::exp(fApB));
00114     }
00115 
00116     SG_DEBUG("leaving CBinaryLabels::scores_to_probabilities()\n")
00117 }
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

SHOGUN Machine Learning Toolbox - Documentation