ECOCPAK v0.9
|
00001 // Copyright (C) 2011 the authors listed below 00002 // http://ecocpak.sourceforge.net 00003 // 00004 // Authors: 00005 // - Dimitrios Bouzas (bouzas at ieee dot org) 00006 // - Nikolaos Arvanitopoulos (niarvani at ieee dot org) 00007 // - Anastasios Tefas (tefas at aiia dot csd dot auth dot gr) 00008 // 00009 // This file is part of the ECOC PAK C++ library. It is 00010 // provided without any warranty of fitness for any purpose. 00011 // 00012 // You can redistribute this file and/or modify it under 00013 // the terms of the GNU Lesser General Public License (LGPL) 00014 // as published by the Free Software Foundation, either 00015 // version 3 of the License or (at your option) any later 00016 // version. 00017 // (see http://www.opensource.org/licenses for more info) 00018 00019 00022 00023 00037 double 00038 custom_metric 00039 ( 00040 rowvec test_code, 00041 rowvec class_code 00042 ) 00043 { 00044 // ================================================================== // 00045 // || DECLARATIONS - INITIALIZATION || // 00046 // ================================================================== // 00047 00048 // output distance -- initialize 00049 double distance = 0.0; 00050 00051 // ========== UNCOMMENT PIECE OF CODE IF NECESSARY ========== // 00052 // - size of codewords // 00053 // ========================================================== // 00054 00055 // %% UNCOMMENT BEGINNING %% 00056 // const unisigned int d = class_code.n_cols; 00057 // %% UNCOMMENT END %% 00058 00059 // ================================================================== // 00060 // || INFO: If you want to use the raw C++ pointer to 1D arrays of || // 00061 // || type "double*" or "unsigned int*" you have to uncomment|| // 00062 // || the pieces of code below. || // 00063 // || || // 00064 // || - The memptr() member function is used to obtain a || // 00065 // || raw pointer to the memory used for storing || // 00066 // || elements. || // 00067 // || || // 00068 // || - As soon as the size of the matrix/vector is || // 00069 // || changed, the pointer is no longer valid. || // 00070 // || || // 00071 // || - Data for matrices is stored in a column-by-column || // 00072 // || order. || // 00073 // ================================================================== // 00074 00075 // ========== UNCOMMENT PIECE OF CODE IF NECESSARY ========== // 00076 // - Raw pointer to test codeword array // 00077 // ========================================================== // 00078 00079 // %% UNCOMMENT BEGINNING %% 00080 // double* test_mem = test_code.memptr(); 00081 // %% UNCOMMENT END %% 00082 00083 // ========== UNCOMMENT PIECE OF CODE IF NECESSARY ========== // 00084 // - Raw pointer to class codeword array // 00085 // ========================================================== // 00086 00087 // %% UNCOMMENT BEGINNING %% 00088 // double* class_mem = class_code.memptr(); 00089 // %% UNCOMMENT END %% 00090 00091 // ================================================================== // 00092 // || START YOUR CODE HERE || // 00093 // ================================================================== // 00094 00095 00096 // ================================================================== // 00097 // || END YOUR CODE HERE || // 00098 // ================================================================== // 00099 00100 // return computed distance 00101 return distance; 00102 } 00103 00104 00105 00122 u32 00123 custom_decoding 00124 ( 00125 const vector<Classifier*>& classifiers_vector, 00126 const vector<ClassData>& classes_vector, 00127 const imat& coding_matrix, 00128 const mat& test_samples, 00129 const ucolvec& test_set_labels, 00130 uvec& predictions, 00131 umat& confussion 00132 ) 00133 { 00134 #ifdef NCURSES_OUTPUT 00135 // print status header 00136 mvaddstr(3, 0, "Computing Custom Decoding:"); 00137 refresh(); 00138 #endif 00139 00140 // ================================================================== // 00141 // || DECLARATIONS - INITIALIZATION || // 00142 // ================================================================== // 00143 00144 // number of test set samples 00145 const u32 n_test_samples = test_samples.n_rows; 00146 00147 // number of classifiers 00148 const u32 n_classifiers = classifiers_vector.size(); 00149 00150 // number of classes -- subclasses 00151 const u32 n_classes = classes_vector.size(); 00152 00153 // number of missclassified test samples -- initialization 00154 u32 n_missed = 0; 00155 00156 // test samples codewords matrix -- initialization, allocation 00157 mat test_codes = zeros<mat>(n_test_samples, n_classifiers); 00158 00159 // ================================================================== // 00160 // || CONSTRUCT TEST SAMPLES CODEWORD MATRIX || // 00161 // ================================================================== // 00162 00163 // iterate through test samples 00164 for(u32 i = 0; i < n_test_samples; i++) 00165 { 00166 00167 // ============================================================== // 00168 // || CONSTRUCT i-TH TEST SAMPLE CODEWORD || // 00169 // ============================================================== // 00170 00171 // iterate through vector of available classifiers and compute 00172 // prediction of each classifier for current i-th test sample 00173 for(u32 j = 0; j < n_classifiers; j++) 00174 { 00175 // assign prediction value of j-th classifier for i-th 00176 // test feature vector 00177 test_codes(i,j) = 00178 classifiers_vector[j]->predict(test_samples.row(i)); 00179 } 00180 00181 // ============================================================== // 00182 // || DECODE CREATED CODEWORD AND ASSIGN PREDICTION LABEL || // 00183 // ============================================================== // 00184 00185 // winning coding matrix row -- initialize to first class 00186 u32 winner = 1; 00187 00188 // winning distance -- initialize to first class 00189 double win_dist = 00190 custom_metric 00191 ( 00192 test_codes.row(i), 00193 conv_to<rowvec>::from(coding_matrix.row(0)) 00194 ); 00195 00196 // iterate through coding matrix to find the closest class codeword 00197 // i.e., the closest row of the coding matrix for current i-th 00198 // test sample previously created codeword for rest of coding matrix 00199 // rows 00200 for(u32 j = 1; j < n_classes; j++) 00201 { 00202 // compute current distance -- according to user difined metric 00203 double tmp_dist = 00204 custom_metric 00205 ( 00206 test_codes.row(i), 00207 conv_to<rowvec>::from(coding_matrix.row(j)) 00208 ); 00209 00210 // update best distance and winning row index if necessary 00211 if(tmp_dist < win_dist) 00212 { 00213 win_dist = tmp_dist; 00214 winner = j + 1; 00215 } 00216 else 00217 { 00218 // in ties random assignment for unbiased estimation 00219 if(tmp_dist == win_dist) 00220 { 00221 if((time(NULL) % 2) == 0) 00222 { 00223 win_dist = tmp_dist; 00224 winner = j + 1; 00225 } 00226 00227 } 00228 00229 } 00230 00231 } 00232 00233 predictions[i] = winner; 00234 00235 // if current test sample missclassified increase missclassified 00236 // counter 00237 if(winner != classes_vector[test_set_labels[i] - 1].ClassLabel()) 00238 { 00239 n_missed++; 00240 } 00241 00242 confussion 00243 ( 00244 classes_vector[winner - 1].ClassLabel(), 00245 classes_vector[test_set_labels[i] - 1].ClassLabel() 00246 )++; 00247 00248 } 00249 00250 return n_missed; 00251 } 00252 00253 00254