VersatileClassificationTutorial-SVM.cpp
Go to the documentation of this file.
1 
2 
3 #include <shark/Data/Dataset.h>
4 #include <shark/Data/Csv.h>
6 
10 
11 
12 using namespace shark;
13 
14 int main()
15 {
16  // Load data, use 70% for training and 30% for testing.
17  // The path is hard coded; make sure to invoke the executable
18  // from a place where the data file can be found. It is located
19  // under [shark]/examples/Supervised/data.
20  ClassificationDataset traindata, testdata;
21  importCSV(traindata, "data/quickstartData.csv", LAST_COLUMN, ' ');
22  testdata = splitAtElement(traindata, 70 * traindata.numberOfElements() / 100);
23 
24  double gamma = 1.0; // kernel bandwidth parameter
25  double C = 10.0; // regularization parameter
26  GaussianRbfKernel<RealVector> kernel(gamma);
27  KernelClassifier<RealVector> model(&kernel);
29  &kernel,
30  C,
31  true); /* true: train model with offset */
32 
33  trainer.train(model, traindata);
34 
35  Data<unsigned int> prediction = model(testdata.inputs());
36 
38  double error_rate = loss(testdata.labels(), prediction);
39 
40  std::cout << "model: " << model.name() << std::endl
41  << "trainer: " << trainer.name() << std::endl
42  << "test error rate: " << error_rate << std::endl;
43 }