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 * Subset support written (W) 2011 Heiko Strathmann 00010 * Copyright (C) 1999-2009 Fraunhofer Institute FIRST and Max-Planck-Society 00011 */ 00012 00013 #ifndef _LABELS__H__ 00014 #define _LABELS__H__ 00015 00016 #include <shogun/lib/common.h> 00017 #include <shogun/io/SGIO.h> 00018 #include <shogun/io/File.h> 00019 #include <shogun/base/SGObject.h> 00020 #include <shogun/features/Subset.h> 00021 00022 namespace shogun 00023 { 00024 00025 class CFile; 00026 00037 class CLabels : public CSGObject 00038 { 00039 public: 00041 CLabels(); 00042 00047 CLabels(int32_t num_labels); 00048 00053 CLabels(SGVector<float64_t> src); 00054 00059 CLabels(CFile* loader); 00060 00062 virtual ~CLabels(); 00063 00070 virtual void load(CFile* loader); 00071 00078 virtual void save(CFile* writer); 00079 00088 inline bool set_label(int32_t idx, float64_t label) 00089 { 00090 int32_t real_num=subset_idx_conversion(idx); 00091 if (labels.vector && real_num<get_num_labels()) 00092 { 00093 labels.vector[real_num]=label; 00094 return true; 00095 } 00096 else 00097 return false; 00098 } 00099 00108 inline bool set_int_label(int32_t idx, int32_t label) 00109 { 00110 int32_t real_num=subset_idx_conversion(idx); 00111 if (labels.vector && real_num<get_num_labels()) 00112 { 00113 labels.vector[real_num]= (float64_t) label; 00114 return true; 00115 } 00116 else 00117 return false; 00118 } 00119 00127 inline float64_t get_label(int32_t idx) 00128 { 00129 int32_t real_num=subset_idx_conversion(idx); 00130 ASSERT(labels.vector && idx<get_num_labels()); 00131 return labels.vector[real_num]; 00132 } 00133 00141 inline int32_t get_int_label(int32_t idx) 00142 { 00143 int32_t real_num=subset_idx_conversion(idx); 00144 ASSERT(labels.vector && idx<get_num_labels()); 00145 if (labels.vector[real_num] != float64_t((int32_t(labels.vector[real_num])))) 00146 SG_ERROR("label[%d]=%g is not an integer\n", labels.vector[real_num]); 00147 00148 return int32_t(labels.vector[real_num]); 00149 } 00150 00157 bool is_two_class_labeling(); 00158 00165 int32_t get_num_classes(); 00166 00173 SGVector<float64_t> get_labels(); 00174 00181 void set_labels(SGVector<float64_t> v); 00182 00188 void set_to_one(); 00189 00197 SGVector<int32_t> get_int_labels(); 00198 00206 SGVector<float64_t> get_classes(); 00207 00215 void set_int_labels(SGVector<int32_t> labels); 00216 00221 inline int32_t get_num_labels() 00222 { 00223 return m_subset ? m_subset->get_size() : labels.vlen; 00224 } 00225 00227 inline virtual const char* get_name() const { return "Labels"; } 00228 00233 virtual void set_subset(CSubset* subset); 00234 00236 virtual void remove_subset(); 00237 00243 inline index_t subset_idx_conversion(index_t idx) const 00244 { 00245 return m_subset ? m_subset->subset_idx_conversion(idx) : idx; 00246 } 00247 00248 private: 00249 void init(); 00250 00251 protected: 00253 SGVector<float64_t> labels; 00254 00256 int32_t m_num_classes; 00257 00258 private: 00259 /* subset class to enable subset support for this class */ 00260 CSubset* m_subset; 00261 }; 00262 } 00263 #endif