public void learn_linear()
{
#region doc_xor_linear
// As an example, we will try to learn a linear machine that can
// replicate the "exclusive-or" logical function. However, since we
// will be using a linear SVM, we will not be able to solve this
// problem perfectly as the XOR is a non-linear classification problem:
double[][] inputs =
{
new double[] { 0, 0 }, // the XOR function takes two booleans
new double[] { 0, 1 }, // and computes their exclusive or: the
new double[] { 1, 0 }, // output is true only if the two booleans
new double[] { 1, 1 } // are different
};
int[] xor = // this is the output of the xor function
{
0, // 0 xor 0 = 0 (inputs are equal)
1, // 0 xor 1 = 1 (inputs are different)
1, // 1 xor 0 = 1 (inputs are different)
0, // 1 xor 1 = 0 (inputs are equal)
};
// Now, we can create the sequential minimal optimization teacher
var learn = new SequentialMinimalOptimization()
{
UseComplexityHeuristic = true,
UseKernelEstimation = false
};
// And then we can obtain a trained SVM by calling its Learn method
SupportVectorMachine svm = learn.Learn(inputs, xor);
// Finally, we can obtain the decisions predicted by the machine:
bool[] prediction = svm.Decide(inputs);
#endregion
Assert.AreEqual(prediction[0], false);
Assert.AreEqual(prediction[1], false);
Assert.AreEqual(prediction[2], false);
Assert.AreEqual(prediction[3], false);
int[] or = // this is the output of the xor function
{
0, // 0 or 0 = 0 (inputs are equal)
1, // 0 or 1 = 1 (inputs are different)
1, // 1 or 0 = 1 (inputs are different)
1, // 1 or 1 = 1 (inputs are equal)
};
learn = new SequentialMinimalOptimization()
{
Complexity = 1e+8,
UseKernelEstimation = false
};
svm = learn.Learn(inputs, or);
prediction = svm.Decide(inputs);
Assert.AreEqual(prediction[0], false);
Assert.AreEqual(prediction[1], true);
Assert.AreEqual(prediction[2], true);
Assert.AreEqual(prediction[3], true);
}