CEBL  2.1
test_qda.cpp
Go to the documentation of this file.
1 #include "CEBLModel.hpp"
2 #include <cppR.hpp>
3 
4 int main(int ac, char ** av)
5 {
6  CEBLModel * model = new CEBLModel;
7 
8  //add a data file preference to the command line options
9  model->preferencesAddOption("datafile",
10  "Training data file to test QDA with.");
11 
12  model->InitModel(ac,av);
13 
14  if(model->initializedSuccessfully())
15  {
16  //--------------------------------------------------
17 
18  // check the filename parameter
19  string data_filename = model->preferencesGetOption("datafile");
20  if(data_filename == "None")
21  {
22  cerr << "usage: test_qda --datafile=<training data filename>\n";
23  return 1;
24  }
25 
26  // load the data file
27  EEGTrainingData data;
28  try
29  {
30  data = model->dataLoadTrainingDataFile(data_filename);
31  }
32  catch(exception &e)
33  {
34  cerr << "Failed to load datafile: " << e.what() << "\n";
35  return 1;
36  }
37 
38  // select QDA classifier
39  model->classifiersSetSelected("QDA");
40 
41  //select lag feature
42  model->featuresSetSelected("Lag");
43  std::map<std::string, CEBL::Param> params = model->featureGetParams();
44  params[0].setInt(5);
45  model->featureSetParams(params);
46 
47  //featurize data
48  data = model->featuresExtract(data);
49 
50  // select first sequence as training and second as testing
51  EEGTrainingData train;
53  for(int i=0;i<data.numClasses();i++)
54  {
55  train.set(i,0,data.get(i,0));
56  test.set(i,0,data.get(i,1));
57  }
58 
59  // train classifier
60  model->classifierTrain(train);
61 
62  // use classifier
63  EEGData test_data = test.collapse();
64  ublas::vector<int> targets = test.getTargets();
65  ublas::vector<int> predicted = model->classifierUse(test_data);
66 
67  double correct = 0;
68  for(unsigned i=0;i<targets.size();i++)
69  {
70  if(targets[i] == predicted[i])
71  correct++;
72  }
73  cout << (correct/targets.size() * 100.0) << "% correct\n";
74 
75  //--------------------------------------------------
76 
77  }
78  delete model;
79  return 0;
80 }