public void learn_test()
{
#region doc_learn
// Generate always same random numbers
Accord.Math.Random.Generator.Seed = 0;
// The following is a simple auto association function in which
// the last column of each input correspond to its own class. This
// problem should be easily solved using a Linear kernel.
// Sample input data
double[][] inputs =
{
new double[] { 1, 2, 0 },
new double[] { 6, 2, 3 },
new double[] { 1, 1, 1 },
new double[] { 7, 6, 2 },
};
// Output for each of the inputs
int[] outputs = { 0, 3, 1, 2 };
// Create the multi-class learning algorithm for the machine
var teacher = new MulticlassSupportVectorLearning<Linear>()
{
// Configure the learning algorithm to use SMO to train the
// underlying SVMs in each of the binary class subproblems.
Learner = (param) => new SequentialMinimalOptimization<Linear>()
{
// If you would like to use other kernels, simply replace
// the generic parameter to the desired kernel class, such
// as for example, Polynomial or Gaussian:
Kernel = new Linear() // use the Linear kernel
}
};
// Estimate the multi-class support vector machine using one-vs-one method
MulticlassSupportVectorMachine<Linear> ovo = teacher.Learn(inputs, outputs);
// Obtain class predictions for each sample
int[] predicted = ovo.Decide(inputs);
// Compute classification error
double error = new ZeroOneLoss(outputs).Loss(predicted);
#endregion
Assert.AreEqual(0, error);
Assert.IsTrue(predicted.IsEqual(outputs));
Assert.IsTrue(ovo.Scores(inputs[0]).IsEqual(new double[] { 0.62, -0.25, -0.59, -0.62 }, 1e-2));
Assert.IsTrue(ovo.Scores(inputs[1]).IsEqual(new double[] { -0.62, -0.57, -0.13, 0.62 }, 1e-2));
Assert.IsTrue(ovo.Scores(inputs[2]).IsEqual(new double[] { -0.25, 0.63, -0.63, -0.51 }, 1e-2));
}