| 1 |
//------------------------------------------------------------------------------ |
| 2 |
// name: x-validate.ck |
| 3 |
// desc: a cross validation example using extracted features |
| 4 |
// |
| 5 |
// version: need chuck version 1.5.0.0 or higher |
| 6 |
// sorting: part of ChAI (ChucK for AI) |
| 7 |
// |
| 8 |
// USAGE: this is purely for cross validation on extracted features, so ... |
| 9 |
// run chuck in non-real-time mode (this actually can be much |
| 10 |
// faster than real-time mode, since it doesn't synch to audio): |
| 11 |
// |
| 12 |
// cross validation from FILE |
| 13 |
// > chuck --silent x-validate.ck:FILE |
| 14 |
// |
| 15 |
// date: Spring 2023 |
| 16 |
// authors: Yikai Li |
| 17 |
// Ge Wang (https://ccrma.stanford.edu/~ge/) |
| 18 |
//------------------------------------------------------------------------------ |
| 19 |
|
| 20 |
// input: pre-extracted features file with labels |
| 21 |
"" => string FEATURES_FILE; |
| 22 |
// if have arguments, override filename |
| 23 |
if( me.args() > 0 ) me.arg(0) => FEATURES_FILE; |
| 24 |
else |
| 25 |
{ <<< "[usage]: chuck --silent x-validate:FILE", "" >>>; me.exit();} |
| 26 |
//------------------------------------------------------------------------------ |
| 27 |
// expected features file format: |
| 28 |
//------------------------------------------------------------------------------ |
| 29 |
// VALUE VALUE ... VALUE LABEL |
| 30 |
// VALUE VALUE ... VALUE LABEL |
| 31 |
// ... ... ... ... LABEL |
| 32 |
// VALUE VALUE ... VALUE LABEL |
| 33 |
//------------------------------------------------------------------------------ |
| 34 |
|
| 35 |
|
| 36 |
//------------------------------------------------------------------------------ |
| 37 |
// load feature data; read important global values like numPoints and numCoeffs |
| 38 |
//------------------------------------------------------------------------------ |
| 39 |
// values to be read from file |
| 40 |
0 => int numPoints; // number of points in data |
| 41 |
0 => int numCoeffs; // number of dimensions in data |
| 42 |
// file read PART 1: read over the file to get numPoints and numCoeffs |
| 43 |
loadFile( FEATURES_FILE ) @=> FileIO @ fin; |
| 44 |
// check |
| 45 |
if( !fin.good() ) me.exit(); |
| 46 |
|
| 47 |
// labels of all data points |
| 48 |
string inLabels[numPoints]; |
| 49 |
// label indices of all data points |
| 50 |
int inLabelsInt[inLabels.size()]; |
| 51 |
// feature vectors of data points |
| 52 |
float inFeatures[numPoints][numCoeffs]; |
| 53 |
// keys |
| 54 |
string labels[0]; |
| 55 |
// use as map: labels to numbers |
| 56 |
int label2int[0]; |
| 57 |
|
| 58 |
|
| 59 |
//------------------------------------------------------------------------------ |
| 60 |
// read the data |
| 61 |
//------------------------------------------------------------------------------ |
| 62 |
readData( fin ); |
| 63 |
|
| 64 |
|
| 65 |
//------------------------------------------------------------------------------ |
| 66 |
// set up our KNN object to use for classification |
| 67 |
// (KNN2 is a fancier version of the KNN object) |
| 68 |
// -- run KNN2.help(); in a separate program to see its available functions -- |
| 69 |
//------------------------------------------------------------------------------ |
| 70 |
KNN2 knn; |
| 71 |
// k nearest neighbors |
| 72 |
10 => int K; |
| 73 |
// results vector |
| 74 |
float knnResult[labels.size()]; |
| 75 |
|
| 76 |
|
| 77 |
//------------------------------------------------------------------------------ |
| 78 |
// cross validation |
| 79 |
//------------------------------------------------------------------------------ |
| 80 |
// number of folds |
| 81 |
20 => int numFolds; |
| 82 |
// number of folds to use for testing |
| 83 |
4 => int numTestFolds; |
| 84 |
// number of folds to use for training |
| 85 |
numFolds - numTestFolds => int numTrainFolds; |
| 86 |
// number of points in each fold |
| 87 |
(numPoints / numFolds) $ int => int numPointsPerFold; |
| 88 |
// feature vectors of training data |
| 89 |
float trainFeatures[numTrainFolds * numPointsPerFold][numCoeffs]; |
| 90 |
// labels of training data |
| 91 |
int trainLabelsInt[numTrainFolds * numPointsPerFold]; |
| 92 |
// feature vectors of testing data |
| 93 |
float testFeatures[numTestFolds * numPointsPerFold][numCoeffs]; |
| 94 |
// labels of testing data |
| 95 |
int testLabelsInt[numTestFolds * numPointsPerFold]; |
| 96 |
// normalize the data |
| 97 |
normalizeData(); |
| 98 |
// shuffle the data |
| 99 |
shuffleData(); |
| 100 |
// cross validation |
| 101 |
for( 0 => int i; i < numFolds / numTestFolds; i++) |
| 102 |
{ |
| 103 |
// prepare training and testing data |
| 104 |
prepareData( i ); |
| 105 |
// train |
| 106 |
knn.train( trainFeatures, trainLabelsInt ); |
| 107 |
// test |
| 108 |
0.0 => float accuracy; |
| 109 |
for( 0 => int j; j < testLabelsInt.size(); j++ ) |
| 110 |
{ |
| 111 |
// predict |
| 112 |
knn.predict( testFeatures[j], K, knnResult ); |
| 113 |
// aggregate accuracy |
| 114 |
knnResult[ testLabelsInt[j] ] +=> accuracy; |
| 115 |
} |
| 116 |
// print accuracy |
| 117 |
chout <= "fold " + i + " accuracy: " + ( accuracy / testLabelsInt.size() ) <= IO.newline(); |
| 118 |
} |
| 119 |
|
| 120 |
|
| 121 |
//------------------------------------------------------------------------------ |
| 122 |
// function: normalizeData() |
| 123 |
//------------------------------------------------------------------------------ |
| 124 |
fun void normalizeData() |
| 125 |
{ |
| 126 |
// for each dimension |
| 127 |
for( 0 => int i; i < numCoeffs; i++ ) |
| 128 |
{ |
| 129 |
// find min and max |
| 130 |
inFeatures[0][i] => float min; |
| 131 |
inFeatures[0][i] => float max; |
| 132 |
for( 1 => int j; j < numPoints; j++ ) |
| 133 |
{ |
| 134 |
if( inFeatures[j][i] < min ) inFeatures[j][i] => min; |
| 135 |
if( inFeatures[j][i] > max ) inFeatures[j][i] => max; |
| 136 |
} |
| 137 |
max - min => float range; |
| 138 |
// normalize |
| 139 |
for( 0 => int j; j < numPoints; j++ ) |
| 140 |
(inFeatures[j][i] - min) / range => inFeatures[j][i]; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
|
| 145 |
//------------------------------------------------------------------------------ |
| 146 |
// function: shuffleData() |
| 147 |
//------------------------------------------------------------------------------ |
| 148 |
fun void shuffleData() |
| 149 |
{ |
| 150 |
// prepare swap data |
| 151 |
float swapFeatures[numCoeffs]; |
| 152 |
int swapLabelInt; |
| 153 |
// shuffle the data |
| 154 |
for( numPoints - 1 => int i; i > 0; i-- ) |
| 155 |
{ |
| 156 |
// random index |
| 157 |
Math.random2( 0, i ) => int j; |
| 158 |
// swap features |
| 159 |
for( 0 => int k; k < numCoeffs; k++ ) |
| 160 |
{ |
| 161 |
inFeatures[i][k] => swapFeatures[k]; |
| 162 |
inFeatures[j][k] => inFeatures[i][k]; |
| 163 |
swapFeatures[k] => inFeatures[j][k]; |
| 164 |
} |
| 165 |
// swap labels |
| 166 |
inLabelsInt[i] => swapLabelInt; |
| 167 |
inLabelsInt[j] => inLabelsInt[i]; |
| 168 |
swapLabelInt => inLabelsInt[j]; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
|
| 173 |
//------------------------------------------------------------------------------ |
| 174 |
// function: prepareData( int fold ) |
| 175 |
//------------------------------------------------------------------------------ |
| 176 |
fun void prepareData( int fold ) |
| 177 |
{ |
| 178 |
// test indices |
| 179 |
fold * numTestFolds * numPointsPerFold => int testStart; |
| 180 |
testStart + numTestFolds * numPointsPerFold => int testEnd; |
| 181 |
// index |
| 182 |
0 => int train_i; |
| 183 |
0 => int test_i; |
| 184 |
// prepare training and testing data |
| 185 |
for( 0 => int i; i < numPoints; i++ ) |
| 186 |
{ |
| 187 |
// test |
| 188 |
if( i >= testStart && i < testEnd ) |
| 189 |
{ |
| 190 |
// copy features |
| 191 |
for( 0 => int j; j < numCoeffs; j++ ) |
| 192 |
inFeatures[i][j] => testFeatures[test_i][j]; |
| 193 |
// copy label |
| 194 |
inLabelsInt[i] => testLabelsInt[test_i]; |
| 195 |
// increment |
| 196 |
test_i++; |
| 197 |
} |
| 198 |
// train |
| 199 |
else |
| 200 |
{ |
| 201 |
// copy features |
| 202 |
for( 0 => int j; j < numCoeffs; j++ ) |
| 203 |
inFeatures[i][j] => trainFeatures[train_i][j]; |
| 204 |
// copy label |
| 205 |
inLabelsInt[i] => trainLabelsInt[train_i]; |
| 206 |
// increment |
| 207 |
train_i++; |
| 208 |
} |
| 209 |
} |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
//------------------------------------------------------------------------------ |
| 214 |
// function: load data file |
| 215 |
//------------------------------------------------------------------------------ |
| 216 |
fun FileIO loadFile( string filepath ) |
| 217 |
{ |
| 218 |
// reset |
| 219 |
0 => numPoints; |
| 220 |
0 => numCoeffs; |
| 221 |
|
| 222 |
// load data |
| 223 |
FileIO fio; |
| 224 |
if( !fio.open( filepath, FileIO.READ ) ) |
| 225 |
{ |
| 226 |
// error |
| 227 |
<<< "cannot open file:", filepath >>>; |
| 228 |
// close |
| 229 |
fio.close(); |
| 230 |
// return |
| 231 |
return fio; |
| 232 |
} |
| 233 |
|
| 234 |
string str; |
| 235 |
string line; |
| 236 |
// read file int array |
| 237 |
while( fio.more() ) |
| 238 |
{ |
| 239 |
// read each line |
| 240 |
fio.readLine().trim() => str; |
| 241 |
// check if empty line |
| 242 |
if( str != "" ) |
| 243 |
{ |
| 244 |
numPoints++; |
| 245 |
str => line; |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
// a string tokenizer |
| 250 |
StringTokenizer tokenizer; |
| 251 |
// set to last non-empty line |
| 252 |
tokenizer.set( line ); |
| 253 |
// -1 (to account for label) |
| 254 |
-1 => numCoeffs; |
| 255 |
// see how many, including label name |
| 256 |
while( tokenizer.more() ) |
| 257 |
{ |
| 258 |
tokenizer.next(); |
| 259 |
numCoeffs++; |
| 260 |
} |
| 261 |
|
| 262 |
// check |
| 263 |
if( numPoints == 0 || numCoeffs <= 0 ) |
| 264 |
{ |
| 265 |
<<< "no data in file:", filepath >>>; |
| 266 |
fio.close(); |
| 267 |
return fio; |
| 268 |
} |
| 269 |
|
| 270 |
// print |
| 271 |
<<< "# of data points:", numPoints, "dimensions:", numCoeffs >>>; |
| 272 |
|
| 273 |
// done for now |
| 274 |
return fio; |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
//------------------------------------------------------------------------------ |
| 279 |
// function: read the data |
| 280 |
//------------------------------------------------------------------------------ |
| 281 |
fun void readData( FileIO fio ) |
| 282 |
{ |
| 283 |
// rewind the file reader |
| 284 |
fio.seek( 0 ); |
| 285 |
|
| 286 |
// read file int array |
| 287 |
string str; |
| 288 |
int ci, ri; |
| 289 |
while( fio => str ) |
| 290 |
{ |
| 291 |
// check for last |
| 292 |
if( (ci != 0) && ((ci % numCoeffs) == 0) ) |
| 293 |
{ |
| 294 |
// read in label |
| 295 |
str => inLabels[ri]; |
| 296 |
// set in map |
| 297 |
1 => label2int[str]; |
| 298 |
// increment row |
| 299 |
ri++; |
| 300 |
// reset column |
| 301 |
0 => ci; |
| 302 |
} |
| 303 |
else |
| 304 |
{ |
| 305 |
// store feature value |
| 306 |
Std.atof(str) => inFeatures[ri][ci]; |
| 307 |
// increment column |
| 308 |
ci++; |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
// get keys from map |
| 313 |
label2int.getKeys( labels ); |
| 314 |
// assign index |
| 315 |
for( int i; i < labels.size(); i++ ) |
| 316 |
{ i => label2int[labels[i]]; } |
| 317 |
// convert in labels to ints |
| 318 |
for( int i; i < inLabels.size(); i++ ) |
| 319 |
{ |
| 320 |
// get index as int |
| 321 |
label2int[inLabels[i]] => inLabelsInt[i]; |
| 322 |
} |
| 323 |
} |