public void RunTest() { // Example regression problem. Suppose we are trying // to model the following equation: f(x, y) = 2x + y double[][] inputs = // (x, y) { new double[] { 0, 1 }, // 2*0 + 1 = 1 new double[] { 4, 3 }, // 2*4 + 3 = 11 new double[] { 8, -8 }, // 2*8 - 8 = 8 new double[] { 2, 2 }, // 2*2 + 2 = 6 new double[] { 6, 1 }, // 2*6 + 1 = 13 new double[] { 5, 4 }, // 2*5 + 4 = 14 new double[] { 9, 1 }, // 2*9 + 1 = 19 new double[] { 1, 6 }, // 2*1 + 6 = 8 }; double[] outputs = // f(x, y) { 1, 11, 8, 6, 13, 14, 19, 8 }; // Create a new linear Support Vector Machine var machine = new SupportVectorMachine(inputs: 2); // Create the linear regression coordinate descent teacher var learn = new LinearRegressionNewtonMethod(machine, inputs, outputs) { Complexity = 100000000, Epsilon = 1e-15, Tolerance = 1e-15, }; // Run the learning algorithm double error = learn.Run(); Assert.AreEqual(860.0, error); // Compute the answer for one particular example double fxy = machine.Compute(inputs[0]); // 1.0003849827673186 // Check for correct answers double[] answers = new double[inputs.Length]; for (int i = 0; i < answers.Length; i++) answers[i] = machine.Compute(inputs[i]); Assert.AreEqual(1.0, fxy, 1e-5); for (int i = 0; i < outputs.Length; i++) Assert.AreEqual(outputs[i], answers[i], 1e-5); }
public void Run() { // Example AND problem double[][] inputs = { new double[] { 0, 0 }, // 0 and 0: 0 (label -1) new double[] { 0, 1 }, // 0 and 1: 0 (label -1) new double[] { 1, 0 }, // 1 and 0: 0 (label -1) new double[] { 1, 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 SupportVectorMachine machine = new SupportVectorMachine(inputs[0].Length); // Instantiate a new learning algorithm for SVMs SequentialMinimalOptimization smo = new SequentialMinimalOptimization(machine, inputs, labels); // Set up the learning algorithm smo.Complexity = 1.0; // Run the learning algorithm double error = smo.Run(); // Compute the decision output for one of the input vectors int decision = System.Math.Sign(machine.Compute(inputs[0])); }
private static void and() { // Create a simple binary AND // classification problem: double[][] problem = { // a b a + b new double[] { 0, 0, 0 }, new double[] { 0, 1, 0 }, new double[] { 1, 0, 0 }, new double[] { 1, 1, 1 }, }; // Get the two first columns as the problem // inputs and the last column as the output // input columns double[][] inputs = problem.GetColumns(0, 1); // output column int[] outputs = problem.GetColumn(2).ToInt32(); // Plot the problem on screen ScatterplotBox.Show("AND", inputs, outputs).Hold(); // However, SVMs expect the output value to be // either -1 or +1. As such, we have to convert // it so the vector contains { -1, -1, -1, +1 }: // outputs = outputs.Apply(x => x == 0 ? -1 : 1); // Create a new linear-SVM for two inputs (a and b) SupportVectorMachine svm = new SupportVectorMachine(inputs: 2); // Create a L2-regularized L2-loss support vector classification var teacher = new LinearDualCoordinateDescent(svm, inputs, outputs) { Loss = Loss.L2, Complexity = 1000, Tolerance = 1e-5 }; // Learn the machine double error = teacher.Run(computeError: true); // Compute the machine's answers for the learned inputs int[] answers = inputs.Apply(x => Math.Sign(svm.Compute(x))); // Plot the results ScatterplotBox.Show("SVM's answer", inputs, answers).Hold(); }
public void LearnTest() { double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] xor = { -1, 1, 1, -1 }; var kernel = new Polynomial(2, 0.0); double[][] augmented = new double[inputs.Length][]; for (int i = 0; i < inputs.Length; i++) augmented[i] = kernel.Transform(inputs[i]); SupportVectorMachine machine = new SupportVectorMachine(augmented[0].Length); // Create the Least Squares Support Vector Machine teacher var learn = new LinearDualCoordinateDescent(machine, augmented, xor); // Run the learning algorithm double error = learn.Run(); Assert.AreEqual(0, error); int[] output = augmented.Apply(p => Math.Sign(machine.Compute(p))); for (int i = 0; i < output.Length; i++) Assert.AreEqual(System.Math.Sign(xor[i]), System.Math.Sign(output[i])); }
public void ComputeTest5() { var dataset = SequentialMinimalOptimizationTest.yinyang; double[][] inputs = dataset.Submatrix(null, 0, 1).ToArray(); int[] labels = dataset.GetColumn(2).ToInt32(); var kernel = new Polynomial(2, 1); Accord.Math.Tools.SetupGenerator(0); var projection = inputs.Apply(kernel.Transform); var machine = new SupportVectorMachine(projection[0].Length); var smo = new LinearCoordinateDescent(machine, projection, labels) { Complexity = 1000000, Tolerance = 1e-15 }; double error = smo.Run(); Assert.AreEqual(1000000.0, smo.Complexity, 1e-15); int[] actual = new int[labels.Length]; for (int i = 0; i < actual.Length; i++) actual[i] = Math.Sign(machine.Compute(projection[i])); ConfusionMatrix matrix = new ConfusionMatrix(actual, labels); Assert.AreEqual(6, matrix.FalseNegatives); Assert.AreEqual(7, matrix.FalsePositives); Assert.AreEqual(44, matrix.TruePositives); Assert.AreEqual(43, matrix.TrueNegatives); }
public void LearnTest5() { double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] positives = { 1, 1, 1, 1 }; // Create Kernel Support Vector Machine with a Polynomial Kernel of 2nd degree SupportVectorMachine machine = new SupportVectorMachine(inputs[0].Length); // Create the sequential minimal optimization teacher SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, positives); learn.Complexity = 1; // Run the learning algorithm double error = learn.Run(); Assert.AreEqual(0, error); int[] output = inputs.Apply(p => (int)machine.Compute(p)); for (int i = 0; i < output.Length; i++) { bool sor = positives[i] >= 0; bool sou = output[i] >= 0; Assert.AreEqual(sor, sou); } }
public void TrainTest2() { double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] or = { -1, -1, -1, +1 }; // Create Kernel Support Vector Machine with a Polynomial Kernel of 2nd degree SupportVectorMachine machine = new SupportVectorMachine(inputs[0].Length); // Create the sequential minimal optimization teacher SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, or); learn.Complexity = 1; // Run the learning algorithm learn.Run(); double[] output = machine.Compute(inputs); for (int i = 0; i < output.Length; i++) { bool sor = or[i] >= 0; bool sou = output[i] >= 0; Assert.AreEqual(sor, sou); } }
public void TransformTest() { var inputs = yinyang.Submatrix(null, 0, 1).ToArray(); var labels = yinyang.GetColumn(2).ToInt32(); ConfusionMatrix actual, expected; SequentialMinimalOptimization a, b; var kernel = new Polynomial(2, 0); { var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length); a = new SequentialMinimalOptimization(machine, inputs, labels); a.UseComplexityHeuristic = true; a.Run(); int[] values = new int[labels.Length]; for (int i = 0; i < values.Length; i++) values[i] = Math.Sign(machine.Compute(inputs[i])); expected = new ConfusionMatrix(values, labels); } { var projection = inputs.Apply(kernel.Transform); var machine = new SupportVectorMachine(projection[0].Length); b = new SequentialMinimalOptimization(machine, projection, labels); b.UseComplexityHeuristic = true; b.Run(); int[] values = new int[labels.Length]; for (int i = 0; i < values.Length; i++) values[i] = Math.Sign(machine.Compute(projection[i])); actual = new ConfusionMatrix(values, labels); } Assert.AreEqual(a.Complexity, b.Complexity, 1e-15); Assert.AreEqual(expected.TrueNegatives, actual.TrueNegatives); Assert.AreEqual(expected.TruePositives, actual.TruePositives); Assert.AreEqual(expected.FalseNegatives, actual.FalseNegatives); Assert.AreEqual(expected.FalsePositives, actual.FalsePositives); }
public void ComputeTest5() { var dataset = SequentialMinimalOptimizationTest.yinyang; var inputs = dataset.Submatrix(null, 0, 1).ToArray(); var labels = dataset.GetColumn(2).ToInt32(); var kernel = new Polynomial(2, 0); { var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length); var smo = new SequentialMinimalOptimization(machine, inputs, labels); smo.UseComplexityHeuristic = true; double error = smo.Run(); Assert.AreEqual(0.2, error); Assert.AreEqual(0.11714451552090824, smo.Complexity); int[] actual = new int[labels.Length]; for (int i = 0; i < actual.Length; i++) actual[i] = Math.Sign(machine.Compute(inputs[i])); ConfusionMatrix matrix = new ConfusionMatrix(actual, labels); Assert.AreEqual(20, matrix.FalseNegatives); Assert.AreEqual(0, matrix.FalsePositives); Assert.AreEqual(30, matrix.TruePositives); Assert.AreEqual(50, matrix.TrueNegatives); } { Accord.Math.Tools.SetupGenerator(0); var projection = inputs.Apply(kernel.Transform); var machine = new SupportVectorMachine(projection[0].Length); var smo = new LinearNewtonMethod(machine, projection, labels); smo.UseComplexityHeuristic = true; double error = smo.Run(); Assert.AreEqual(0.18, error); Assert.AreEqual(0.11714451552090821, smo.Complexity, 1e-15); int[] actual = new int[labels.Length]; for (int i = 0; i < actual.Length; i++) actual[i] = Math.Sign(machine.Compute(projection[i])); ConfusionMatrix matrix = new ConfusionMatrix(actual, labels); Assert.AreEqual(17, matrix.FalseNegatives); Assert.AreEqual(1, matrix.FalsePositives); Assert.AreEqual(33, matrix.TruePositives); Assert.AreEqual(49, matrix.TrueNegatives); } }
public void ReceiverOperatingCharacteristicConstructorTest3() { // This example shows how to measure the accuracy of a // binary classifier using a ROC curve. For this example, // we will be creating a Support Vector Machine trained // on the following instances: double[][] inputs = { // Those are from class -1 new double[] { 2, 4, 0 }, new double[] { 5, 5, 1 }, new double[] { 4, 5, 0 }, new double[] { 2, 5, 5 }, new double[] { 4, 5, 1 }, new double[] { 4, 5, 0 }, new double[] { 6, 2, 0 }, new double[] { 4, 1, 0 }, // Those are from class +1 new double[] { 1, 4, 5 }, new double[] { 7, 5, 1 }, new double[] { 2, 6, 0 }, new double[] { 7, 4, 7 }, new double[] { 4, 5, 0 }, new double[] { 6, 2, 9 }, new double[] { 4, 1, 6 }, new double[] { 7, 2, 9 }, }; int[] outputs = { -1, -1, -1, -1, -1, -1, -1, -1, // fist eight from class -1 +1, +1, +1, +1, +1, +1, +1, +1 // last eight from class +1 }; // Create a linear Support Vector Machine with 4 inputs SupportVectorMachine machine = new SupportVectorMachine(inputs: 3); // Create the sequential minimal optimization teacher SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, outputs); // Run the learning algorithm double error = learn.Run(); // Extract the input labels predicted by the machine double[] predicted = new double[inputs.Length]; for (int i = 0; i < predicted.Length; i++) predicted[i] = machine.Compute(inputs[i]); // Create a new ROC curve to assess the performance of the model var roc = new ReceiverOperatingCharacteristic(outputs, predicted); roc.Compute(100); // Compute a ROC curve with 100 points /* // Generate a connected scatter plot for the ROC curve and show it on-screen ScatterplotBox.Show(roc.GetScatterplot(includeRandom: true), nonBlocking: true) .SetSymbolSize(0) // do not display data points .SetLinesVisible(true) // show lines connecting points .SetScaleTight(true) // tighten the scale to points .WaitForClose(); */ Assert.AreEqual(0.7890625, roc.Area); // Assert.AreEqual(0.1174774, roc.StandardError, 1e-6); HanleyMcNeil Assert.AreEqual(0.11958120746409709, roc.StandardError, 1e-6); }
private static void xor() { // Create a simple binary XOR // classification problem: double[][] problem = { // a b a XOR b new double[] { 0, 0, 0 }, new double[] { 0, 1, 1 }, new double[] { 1, 0, 1 }, new double[] { 1, 1, 0 }, }; // Get the two first columns as the problem // inputs and the last column as the output // input columns double[][] inputs = problem.GetColumns(0, 1); // output column int[] outputs = problem.GetColumn(2).ToInt32(); // Plot the problem on screen ScatterplotBox.Show("XOR", inputs, outputs).Hold(); // However, SVMs expect the output value to be // either -1 or +1. As such, we have to convert // it so the vector contains { -1, -1, -1, +1 }: // outputs = outputs.Apply(x => x == 0 ? -1 : 1); // Create a new linear-SVM for two inputs (a and b) SupportVectorMachine svm = new SupportVectorMachine(inputs: 2); // Create a L2-regularized L2-loss support vector classification var teacher = new LinearDualCoordinateDescent(svm, inputs, outputs) { Loss = Loss.L2, Complexity = 1000, Tolerance = 1e-5 }; // Learn the machine double error = teacher.Run(computeError: true); // Compute the machine's answers for the learned inputs int[] answers = inputs.Apply(x => Math.Sign(svm.Compute(x))); // Plot the results ScatterplotBox.Show("SVM's answer", inputs, answers).Hold(); // Use an explicit kernel expansion to transform the // non-linear classification problem into a linear one // // Create a quadratic kernel Quadratic quadratic = new Quadratic(constant: 1); // Project the inptus into a higher dimensionality space double[][] expansion = inputs.Apply(quadratic.Transform); // Create a new linear-SVM for the transformed input space svm = new SupportVectorMachine(inputs: expansion[0].Length); // Create the same learning algorithm in the expanded input space teacher = new LinearDualCoordinateDescent(svm, expansion, outputs) { Loss = Loss.L2, Complexity = 1000, Tolerance = 1e-5 }; // Learn the machine error = teacher.Run(computeError: true); // Compute the machine's answers for the learned inputs answers = expansion.Apply(x => Math.Sign(svm.Compute(x))); // Plot the results ScatterplotBox.Show("SVM's answer", inputs, answers).Hold(); }
private static void cancer() { // Create a new LibSVM sparse format data reader // to read the Wisconsin's Breast Cancer dataset // var reader = new SparseReader("examples-sparse.txt"); int[] outputs; // Read the classification problem into dense memory double[][] inputs = reader.ReadToEnd(sparse: false, labels: out outputs); // The dataset has output labels as 4 and 2. We have to convert them // into negative and positive labels so they can be properly processed. // outputs = outputs.Apply(x => x == 2 ? -1 : +1); // Create a new linear-SVM for the problem dimensions var svm = new SupportVectorMachine(inputs: reader.Dimensions); // Create a learning algorithm for the problem's dimensions var teacher = new LinearDualCoordinateDescent(svm, inputs, outputs) { Loss = Loss.L2, Complexity = 1000, Tolerance = 1e-5 }; // Learn the classification double error = teacher.Run(); // Compute the machine's answers for the learned inputs int[] answers = inputs.Apply(x => Math.Sign(svm.Compute(x))); // Create a confusion matrix to show the machine's performance var m = new ConfusionMatrix(predicted: answers, expected: outputs); // Show it onscreen DataGridBox.Show(new ConfusionMatrixView(m)); }