public void SparseComputeTest()
{
// Example AND problem
double[][] inputs =
{
new double[] { }, // 0 and 0: 0 (label -1)
new double[] { 2,1 }, // 0 and 1: 0 (label -1)
new double[] { 1,1 }, // 1 and 0: 0 (label -1)
new double[] { 1,1, 2,1 } // 1 and 1: 1 (label +1)
};
// Dichotomy SVM outputs should be given as [-1;+1]
int[] labels =
{
// 0, 0, 0, 1
-1, -1, -1, 1
};
// Create a Support Vector Machine for the given inputs
// (sparse machines should use 0 as the number of inputs)
var machine = new KernelSupportVectorMachine(new SparseLinear(), inputs: 0);
// Instantiate a new learning algorithm for SVMs
var smo = new SequentialMinimalOptimization(machine, inputs, labels);
// Set up the learning algorithm
smo.Complexity = 100000.0;
// Run
double error = smo.Run(); // should be zero
double[] predicted = inputs.Apply(machine.Compute).Sign();
// Outputs should be -1, -1, -1, +1
Assert.AreEqual(-1, predicted[0]);
Assert.AreEqual(-1, predicted[1]);
Assert.AreEqual(-1, predicted[2]);
Assert.AreEqual(+1, predicted[3]);
Assert.AreEqual(error, 0);
// Same as ComputeTest3 test above
Assert.AreEqual(-3.0, machine.Threshold);
Assert.AreEqual(4, machine.Weights[0]);
Assert.AreEqual(-2, machine.Weights[1]);
Assert.AreEqual(-2, machine.Weights[2]);
}