Shark machine learning library
About Shark
News!
Contribute
Credits and copyright
Downloads
Getting Started
Installation
Using the docs
Documentation
Tutorials
Quick references
Class list
Global functions
FAQ
Showroom
examples
Supervised
VersatileClassificationTutorial-SVM.cpp
Go to the documentation of this file.
1
2
3
#include <
shark/Data/Dataset.h
>
4
#include <
shark/Data/Csv.h
>
5
#include <
shark/ObjectiveFunctions/Loss/ZeroOneLoss.h
>
6
7
#include <
shark/Models/Kernels/GaussianRbfKernel.h
>
8
#include <
shark/Models/Kernels/KernelExpansion.h
>
9
#include <
shark/Algorithms/Trainers/CSvmTrainer.h
>
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);
28
CSvmTrainer<RealVector>
trainer(
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
37
ZeroOneLoss<unsigned int>
loss;
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
}