public void Test_SVM_XOR_Classification() { var xor = new[] { new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, }; var d = Descriptor.New("XOR") .With("a").As(typeof(bool)) .With("b").As(typeof(bool)) .Learn("c").As(typeof(bool)); var generator = new numl.Supervised.SVM.SVMGenerator { Descriptor = d, C = 0.1, KernelFunction = new Math.Kernels.PolyKernel(3) }; var model = Learner.Learn(xor, 1.0, 10, generator).Model; Matrix x = new[, ] { { -1, -1 }, // false, false -> - { -1, 1 }, // false, true -> + { 1, -1 }, // true, false -> + { 1, 1 } }; // true, true -> - Vector actual = new int[] { -1, 1, 1, -1 }; Vector y = new[] { 0, 0, 0, 0 }; for (int i = 0; i < x.Rows; i++) { y[i] = model.Predict(x[i, VectorType.Row]); } var score = numl.Supervised.Score.ScorePredictions(y, actual); Console.WriteLine($"SVM Model\n: { score }"); }
public void Test_SVM_XOR_Classification() { var xor = new[] { new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, new { a = false, b = false, c = false }, new { a = false, b = true, c = true }, new { a = true, b = false, c = true }, new { a = true, b = true, c = false }, }; var d = Descriptor.New("XOR") .With("a").As(typeof(bool)) .With("b").As(typeof(bool)) .Learn("c").As(typeof(bool)); var generator = new numl.Supervised.SVM.SVMGenerator { Descriptor = d, C = 0.1, KernelFunction = new Math.Kernels.PolyKernel(3) }; var model = Learner.Learn(xor, 1.0, 10, generator).Model; Matrix x = new[,] {{ -1, -1 }, // false, false -> - { -1, 1 }, // false, true -> + { 1, -1 }, // true, false -> + { 1, 1 }}; // true, true -> - Vector actual = new int[] { -1, 1, 1, -1 }; Vector y = new[] { 0, 0, 0, 0 }; for (int i = 0; i < x.Rows; i++) y[i] = model.Predict(x[i, VectorType.Row]); var score = numl.Supervised.Score.ScorePredictions(y, actual); Console.WriteLine($"SVM Model\n: { score }"); }