/// <summary> /// Initializes a new instance of a Sequential Minimal Optimization (SMO) algorithm. /// </summary> /// /// <param name="machine">A Support Vector Machine.</param> /// <param name="inputs">The input data points as row vectors.</param> /// <param name="outputs">The classification label for each data point in the range [-1;+1].</param> /// public SequentialMinimalOptimization(SupportVectorMachine machine, double[][] inputs, int[] outputs) { // Initial argument checking if (machine == null) throw new ArgumentNullException("machine"); if (inputs == null) throw new ArgumentNullException("inputs"); if (outputs == null) throw new ArgumentNullException("outputs"); if (inputs.Length != outputs.Length) throw new ArgumentException("The number of inputs and outputs does not match.", "outputs"); for (int i = 0; i < outputs.Length; i++) { if (outputs[i] != 1 && outputs[i] != -1) throw new ArgumentOutOfRangeException("outputs", "One of the labels in the output vector is neither +1 or -1."); } if (machine.Inputs > 0) { // This machine has a fixed input vector size for (int i = 0; i < inputs.Length; i++) if (inputs[i].Length != machine.Inputs) throw new ArgumentException( "The size of the input vectors does not match the expected number of inputs of the machine"); } // Machine this.machine = machine; // Kernel (if applicable) KernelSupportVectorMachine ksvm = machine as KernelSupportVectorMachine; this.kernel = (ksvm != null) ? ksvm.Kernel : new Linear(); // Learning data this.inputs = inputs; this.outputs = outputs; }
public void CreateKernelplot(ZedGraphControl zgc, double[,] graph, SupportVectorMachine svm) { GraphPane myPane = zgc.GraphPane; myPane.CurveList.Clear(); // Set the titles myPane.Title.IsVisible = false; myPane.XAxis.Title.Text = _sourceColumns[0]; myPane.YAxis.Title.Text = _sourceColumns[1]; // Classification problem PointPairList list1 = new PointPairList(); // Z = -1 PointPairList list2 = new PointPairList(); // Z = +1 for (int i = 0; i < graph.GetLength(0); i++) { if (graph[i, 2] == -1) list1.Add(graph[i, 0], graph[i, 1]); if (graph[i, 2] == 1) list2.Add(graph[i, 0], graph[i, 1]); } var list3 = new PointPairList(); var list_up = new PointPairList(); var list_down = new PointPairList(); double x, y = 0.0, y_down = 0.0, y_up = 0.0; for (var i = -2; i < 12; i++) { var a = -svm.Weights[0] / svm.Weights[1]; y = a * i - svm.Threshold / svm.Weights[1]; var b = svm.SupportVectorsTwoSides[0]; y_up = a * i + (b[1] - a * b[0]); list3.Add(i, y); list_up.Add(i, y_up); b = svm.SupportVectorsTwoSides[1]; y_down = a * i + (b[1] - a * b[0]); list_down.Add(i, y_down); } // Add the curve LineItem myCurve = myPane.AddCurve("G1", list1, Color.Blue, SymbolType.Diamond); myCurve.Line.IsVisible = false; myCurve.Symbol.Border.IsVisible = false; myCurve.Symbol.Fill = new Fill(Color.Blue); myCurve = myPane.AddCurve("G2", list2, Color.Green, SymbolType.Diamond); myCurve.Line.IsVisible = false; myCurve.Symbol.Border.IsVisible = false; myCurve.Symbol.Fill = new Fill(Color.Green); myCurve = myPane.AddCurve("L", list3, Color.Black, SymbolType.Circle); myCurve.Line.IsVisible = true; myCurve = myPane.AddCurve("L", list_up, Color.Black, SymbolType.Circle); myCurve.Line.IsVisible = true; myCurve = myPane.AddCurve("L", list_down, Color.Black, SymbolType.Circle); myCurve.Line.IsVisible = true; // Fill the background of the chart rect and pane //myPane.Chart.Fill = new Fill(Color.White, Color.LightGoldenrodYellow, 45.0f); //myPane.Fill = new Fill(Color.White, Color.SlateGray, 45.0f); myPane.Fill = new Fill(Color.WhiteSmoke); zgc.AxisChange(); zgc.Invalidate(); }
private void btnSimpleSMO_Click(object sender, EventArgs e) { double[,] sourceMatrix; double[,] inputs; int[] labels; GetData(out sourceMatrix, out inputs, out labels); //_svm.SimpleSMO(inputs, labels); // Perform classification SequentialMinimalOptimization smo; // Creates the Support Vector Machine using the selected kernel //svm = new KernelSupportVectorMachine(kernel, 2); SupportVectorMachine svm = new SupportVectorMachine(2); // Creates a new instance of the SMO Learning Algorithm smo = new SequentialMinimalOptimization(svm, inputs.ToArray(), labels); // Set learning parameters smo.Complexity =1.0; smo.Tolerance = 0.001; bool converged = true; try { // Run double error = smo.Run(); } catch { converged = false; } var a = sourceMatrix.Submatrix(0, sourceMatrix.GetLength(0) - 1, 0, 2); CreateScatterplot(graphInput, a, svm); }