public void weight_test_homogeneous_linear_kernel() { var dataset = yinyang; double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged(); int[] labels = dataset.GetColumn(2).ToInt32(); Accord.Math.Tools.SetupGenerator(0); var kernel = new Linear(); Assert.AreEqual(kernel.Constant, 0); { var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length); var smo = new SequentialMinimalOptimization(machine, inputs, labels); smo.Complexity = 1.0; smo.PositiveWeight = 1; smo.NegativeWeight = 1; smo.Tolerance = 0.001; double error = smo.Run(); int[] actual = new int[labels.Length]; for (int i = 0; i < actual.Length; i++) { actual[i] = machine.Decide(inputs[i]) ? 1 : 0; } ConfusionMatrix matrix = new ConfusionMatrix(actual, labels); Assert.AreEqual(43, matrix.TruePositives); // both classes are Assert.AreEqual(43, matrix.TrueNegatives); // well equilibrated Assert.AreEqual(7, matrix.FalseNegatives); Assert.AreEqual(7, matrix.FalsePositives); Assert.AreEqual(1.0, smo.Complexity); Assert.AreEqual(1.0, smo.WeightRatio); Assert.AreEqual(1.0, smo.NegativeWeight); Assert.AreEqual(1.0, smo.PositiveWeight); Assert.AreEqual(0.14, error); Assert.AreEqual(0.001, smo.Tolerance); Assert.AreEqual(31, machine.SupportVectors.Length); machine.Compress(); Assert.AreEqual(1, machine.Weights[0]); Assert.AreEqual(1, machine.SupportVectors.Length); Assert.AreEqual(-1.3107402300323954, machine.SupportVectors[0][0]); Assert.AreEqual(-0.5779471529948812, machine.SupportVectors[0][1]); Assert.AreEqual(-0.53366022455811646, machine.Threshold); for (int i = 0; i < actual.Length; i++) { int expected = actual[i]; int y = machine.Decide(inputs[i]) ? 1 : 0; Assert.AreEqual(expected, y); } } { var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length); var smo = new SequentialMinimalOptimization(machine, inputs, labels); smo.Complexity = 1; smo.PositiveWeight = 100; smo.NegativeWeight = 1; smo.Tolerance = 0.001; double error = smo.Run(); int[] actual = new int[labels.Length]; for (int i = 0; i < actual.Length; i++) { actual[i] = machine.Decide(inputs[i]) ? 1 : 0; } ConfusionMatrix matrix = new ConfusionMatrix(actual, labels); Assert.AreEqual(50, matrix.TruePositives); // has more importance Assert.AreEqual(23, matrix.TrueNegatives); Assert.AreEqual(0, matrix.FalseNegatives); // has more importance Assert.AreEqual(27, matrix.FalsePositives); Assert.AreEqual(1.0, smo.Complexity); Assert.AreEqual(100, smo.WeightRatio); Assert.AreEqual(1.0, smo.NegativeWeight); Assert.AreEqual(100, smo.PositiveWeight); Assert.AreEqual(0.001, smo.Tolerance); Assert.AreEqual(0.27, error); Assert.AreEqual(42, machine.SupportVectors.Length); } { var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length); var smo = new SequentialMinimalOptimization(machine, inputs, labels); smo.Complexity = 1; smo.PositiveWeight = 1; smo.NegativeWeight = 100; smo.Tolerance = 0.001; double error = smo.Run(); int[] actual = new int[labels.Length]; for (int i = 0; i < actual.Length; i++) { actual[i] = machine.Decide(inputs[i]) ? 1 : 0; } var matrix = new ConfusionMatrix(actual, labels); Assert.AreEqual(25, matrix.TruePositives); Assert.AreEqual(50, matrix.TrueNegatives); // has more importance Assert.AreEqual(25, matrix.FalseNegatives); Assert.AreEqual(0, matrix.FalsePositives); // has more importance Assert.AreEqual(1.0, smo.Complexity); Assert.AreEqual(0.01, smo.WeightRatio); Assert.AreEqual(100, smo.NegativeWeight); Assert.AreEqual(1.0, smo.PositiveWeight); Assert.AreEqual(0.25, error); Assert.AreEqual(0.001, smo.Tolerance); Assert.AreEqual(40, machine.SupportVectors.Length); } }