File size: 34,109 Bytes
9375c9a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 |
<html><!-- Created using the cpp_pretty_printer from the dlib C++ library. See http://dlib.net for updates. --><head><title>dlib C++ Library - libsvm_io.h</title></head><body bgcolor='white'><pre>
<font color='#009900'>// Copyright (C) 2010 Davis E. King ([email protected])
</font><font color='#009900'>// License: Boost Software License See LICENSE.txt for the full license.
</font><font color='#0000FF'>#ifndef</font> DLIB_LIBSVM_iO_Hh_
<font color='#0000FF'>#define</font> DLIB_LIBSVM_iO_Hh_
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='libsvm_io_abstract.h.html'>libsvm_io_abstract.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>fstream<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>string<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>utility<font color='#5555FF'>></font>
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../algs.h.html'>../algs.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../matrix.h.html'>../matrix.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../string.h.html'>../string.h</a>"
<font color='#0000FF'>#include</font> "<a style='text-decoration:none' href='../svm/sparse_vector.h.html'>../svm/sparse_vector.h</a>"
<font color='#0000FF'>#include</font> <font color='#5555FF'><</font>vector<font color='#5555FF'>></font>
<font color='#0000FF'>namespace</font> dlib
<b>{</b>
<font color='#0000FF'>struct</font> <b><a name='sample_data_io_error'></a>sample_data_io_error</b> : <font color='#0000FF'>public</font> error
<b>{</b>
<b><a name='sample_data_io_error'></a>sample_data_io_error</b><font face='Lucida Console'>(</font><font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> message<font face='Lucida Console'>)</font>: error<font face='Lucida Console'>(</font>message<font face='Lucida Console'>)</font> <b>{</b><b>}</b>
<b>}</b>;
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> sample_type, <font color='#0000FF'>typename</font> label_type, <font color='#0000FF'>typename</font> alloc1, <font color='#0000FF'>typename</font> alloc2<font color='#5555FF'>></font>
<font color='#0000FF'><u>void</u></font> <b><a name='load_libsvm_formatted_data'></a>load_libsvm_formatted_data</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> file_name,
std::vector<font color='#5555FF'><</font>sample_type, alloc1<font color='#5555FF'>></font><font color='#5555FF'>&</font> samples,
std::vector<font color='#5555FF'><</font>label_type, alloc2<font color='#5555FF'>></font><font color='#5555FF'>&</font> labels
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> sample_type::value_type pair_type;
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font><font color='#0000FF'>typename</font> pair_type::first_type<font color='#5555FF'>></font>::type key_type;
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> pair_type::second_type value_type;
<font color='#009900'>// You must use unsigned integral key types in your sparse vectors
</font> <font color='#BB00BB'>COMPILE_TIME_ASSERT</font><font face='Lucida Console'>(</font>is_unsigned_type<font color='#5555FF'><</font>key_type<font color='#5555FF'>></font>::value<font face='Lucida Console'>)</font>;
samples.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
labels.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
ifstream <font color='#BB00BB'>fin</font><font face='Lucida Console'>(</font>file_name.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>fin<font face='Lucida Console'>)</font>
<font color='#0000FF'>throw</font> <font color='#BB00BB'>sample_data_io_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to open file </font>" <font color='#5555FF'>+</font> file_name<font face='Lucida Console'>)</font>;
string line;
istringstream sin;
key_type key;
value_type value;
label_type label;
sample_type sample;
<font color='#0000FF'><u>long</u></font> line_num <font color='#5555FF'>=</font> <font color='#979000'>0</font>;
<font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>fin.<font color='#BB00BB'>peek</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> EOF<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#5555FF'>+</font><font color='#5555FF'>+</font>line_num;
<font color='#BB00BB'>getline</font><font face='Lucida Console'>(</font>fin, line<font face='Lucida Console'>)</font>;
string::size_type pos <font color='#5555FF'>=</font> line.<font color='#BB00BB'>find_first_not_of</font><font face='Lucida Console'>(</font>"<font color='#CC0000'> \t\r\n</font>"<font face='Lucida Console'>)</font>;
<font color='#009900'>// ignore empty lines or comment lines
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>pos <font color='#5555FF'>=</font><font color='#5555FF'>=</font> string::npos <font color='#5555FF'>|</font><font color='#5555FF'>|</font> line[pos] <font color='#5555FF'>=</font><font color='#5555FF'>=</font> '<font color='#FF0000'>#</font>'<font face='Lucida Console'>)</font>
<font color='#0000FF'>continue</font>;
sin.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sin.<font color='#BB00BB'>str</font><font face='Lucida Console'>(</font>line<font face='Lucida Console'>)</font>;
sample.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
sin <font color='#5555FF'>></font><font color='#5555FF'>></font> label;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>sin<font face='Lucida Console'>)</font>
<font color='#0000FF'>throw</font> <font color='#BB00BB'>sample_data_io_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>On line: </font>" <font color='#5555FF'>+</font> <font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>line_num<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> "<font color='#CC0000'>, error while reading file </font>" <font color='#5555FF'>+</font> file_name <font face='Lucida Console'>)</font>;
<font color='#009900'>// eat whitespace
</font> sin <font color='#5555FF'>></font><font color='#5555FF'>></font> ws;
<font color='#0000FF'>while</font> <font face='Lucida Console'>(</font>sin.<font color='#BB00BB'>peek</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> EOF <font color='#5555FF'>&</font><font color='#5555FF'>&</font> sin.<font color='#BB00BB'>peek</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> '<font color='#FF0000'>#</font>'<font face='Lucida Console'>)</font>
<b>{</b>
sin <font color='#5555FF'>></font><font color='#5555FF'>></font> key <font color='#5555FF'>></font><font color='#5555FF'>></font> ws;
<font color='#009900'>// ignore what should be a : character
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>sin.<font color='#BB00BB'>get</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> '<font color='#FF0000'>:</font>'<font face='Lucida Console'>)</font>
<font color='#0000FF'>throw</font> <font color='#BB00BB'>sample_data_io_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>On line: </font>" <font color='#5555FF'>+</font> <font color='#BB00BB'>cast_to_string</font><font face='Lucida Console'>(</font>line_num<font face='Lucida Console'>)</font> <font color='#5555FF'>+</font> "<font color='#CC0000'>, error while reading file </font>" <font color='#5555FF'>+</font> file_name<font face='Lucida Console'>)</font>;
sin <font color='#5555FF'>></font><font color='#5555FF'>></font> value;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>sin <font color='#5555FF'>&</font><font color='#5555FF'>&</font> value <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<b>{</b>
sample.<font color='#BB00BB'>insert</font><font face='Lucida Console'>(</font>sample.<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, <font color='#BB00BB'>make_pair</font><font face='Lucida Console'>(</font>key, value<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
sin <font color='#5555FF'>></font><font color='#5555FF'>></font> ws;
<b>}</b>
samples.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>sample<font face='Lucida Console'>)</font>;
labels.<font color='#BB00BB'>push_back</font><font face='Lucida Console'>(</font>label<font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> sample_type, <font color='#0000FF'>typename</font> alloc<font color='#5555FF'>></font>
<font color='#0000FF'>typename</font> enable_if<font color='#5555FF'><</font>is_const_type<font color='#5555FF'><</font><font color='#0000FF'>typename</font> sample_type::value_type::first_type<font color='#5555FF'>></font> <font color='#5555FF'>></font>::type
<b><a name='fix_nonzero_indexing'></a>fix_nonzero_indexing</b> <font face='Lucida Console'>(</font>
std::vector<font color='#5555FF'><</font>sample_type,alloc<font color='#5555FF'>></font><font color='#5555FF'>&</font> samples
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> sample_type::value_type pair_type;
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font><font color='#0000FF'>typename</font> pair_type::first_type<font color='#5555FF'>></font>::type key_type;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<font color='#0000FF'>return</font>;
<font color='#009900'>// figure out the min index value
</font> key_type min_idx <font color='#5555FF'>=</font> samples[<font color='#979000'>0</font>].<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>></font>first;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
min_idx <font color='#5555FF'>=</font> std::<font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>min_idx, samples[i].<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>></font>first<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now adjust all the samples so that their min index value is zero.
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>min_idx <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<b>{</b>
sample_type temp;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// copy samples[i] into temp but make sure it has a min index of zero.
</font> temp.<font color='#BB00BB'>clear</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>typename</font> sample_type::iterator j;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font>j <font color='#5555FF'>=</font> samples[i].<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; j <font color='#5555FF'>!</font><font color='#5555FF'>=</font> samples[i].<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j<font face='Lucida Console'>)</font>
<b>{</b>
temp.<font color='#BB00BB'>insert</font><font face='Lucida Console'>(</font>temp.<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>, std::<font color='#BB00BB'>make_pair</font><font face='Lucida Console'>(</font>j<font color='#5555FF'>-</font><font color='#5555FF'>></font>first<font color='#5555FF'>-</font>min_idx, j<font color='#5555FF'>-</font><font color='#5555FF'>></font>second<font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
<b>}</b>
<font color='#009900'>// replace the current sample with temp.
</font> samples[i].<font color='#BB00BB'>swap</font><font face='Lucida Console'>(</font>temp<font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// If the "first" values in the std::pair objects are not const then we can modify them
</font><font color='#009900'>// directly and that is what this version of fix_nonzero_indexing() does.
</font> <font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> sample_type, <font color='#0000FF'>typename</font> alloc<font color='#5555FF'>></font>
<font color='#0000FF'>typename</font> disable_if<font color='#5555FF'><</font>is_const_type<font color='#5555FF'><</font><font color='#0000FF'>typename</font> sample_type::value_type::first_type<font color='#5555FF'>></font> <font color='#5555FF'>></font>::type
<b><a name='fix_nonzero_indexing'></a>fix_nonzero_indexing</b> <font face='Lucida Console'>(</font>
std::vector<font color='#5555FF'><</font>sample_type,alloc<font color='#5555FF'>></font><font color='#5555FF'>&</font> samples
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> sample_type::value_type pair_type;
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font><font color='#0000FF'>typename</font> pair_type::first_type<font color='#5555FF'>></font>::type key_type;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<font color='#0000FF'>return</font>;
<font color='#009900'>// figure out the min index value
</font> key_type min_idx <font color='#5555FF'>=</font> samples[<font color='#979000'>0</font>].<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>></font>first;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
min_idx <font color='#5555FF'>=</font> std::<font color='#BB00BB'>min</font><font face='Lucida Console'>(</font>min_idx, samples[i].<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font color='#5555FF'>-</font><font color='#5555FF'>></font>first<font face='Lucida Console'>)</font>;
<font color='#009900'>// Now adjust all the samples so that their min index value is zero.
</font> <font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>min_idx <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>typename</font> sample_type::iterator j;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font>j <font color='#5555FF'>=</font> samples[i].<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; j <font color='#5555FF'>!</font><font color='#5555FF'>=</font> samples[i].<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j<font face='Lucida Console'>)</font>
<b>{</b>
j<font color='#5555FF'>-</font><font color='#5555FF'>></font>first <font color='#5555FF'>-</font><font color='#5555FF'>=</font> min_idx;
<b>}</b>
<b>}</b>
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font><font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This is an overload for sparse vectors
</font> <font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> sample_type, <font color='#0000FF'>typename</font> label_type, <font color='#0000FF'>typename</font> alloc1, <font color='#0000FF'>typename</font> alloc2<font color='#5555FF'>></font>
<font color='#0000FF'>typename</font> disable_if<font color='#5555FF'><</font>is_matrix<font color='#5555FF'><</font>sample_type<font color='#5555FF'>></font>,<font color='#0000FF'><u>void</u></font><font color='#5555FF'>></font>::type <b><a name='save_libsvm_formatted_data'></a>save_libsvm_formatted_data</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> file_name,
<font color='#0000FF'>const</font> std::vector<font color='#5555FF'><</font>sample_type, alloc1<font color='#5555FF'>></font><font color='#5555FF'>&</font> samples,
<font color='#0000FF'>const</font> std::vector<font color='#5555FF'><</font>label_type, alloc2<font color='#5555FF'>></font><font color='#5555FF'>&</font> labels
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> sample_type::value_type pair_type;
<font color='#0000FF'>typedef</font> <font color='#0000FF'>typename</font> basic_type<font color='#5555FF'><</font><font color='#0000FF'>typename</font> pair_type::first_type<font color='#5555FF'>></font>::type key_type;
<font color='#009900'>// You must use unsigned integral key types in your sparse vectors
</font> <font color='#BB00BB'>COMPILE_TIME_ASSERT</font><font face='Lucida Console'>(</font>is_unsigned_type<font color='#5555FF'><</font>key_type<font color='#5555FF'>></font>::value<font face='Lucida Console'>)</font>;
<font color='#009900'>// make sure requires clause is not broken
</font> <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> labels.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,
"<font color='#CC0000'>\t void save_libsvm_formatted_data()</font>"
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t You have to have labels for each sample and vice versa</font>"
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t samples.size(): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t labels.size(): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> labels.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<font face='Lucida Console'>)</font>;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
ofstream <font color='#BB00BB'>fout</font><font face='Lucida Console'>(</font>file_name.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
fout.<font color='#BB00BB'>precision</font><font face='Lucida Console'>(</font><font color='#979000'>14</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>fout<font face='Lucida Console'>)</font>
<font color='#0000FF'>throw</font> <font color='#BB00BB'>sample_data_io_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to open file </font>" <font color='#5555FF'>+</font> file_name<font face='Lucida Console'>)</font>;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
<b>{</b>
fout <font color='#5555FF'><</font><font color='#5555FF'><</font> labels[i];
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'>typename</font> sample_type::const_iterator j <font color='#5555FF'>=</font> samples[i].<font color='#BB00BB'>begin</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; j <font color='#5555FF'>!</font><font color='#5555FF'>=</font> samples[i].<font color='#BB00BB'>end</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>j<font color='#5555FF'>-</font><font color='#5555FF'>></font>second <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
fout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'> </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> j<font color='#5555FF'>-</font><font color='#5555FF'>></font>first <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>:</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> j<font color='#5555FF'>-</font><font color='#5555FF'>></font>second;
<b>}</b>
fout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n</font>";
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>fout<font face='Lucida Console'>)</font>
<font color='#0000FF'>throw</font> <font color='#BB00BB'>sample_data_io_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Error while writing to file </font>" <font color='#5555FF'>+</font> file_name<font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<font color='#009900'>// This is an overload for dense vectors
</font> <font color='#0000FF'>template</font> <font color='#5555FF'><</font><font color='#0000FF'>typename</font> sample_type, <font color='#0000FF'>typename</font> label_type, <font color='#0000FF'>typename</font> alloc1, <font color='#0000FF'>typename</font> alloc2<font color='#5555FF'>></font>
<font color='#0000FF'>typename</font> enable_if<font color='#5555FF'><</font>is_matrix<font color='#5555FF'><</font>sample_type<font color='#5555FF'>></font>,<font color='#0000FF'><u>void</u></font><font color='#5555FF'>></font>::type <b><a name='save_libsvm_formatted_data'></a>save_libsvm_formatted_data</b> <font face='Lucida Console'>(</font>
<font color='#0000FF'>const</font> std::string<font color='#5555FF'>&</font> file_name,
<font color='#0000FF'>const</font> std::vector<font color='#5555FF'><</font>sample_type, alloc1<font color='#5555FF'>></font><font color='#5555FF'>&</font> samples,
<font color='#0000FF'>const</font> std::vector<font color='#5555FF'><</font>label_type, alloc2<font color='#5555FF'>></font><font color='#5555FF'>&</font> labels
<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#009900'>// make sure requires clause is not broken
</font> <font color='#BB00BB'>DLIB_ASSERT</font><font face='Lucida Console'>(</font>samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font> <font color='#5555FF'>=</font><font color='#5555FF'>=</font> labels.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>,
"<font color='#CC0000'>\t void save_libsvm_formatted_data()</font>"
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t You have to have labels for each sample and vice versa</font>"
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t samples.size(): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n\t labels.size(): </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> labels.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>
<font face='Lucida Console'>)</font>;
<font color='#0000FF'>using</font> <font color='#0000FF'>namespace</font> std;
ofstream <font color='#BB00BB'>fout</font><font face='Lucida Console'>(</font>file_name.<font color='#BB00BB'>c_str</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font><font face='Lucida Console'>)</font>;
fout.<font color='#BB00BB'>precision</font><font face='Lucida Console'>(</font><font color='#979000'>14</font><font face='Lucida Console'>)</font>;
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>fout<font face='Lucida Console'>)</font>
<font color='#0000FF'>throw</font> <font color='#BB00BB'>sample_data_io_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Unable to open file </font>" <font color='#5555FF'>+</font> file_name<font face='Lucida Console'>)</font>;
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>unsigned</u></font> <font color='#0000FF'><u>long</u></font> i <font color='#5555FF'>=</font> <font color='#979000'>0</font>; i <font color='#5555FF'><</font> samples.<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>i<font face='Lucida Console'>)</font>
<b>{</b>
fout <font color='#5555FF'><</font><font color='#5555FF'><</font> labels[i];
<font color='#0000FF'>for</font> <font face='Lucida Console'>(</font><font color='#0000FF'><u>long</u></font> j <font color='#5555FF'>=</font> <font color='#979000'>0</font>; j <font color='#5555FF'><</font> samples[i].<font color='#BB00BB'>size</font><font face='Lucida Console'>(</font><font face='Lucida Console'>)</font>; <font color='#5555FF'>+</font><font color='#5555FF'>+</font>j<font face='Lucida Console'>)</font>
<b>{</b>
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font>samples[i]<font face='Lucida Console'>(</font>j<font face='Lucida Console'>)</font> <font color='#5555FF'>!</font><font color='#5555FF'>=</font> <font color='#979000'>0</font><font face='Lucida Console'>)</font>
fout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'> </font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> j <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>:</font>" <font color='#5555FF'><</font><font color='#5555FF'><</font> samples[i]<font face='Lucida Console'>(</font>j<font face='Lucida Console'>)</font>;
<b>}</b>
fout <font color='#5555FF'><</font><font color='#5555FF'><</font> "<font color='#CC0000'>\n</font>";
<font color='#0000FF'>if</font> <font face='Lucida Console'>(</font><font color='#5555FF'>!</font>fout<font face='Lucida Console'>)</font>
<font color='#0000FF'>throw</font> <font color='#BB00BB'>sample_data_io_error</font><font face='Lucida Console'>(</font>"<font color='#CC0000'>Error while writing to file </font>" <font color='#5555FF'>+</font> file_name<font face='Lucida Console'>)</font>;
<b>}</b>
<b>}</b>
<font color='#009900'>// ----------------------------------------------------------------------------------------
</font>
<b>}</b>
<font color='#0000FF'>#endif</font> <font color='#009900'>// DLIB_LIBSVM_iO_Hh_
</font>
</pre></body></html> |