SVM classification and regression
This tutorial shows how to train and evaluate the performance of SVM models for for two and one class classification and regression problems using LIBSVM datasets.
Initialization
First, we need to load Semagle framework assemblies for manipulation of vector data and SVM training/prediction.
1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: |
|
Reading LIBSVM Data
Semagle.Data.Formats
provides function LibSVM.read
that returns the lazy sequence of (y, x)
pairs, but Semagle.MachineLearning.SVM
requires separate arrays of labels and samples for training. Function readData
converts the sequence to array and splits
the array of pairs.
1: 2: 3: 4: 5: |
|
Training
There are three different functions SMO.C_SVC
, SMO.OneClass
and SMO.C_SVR
that build two class classification, one class classification and regression SVM models. The functions take
the samples array train_x
, the labels array train_y
(except for OneClass
), the kernel function (Kernel.rbf 0.1f)
and
parameters specific to the particular optimization problem.
Two Class
Two class classification problem requires separate penalties for positive C_p
and negative C_n
samples:
1: 2: |
|
One Class
One class classification problem requires the fraction of support vectors nu
:
1: 2: |
|
Regression
Regression problem requires the boundary eta
and the penalty C
:
1: 2: 3: |
|
Predicting
There are three different prediction functions TwoClass.predict
,
OneClass.predict
and
Regression.predict
. The functions take the model model
and the sample x
and return the label y
. The prediction function can be curried
-
Two Class
1:
let predict = TwoClass.predict svm
-
One Class
1:
let predict = OneClass.predict svm
-
Regression
1:
let predict = Regression.predict svm
and applied to the test samples vector
1:
|
|
Evaluating
There are two widely used metrics for the evaluation of the performace:
-
Accuracy (two and one class classification)
1:
let accuracy = Classification.accuracy test_y predict_y
-
Mean Squared Error (regression)
1:
let mse = Regression.mse test_y predict_y
from Microsoft.FSharp.Core
from Microsoft.FSharp.Collections
member Clone : unit -> obj
member CopyTo : array:Array * index:int -> unit + 1 overload
member GetEnumerator : unit -> IEnumerator
member GetLength : dimension:int -> int
member GetLongLength : dimension:int -> int64
member GetLowerBound : dimension:int -> int
member GetUpperBound : dimension:int -> int
member GetValue : [<ParamArray>] indices:int[] -> obj + 7 overloads
member Initialize : unit -> unit
member IsFixedSize : bool
...