SHOGUN
v3.2.0
|
00001 #include <shogun/labels/DenseLabels.h> 00002 #include <shogun/labels/BinaryLabels.h> 00003 #include <shogun/labels/MulticlassLabels.h> 00004 #include <shogun/base/ParameterMap.h> 00005 00006 using namespace shogun; 00007 00008 CMulticlassLabels::CMulticlassLabels() : CDenseLabels() 00009 { 00010 init(); 00011 } 00012 00013 CMulticlassLabels::CMulticlassLabels(int32_t num_labels) : CDenseLabels(num_labels) 00014 { 00015 init(); 00016 } 00017 00018 CMulticlassLabels::CMulticlassLabels(const SGVector<float64_t> src) : CDenseLabels() 00019 { 00020 init(); 00021 set_labels(src); 00022 } 00023 00024 CMulticlassLabels::CMulticlassLabels(CFile* loader) : CDenseLabels(loader) 00025 { 00026 init(); 00027 } 00028 00029 CMulticlassLabels::~CMulticlassLabels() 00030 { 00031 } 00032 00033 void CMulticlassLabels::init() 00034 { 00035 /* for this to work, migration has to be fixed */ 00036 // SG_ADD(&m_multiclass_confidences, "multiclass_confidences", "Vectors of " 00037 // "multiclass confidences", MS_NOT_AVAILABLE); 00038 00039 // m_parameter_map->finalize_map(); 00040 00041 m_multiclass_confidences=SGMatrix<float64_t>(); 00042 } 00043 00044 void CMulticlassLabels::set_multiclass_confidences(int32_t i, 00045 SGVector<float64_t> confidences) 00046 { 00047 REQUIRE(confidences.size()==m_multiclass_confidences.num_rows, 00048 "%s::set_multiclass_confidences(): Length of confidences should " 00049 "match size of the matrix", get_name()); 00050 00051 for (index_t j=0; j<confidences.size(); j++) 00052 m_multiclass_confidences(j,i) = confidences[j]; 00053 } 00054 00055 SGVector<float64_t> CMulticlassLabels::get_multiclass_confidences(int32_t i) 00056 { 00057 SGVector<float64_t> confs(m_multiclass_confidences.num_rows); 00058 for (index_t j=0; j<confs.size(); j++) 00059 confs[j] = m_multiclass_confidences(j,i); 00060 00061 return confs; 00062 } 00063 00064 void CMulticlassLabels::allocate_confidences_for(int32_t n_classes) 00065 { 00066 int32_t n_labels = m_labels.size(); 00067 REQUIRE(n_labels!=0,"%s::allocate_confidences_for(): There should be " 00068 "labels to store confidences", get_name()); 00069 00070 m_multiclass_confidences = SGMatrix<float64_t>(n_classes,n_labels); 00071 } 00072 00073 void CMulticlassLabels::ensure_valid(const char* context) 00074 { 00075 CDenseLabels::ensure_valid(context); 00076 00077 int32_t subset_size=get_num_labels(); 00078 for (int32_t i=0; i<subset_size; i++) 00079 { 00080 int32_t real_i = m_subset_stack->subset_idx_conversion(i); 00081 int32_t label = int32_t(m_labels[real_i]); 00082 00083 if (label<0 || float64_t(label)!=m_labels[real_i]) 00084 { 00085 SG_ERROR("%s%sMulticlass Labels must be in range 0...<nr_classes-1> and integers!\n", 00086 context?context:"", context?": ":""); 00087 } 00088 } 00089 } 00090 00091 ELabelType CMulticlassLabels::get_label_type() const 00092 { 00093 return LT_MULTICLASS; 00094 } 00095 00096 CBinaryLabels* CMulticlassLabels::get_binary_for_class(int32_t i) 00097 { 00098 SGVector<float64_t> binary_labels(get_num_labels()); 00099 00100 bool use_confidences = false; 00101 if ((m_multiclass_confidences.num_rows != 0) && (m_multiclass_confidences.num_cols != 0)) 00102 { 00103 use_confidences = true; 00104 } 00105 if (use_confidences) 00106 { 00107 for (int32_t k=0; k<binary_labels.vlen; k++) 00108 { 00109 int32_t label = get_int_label(k); 00110 float64_t confidence = m_multiclass_confidences(label,k); 00111 binary_labels[k] = label == i ? confidence : -confidence; 00112 } 00113 } 00114 else 00115 { 00116 for (int32_t k=0; k<binary_labels.vlen; k++) 00117 { 00118 int32_t label = get_int_label(k); 00119 binary_labels[k] = label == i ? +1.0 : -1.0; 00120 } 00121 } 00122 return new CBinaryLabels(binary_labels); 00123 } 00124 00125 SGVector<float64_t> CMulticlassLabels::get_unique_labels() 00126 { 00127 /* extract all labels (copy because of possible subset) */ 00128 SGVector<float64_t> unique_labels=get_labels_copy(); 00129 unique_labels.vlen=SGVector<float64_t>::unique(unique_labels.vector, unique_labels.vlen); 00130 00131 SGVector<float64_t> result(unique_labels.vlen); 00132 memcpy(result.vector, unique_labels.vector, 00133 sizeof(float64_t)*unique_labels.vlen); 00134 00135 return result; 00136 } 00137 00138 00139 int32_t CMulticlassLabels::get_num_classes() 00140 { 00141 SGVector<float64_t> unique=get_unique_labels(); 00142 return unique.vlen; 00143 }