/
nexamples.hpp
354 lines (285 loc) · 13.6 KB
/
nexamples.hpp
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
// nexamples.hpp - 2016 - Atlee Brink
// Machine-Learning Example-Dataset module
#pragma once
#include "nrandom.hpp"
#include "nutil.hpp"
#include <algorithm>
#include <cmath>
#include <iostream>
#include <fstream>
#include <limits>
//#include <random>
#include <set>
#include <string>
#include <sstream>
#include <utility>
#include <vector>
namespace nexamples {
using namespace std;
using namespace nrandom;
typedef float feature_t; // pseudo-continuous or discrete features
typedef int label_t; // discrete labels
typedef vector< feature_t > featurevector_t;
class cexampleset {
public:
vector< string > names; // should match size of featurevector_t
vector< featurevector_t > featurevectors;
string labelname; // may be empty
vector< label_t > labels; // may be empty, else should be same size as examples
set< label_t > labelset; // contains all unique labels
vector< string > exnames; // excluded feature names in order of column
vector< vector< string > > exfeaturevectors; // excluded feature values
bool statsed = false, normalized = false;
vector< double > featuremeans; // average of each feature
vector< double > featurestddevs; // standard deviation of each feature
void
computefeaturestats() {
// compute featuremeans and featurestddevs
if( statsed ) return;
featuremeans = vector< double >( names.size(), 0.0 );
// sums
for( const auto &X : featurevectors ) {
auto meansi = featuremeans.begin();
for( const auto &x : X ) *meansi++ += x;
}
// means
double rnum = 1.0 / featurevectors.size();
for( auto &mean : featuremeans ) mean *= rnum;
featurestddevs = vector< double >( names.size(), 0.0 );
// sum of squared deviations
for( const auto &X : featurevectors ) {
auto meansi = featuremeans.cbegin();
auto stddevsi = featurestddevs.begin();
for( const auto &x : X ) {
auto dev = *meansi++ - x;
*stddevsi++ += dev * dev;
}
}
// standard deviations (square root of mean of squared deviations)
for( auto &stddev : featurestddevs ) stddev = sqrt( stddev * rnum );
// set flag that stats have been computed
statsed = true;
}
bool
loadfromfile(
const string &filename,
string &labelcolumnname, // if empty and islabeled, then will be filled on return
bool &islabeled, // in: must find label column; out: whether it was found
const set< string > &excludefeatures
) {
// reset flags
statsed = false;
normalized = false;
// check parameters a bit
if( excludefeatures.find( labelcolumnname )
!= excludefeatures.end() ) {
cerr << "was asked to exclude label column, but I can't do that" << endl;
return false;
}
// open file
ifstream infile( filename );
if( !infile ) {
cerr << "error opening file: " << filename << endl;
return false;
}
// prepare column indices
bool labelfound = false;
size_t numfilecolumns = 0;
enum coltype : char { EXCLUDE, FEATURE, LABEL };
vector< coltype > columnmap;
{ // read header line
stringstream linestream( nutil::getline( infile ) );
size_t index = 0;
// read column names: learn indices of label and excluded columns
for( string token; getline( linestream, token, ',' ); index++ ) {
if( token == labelcolumnname ) {
labelfound = true;
columnmap.push_back( LABEL );
}
else {
if( excludefeatures.find( token ) != excludefeatures.end() ) {
exnames.push_back( move(token) );
columnmap.push_back( EXCLUDE );
}
else {
names.push_back( move(token) );
columnmap.push_back( FEATURE );
}
}
}
numfilecolumns = index;
// decide whether there should be a label column present,
// and which one it is (if needed):
if( labelcolumnname.empty() ) {
if( islabeled ) {
// since no name was specified, assume last column is the label
columnmap[ numfilecolumns - 1 ] = LABEL;
labelcolumnname = names.back();
//cout << "assuming label column is: " << labelcolumnname << "\n";
names.pop_back();
}
}
else {
// else a label name was specified
if( !labelfound ) { // but it wasn't found
if( islabeled ) { // and this is a problem
cerr << "couldn't find label column: \""
<< labelcolumnname << "\", check data!" << endl;
return false;
} // else we don't care that it wasn't found:
// labels and labelset will remain empty
}
else { // else it was found, so set islabeled = true
islabeled = true;
}
}
if( exnames.size() != excludefeatures.size() ) {
cerr << "couldn't find all excluded columns, check data!" << endl;
return false;
}
} // done reading header
labelname = labelcolumnname;
// read values
size_t linenum = 2;
for( string line; getline( infile, line ); linenum++ ) {
featurevector_t featurevector;
vector< string > exfeaturevector;
stringstream linestream( line );
size_t index = 0;
for( string token; getline( linestream, token, ',' ); index++ ) {
if( index >= numfilecolumns ) {
cerr << filename << ": wrong numer of columns on line: "
<< linenum << "\n"
<< "expected " << numfilecolumns
<< ", found " << index << " instead" << endl;
return false;
}
switch( columnmap[ index ] ) {
case LABEL: {
label_t label;
try {
label = stoi( token );
} catch( invalid_argument &ex ) {
cerr << filename
<< ": line " << linenum
<< ": column " << index
<< ": trying to read int, but: "
<< ex.what() << endl;
return false;
}
labels.push_back( label );
labelset.insert( label );
break;
}
case FEATURE: {
feature_t featurevalue;
try {
featurevalue = stof( token );
} catch( invalid_argument &ex ) {
cerr << filename
<< ": line " << linenum
<< ": column " << index
<< ": trying to read float, but: "
<< ex.what() << endl;
return false;
}
featurevector.push_back( featurevalue );
break;
}
default: { // EXCLUDE
exfeaturevector.push_back( token );
break;
}
}
}
if( index != numfilecolumns ) {
cerr << filename
<< ": wrong number of columns on line: " << linenum << "\n"
<< "expected " << numfilecolumns << ", found " << index << endl;
return false;
}
featurevectors.push_back( move( featurevector ) );
if( !exfeaturevector.empty() ) {
exfeaturevectors.push_back( move( exfeaturevector ) );
}
} // for
// check for some io error that means this file didn't actually read properly
if( infile.bad() ) {
cerr << "IO error reading file: " << filename << endl;
return false;
}
// success!
return true;
} // loadfromfile
void
normalizefeatures() {
// normalize each feature separately using its mean and standard deviation:
// feature <- (feature - mean) / stddev
if( normalized ) return;
if( !statsed ) computefeaturestats();
// precompute reciprocals for fastness
vector< double > rstddevs( names.size(), 0.0 );
auto rstddevsi = rstddevs.begin();
for( const auto &stddev : featurestddevs ) {
*rstddevsi++ = stddev == 0.0 ? 1.0 : 1.0 / stddev;
}
// process all feature vectors
for( auto &X : featurevectors ) {
auto meansi = featuremeans.cbegin();
auto rstddevsi = rstddevs.cbegin();
for( auto &x : X ) x = (x - *meansi++) * *rstddevsi++;
}
// set flag
statsed = false; // old stats won't apply anymore
normalized = true;
}
void
normalizefeatures(
const vector< double > &means,
const vector< double > &stddevs
) {
featuremeans = means;
featurestddevs = stddevs;
statsed = true;
normalizefeatures();
}
vector< cexampleset >
split(
double proportionfortraining
) {
if( proportionfortraining < 0.0 ) proportionfortraining = 0.0;
else if( proportionfortraining > 1.0 ) proportionfortraining = 1.0;
size_t numexamples = labels.size();
// build ordered list of indices
vector< size_t > indices( numexamples );
for( size_t i = 0; i < numexamples; i++ ) indices[i] = i;
// shuffle the list of indices
for( size_t i = 0; i + 1 < numexamples; i++ ) {
size_t target = i + urand_sizet( twister ) % (numexamples - i);
swap( indices[ i ], indices[ target ] );
}
// calculate divider
size_t divider = (size_t)( proportionfortraining * numexamples );
// initialize subsets
vector< cexampleset > subsets( 2 );
for( cexampleset &subset : subsets ) {
subset.names = names;
subset.labelset = labelset;
}
subsets[0].featurevectors.reserve( divider );
subsets[0].labels.reserve( divider );
subsets[1].featurevectors.reserve( numexamples - divider );
subsets[1].labels.reserve( numexamples - divider );
// fill subsets
for( size_t i = 0; i < divider; i++ ) {
subsets[0].featurevectors.push_back( featurevectors[ indices[ i ] ] );
subsets[0].labels.push_back( labels[ indices[ i ] ] );
}
for( size_t i = divider; i < numexamples; i++ ) {
subsets[1].featurevectors.push_back( featurevectors[ indices[ i ] ] );
subsets[1].labels.push_back( labels[ indices[ i ] ] );
}
return subsets;
}
}; // class cexampleset
} // namespace nexamples