Labels.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  * Written (W) 1999-2009 Soeren Sonnenburg
00008  * Written (W) 1999-2008 Gunnar Raetsch
00009  * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society
00010  */
00011 
00012 #include "features/Labels.h"
00013 #include "lib/common.h"
00014 #include "lib/File.h"
00015 #include "lib/io.h"
00016 #include "lib/Mathematics.h"
00017 
00018 #ifdef HAVE_BOOST_SERIALIZATION
00019 #include <boost/serialization/export.hpp>
00020 BOOST_CLASS_EXPORT(shogun::CLabels);
00021 #endif //HAVE_BOOST_SERIALIZATION
00022 
00023 using namespace shogun;
00024 
00025 CLabels::CLabels()
00026 : CSGObject()
00027 {
00028     labels = NULL;
00029     num_labels = 0;
00030 }
00031 
00032 CLabels::CLabels(int32_t num_lab)
00033 : CSGObject(), num_labels(num_lab)
00034 {
00035     labels=new float64_t[num_lab];
00036     for (int32_t i=0; i<num_lab; i++)
00037         labels[i]=0;
00038 }
00039 
00040 CLabels::CLabels(float64_t* p_labels, int32_t len)
00041 : CSGObject()
00042 {
00043     labels = NULL;
00044     num_labels = 0;
00045 
00046     set_labels(p_labels, len);
00047 }
00048 
00049 CLabels::CLabels(char* fname)
00050 : CSGObject()
00051 {
00052     num_labels=0;
00053     labels=NULL;
00054 
00055     load(fname);
00056 }
00057 
00058 CLabels::~CLabels()
00059 {
00060     delete[] labels;
00061     num_labels=0;
00062     labels=NULL;
00063 }
00064 
00065 void CLabels::set_labels(float64_t* p_labels, int32_t len)
00066 {
00067     ASSERT(len>0);
00068     num_labels=len;
00069 
00070     delete[] labels;
00071     labels=CMath::clone_vector(p_labels, len);
00072 }
00073 
00074 bool CLabels::is_two_class_labeling()
00075 {
00076     ASSERT(labels);
00077     bool found_plus_one=false;
00078     bool found_minus_one=false;
00079 
00080     for (int32_t i=0; i<num_labels; i++)
00081     {
00082         if (labels[i]==+1.0)
00083             found_plus_one=true;
00084         else if (labels[i]==-1.0)
00085             found_minus_one=true;
00086         else
00087             SG_ERROR("Not a two class labeling label[%d]=%f (only +1/-1 allowed)\n", i, labels[i]);
00088     }
00089 
00090     if (!found_plus_one)
00091         SG_ERROR("Not a two class labeling - no positively labeled examples found\n");
00092     if (!found_minus_one)
00093         SG_ERROR("Not a two class labeling - no negatively labeled examples found\n");
00094 
00095     return true;
00096 }
00097 
00098 int32_t CLabels::get_num_classes()
00099 {
00100     int32_t n=-1;
00101     int32_t* lab=get_int_labels(n);
00102 
00103     int32_t num_classes=0;
00104     for (int32_t i=0; i<n; i++)
00105         num_classes=CMath::max(num_classes,lab[i]);
00106 
00107     delete[] lab;
00108 
00109     return num_classes+1;
00110 }
00111 
00112 float64_t* CLabels::get_labels(int32_t &len)
00113 {
00114     len=num_labels;
00115 
00116     if (num_labels>0)
00117     {
00118         float64_t* _labels=new float64_t[num_labels] ;
00119         for (int32_t i=0; i<len; i++)
00120             _labels[i]=get_label(i) ;
00121         return _labels ;
00122     }
00123     else 
00124         return NULL;
00125 }
00126 
00127 void CLabels::get_labels(float64_t** p_labels, int32_t* len)
00128 {
00129     ASSERT(p_labels && len);
00130     *p_labels=NULL;
00131     *len=num_labels;
00132 
00133     if (num_labels>0)
00134     {
00135         *p_labels=(float64_t*) malloc(sizeof(float64_t)*num_labels);
00136 
00137         for (int32_t i=0; i<num_labels; i++)
00138             (*p_labels)[i]=get_label(i);
00139     }
00140 }
00141 
00142 int32_t* CLabels::get_int_labels(int32_t &len)
00143 {
00144     len=num_labels;
00145 
00146     if (num_labels>0)
00147     {
00148         int32_t* _labels=new int32_t[num_labels] ;
00149         for (int32_t i=0; i<len; i++)
00150             _labels[i]= (int32_t) get_label(i) ;
00151         return _labels ;
00152     }
00153     else 
00154         return NULL;
00155 }
00156 
00157 void CLabels::set_int_labels(int32_t * mylabels, int32_t len)
00158 {
00159     num_labels = len ;
00160     delete[] labels ;
00161     
00162     labels = new float64_t[num_labels] ;
00163     for (int32_t i=0; i<num_labels; i++)
00164         set_int_label(i, mylabels[i]) ;
00165 }
00166 
00167 bool CLabels::load(char* fname)
00168 {
00169     bool status=false;
00170 
00171     delete[] labels;
00172     num_labels=0;
00173 
00174     CFile f(fname, 'r', F_DREAL);
00175     int64_t num_lab=0;
00176     labels=f.load_real_data(NULL, num_lab);
00177     num_labels=num_lab;
00178 
00179     if (!f.is_ok()) {
00180       SG_ERROR( "loading file \"%s\" failed", fname);
00181     }
00182     else
00183     {
00184         SG_INFO( "%ld labels successfully read\n", num_labels);
00185         status=true;
00186     }
00187 
00188     return status;
00189 }
00190 
00191 bool CLabels::save(char* fname)
00192 {
00193     return false;
00194 }

SHOGUN Machine Learning Toolbox - Documentation