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 00024 00025 // alias to svm_node** 00026 typedef struct svm_node** sparse_mat; 00027 00028 00029 00041 sparse_mat 00042 dense_to_sparse 00043 ( 00044 const mat& X 00045 ) 00046 { 00047 // number of rows 00048 const u32 n_rows = X.n_rows; 00049 00050 // sparse matrix X 00051 struct svm_node** sparse_matrix = Malloc(struct svm_node *, n_rows); 00052 00053 // for each row 00054 for(u32 i = 0; i < n_rows; i++) 00055 { 00056 // indices of non zero elements 00057 ucolvec inds = find(X.row(i)); 00058 00059 // number of non zero elements 00060 const u32 n_elem = inds.n_elem; 00061 00062 // allocate space for sparse row 00063 struct svm_node* x_space = Malloc(struct svm_node, n_elem + 1); 00064 00065 // set last row element of sparse representation 00066 x_space[n_elem].index = -1; 00067 x_space[n_elem].value = 0; 00068 00069 // fill in the rest of the elements 00070 for(u32 j = 0; j < n_elem; j++) 00071 { 00072 x_space[j].index = inds[j]; 00073 x_space[j].value = X(i, inds[j]); 00074 } 00075 00076 sparse_matrix[i] = x_space; 00077 } 00078 00079 return sparse_matrix; 00080 } 00081 00082 00083 00087 void 00088 delete_sparse_matrix(sparse_mat A, const u32 n_rows) 00089 { 00090 // free sparse matrix 00091 if(A != 0) 00092 { 00093 for(u32 i = 0; i < n_rows; i++) 00094 { 00095 svm_node* tmp_node = A[i]; 00096 00097 if(tmp_node != 0) 00098 { 00099 delete [] tmp_node; 00100 } 00101 00102 } 00103 00104 delete [] A; 00105 } 00106 00107 } 00108 00109 00110 00114 void 00115 delete_sparse_matrix(sparse_mat A, const u32 n_rows, const struct svm_model* model) 00116 { 00117 // free sparse matrix 00118 if(A != 0) 00119 { 00120 00121 for(u32 i = 0; i < n_rows; i++) 00122 { 00123 //bool erase = true; 00124 00125 //for(u32 j = 0; j < model->l; j++) 00126 // { 00127 // if(model->SV[j] == A[i]) 00128 // { 00129 // erase = false; 00130 // break; 00131 // } 00132 00133 // } 00134 00135 // if(erase == true) 00136 // { 00137 svm_node* tmp_node = A[i]; 00138 00139 // if(tmp_node != 0) 00140 // { 00141 free(tmp_node); 00142 // } 00143 00144 // } 00145 00146 } 00147 00148 free(A); 00149 } 00150 00151 } 00152 00153 00154 00155 svm_model 00156 modelcpy(svm_model* m) 00157 { 00158 // output LIBSVM model 00159 svm_model out; 00160 00161 // copy svm parameters 00162 out.param = m->param; 00163 00164 // copy number of classes 00165 out.nr_class = m->nr_class; 00166 00167 // copy total number of support vectors 00168 out.l = m->l; 00169 00170 // copy free sentinel 00171 out.free_sv = m->free_sv; 00172 00173 // copy label for each class 00174 out.label = new int[2]; 00175 out.label[0] = m->label[0]; 00176 out.label[1] = m->label[1]; 00177 00178 // copy number of support vectors for each class 00179 out.nSV = new int[2]; 00180 out.nSV[0] = m->nSV[0]; 00181 out.nSV[1] = m->nSV[1]; 00182 00183 // copy coefficients of support vectors in decision functions 00184 out.sv_coef = new double*[1]; 00185 out.sv_coef[0] = new double[out.l]; 00186 memcpy(out.sv_coef[0], m->sv_coef[0], out.l * sizeof(double)); 00187 00188 // copy biases in decision functions -- one rho since 2 classes 00189 out.rho = new double[1]; 00190 out.rho[0] = m->rho[0]; 00191 00192 // --- copy support vectors --- // 00193 00194 out.SV = new svm_node*[out.l]; 00195 00196 for(u32 i = 0; i < out.l; i++) 00197 { 00198 vector<svm_node> tmpSV; 00199 svm_node tmp = m->SV[i][0]; 00200 00201 u32 k = 0; 00202 while(tmp.index != -1) 00203 { 00204 tmpSV.push_back(tmp); 00205 k++; 00206 00207 tmp = m->SV[i][k]; 00208 } 00209 00210 out.SV[i] = new svm_node[tmpSV.size() + 1]; 00211 memcpy(out.SV[i], &tmpSV[0], tmpSV.size() * sizeof(svm_node)); 00212 out.SV[i][tmpSV.size()].index = -1; 00213 } 00214 00215 // copy pairwise probabilities information 00216 if(m->probA != NULL) 00217 { 00218 out.probA = new double[1]; 00219 out.probA[0] = m->probA[0]; 00220 } 00221 else 00222 { 00223 out.probA = 0; 00224 } 00225 00226 if(m->probB != NULL) 00227 { 00228 out.probB = new double[1]; 00229 out.probB[0] = m->probB[0]; 00230 } 00231 else 00232 { 00233 out.probB = 0; 00234 } 00235 00236 return out; 00237 } 00238 00239 00240