public static void SVM(Book newBook, List <Book> oldBooks, Author author) { double[][] inputs = oldBooks.Select(book => new double[] { book.AverageSentenceWordCount, book.PunctoationToWordRatio, book.NounToWordRatio, book.VerbToWordRatio, book.AdjectiveToWordRatio, book.AdverbToWordRatio }).ToArray(); int[] outputs = oldBooks.Select(book => (book.Author.Gender.GenderID)).ToArray(); var learn = new SequentialMinimalOptimization <Gaussian>() { UseComplexityHeuristic = true, UseKernelEstimation = true }; SupportVectorMachine <Gaussian> svm = learn.Learn(inputs, outputs); double[] newBookInput = new double[] { newBook.AverageSentenceWordCount, newBook.PunctoationToWordRatio, newBook.NounToWordRatio, newBook.VerbToWordRatio, newBook.AdjectiveToWordRatio, newBook.AdverbToWordRatio }; int newGender = (int)svm.Score(newBookInput); author.GenderID = newGender; }
public void SVMTestData() { List <DataSet> dataset = new List <DataSet>(); FillOnes(dataset); FillZeros(dataset); KernelSupportVectorMachine machine = new KernelSupportVectorMachine( new Polynomial(2), 25); var learn = new SequentialMinimalOptimization(machine, dataset.ToArray()); double[] error = learn.Run(); double[] output = machine.Compute(dataset.ToArray()); double[] expected = new double[dataset.Count]; for (int i = 0; i < dataset.Count; i++) { output[i] = Math.Round(output[i]); expected[i] = dataset[i].Expected; } CollectionAssert.AreEqual(expected, output); }
/// <summary> /// Classify our data using support vector machine classifer and save the model. /// </summary> /// <param name="train_data">Frame objects that we will use to train classifers.</param> /// <param name="test_data">Frame objects that we will use to test classifers.</param> /// <param name="train_label">Labels of the train data.</param> /// <param name="test_label">Labels of the test data.</param> /// <param name="Classifier_Path">Path where we want to save the classifer on the disk.</param> /// <param name="Classifier_Name">Name of the classifer we wnat to save.</param> /// <returns></returns> public void SVM(double[][] train_data, double[][] test_data, int[] train_label, int[] test_label, String Classifier_Path, String Classifier_Name) { var learn = new SequentialMinimalOptimization <Gaussian>() { UseComplexityHeuristic = true, UseKernelEstimation = true }; try { SupportVectorMachine <Gaussian> svm = learn.Learn(train_data, train_label); bool[] prediction = svm.Decide(test_data); var cm = GeneralConfusionMatrix.Estimate(svm, test_data, test_label); double error = cm.Error; Console.WriteLine(error); svm.Save(Path.Combine(Classifier_Path, Classifier_Name)); } catch (Exception e) { Console.WriteLine(e.StackTrace); } }
private static void XorLearn() { double[][] inputs = { new double[] { 0, 0 }, new double[] { 1, 0 }, new double[] { 0, 1 }, new double[] { 1, 1 } }; int[] outputs = { 0, 1, 1, 0 }; var smo = new SequentialMinimalOptimization <Gaussian>() { Complexity = 100 }; var svm = smo.Learn(inputs, outputs); bool[] prediction = svm.Decide(inputs); double error = new AccuracyLoss(outputs).Loss(prediction); Console.WriteLine("Error: " + error); ScatterplotBox.Show("Training data", inputs, outputs); ScatterplotBox.Show("SVM results", inputs, prediction.ToZeroOne()); }
public void learn() { #region doc_learn // Let's try to obtain a classifier for an // example 2D binary classification dataset: var iris = new DataSets.YinYang(); double[][] inputs = iris.Instances; bool[] outputs = iris.ClassLabels; // Create a learning algorithm with the Spline kernel var smo = new SequentialMinimalOptimization <Spline>() { // Force a complexity value C or let it be // determined automatically by a heuristic: // Complexity = 1.5 }; // Use it to learn a new s.v. machine var svm = smo.Learn(inputs, outputs); // Now we can compute predicted values bool[] predicted = svm.Decide(inputs); // And check how far we are from the expected values double error = new ZeroOneLoss(outputs).Loss(predicted); // error will be 0.20 #endregion Assert.AreEqual(0.2, error, 1e-6); }
private static void kernelSvm(double[][] inputs, int[] outputs) { // Create a new Sequential Minimal Optimization (SMO) learning // algorithm and estimate the complexity parameter C from data var teacher = new SequentialMinimalOptimization <Gaussian>() { UseComplexityHeuristic = true, UseKernelEstimation = true // estimate the kernel from the data }; // Teach the vector machine var svm = teacher.Learn(inputs, outputs); // Classify the samples using the model bool[] answers = svm.Decide(inputs); // Convert to Int32 so we can plot: int[] zeroOneAnswers = answers.ToZeroOne(); // Plot the results ScatterplotBox.Show("Expected results", inputs, outputs); ScatterplotBox.Show("GaussianSVM results", inputs, zeroOneAnswers); // Grab the index of multipliers higher than 0 int[] idx = teacher.Lagrange.Find(x => x > 0); // Select the input vectors for those double[][] sv = inputs.Get(idx); // Plot the support vectors selected by the machine ScatterplotBox.Show("Support vectors", sv).Hold(); }
public void ComplexityHeuristicTest() { var dataset = yinyang; double[][] inputs = dataset.Submatrix(null, 0, 1).ToJagged(); int[] labels = dataset.GetColumn(2).ToInt32(); var linear = new SupportVectorMachine(inputs[0].Length); Linear kernel = new Linear(0); var machine = new KernelSupportVectorMachine(kernel, inputs[0].Length); var smo1 = new SequentialMinimalOptimization(machine, inputs, labels); smo1.UseClassProportions = true; smo1.UseComplexityHeuristic = true; double e1 = smo1.Run(); var smo2 = new SequentialMinimalOptimization(linear, inputs, labels); smo2.UseClassProportions = true; smo2.UseComplexityHeuristic = true; double e2 = smo2.Run(); Assert.AreEqual(smo1.Complexity, smo2.Complexity); Assert.AreEqual(e1, e2); }
private static void kernelSvm(double[][] inputs, int[] outputs) { // Estimate the kernel from the data var gaussian = Gaussian.Estimate(inputs); // Create a Gaussian binary support machine with 2 inputs var svm = new KernelSupportVectorMachine(gaussian, inputs: 2); // Create a new Sequential Minimal Optimization (SMO) learning // algorithm and estimate the complexity parameter C from data var teacher = new SequentialMinimalOptimization(svm, inputs, outputs) { UseComplexityHeuristic = true }; // Teach the vector machine double error = teacher.Run(); // Classify the samples using the model int[] answers = inputs.Apply(svm.Compute).Apply(System.Math.Sign); // Plot the results ScatterplotBox.Show("Expected results", inputs, outputs); ScatterplotBox.Show("GaussianSVM results", inputs, answers); // Grab the index of multipliers higher than 0 int[] idx = teacher.Lagrange.Find(x => x > 0); // Select the input vectors for those double[][] sv = inputs.Submatrix(idx); // Plot the support vectors selected by the machine ScatterplotBox.Show("Support vectors", sv).Hold(); }
public double v3_0_1() { var ksvm = new KernelSupportVectorMachine(new Polynomial(2), 2); var smo = new SequentialMinimalOptimization(ksvm, inputs, outputs); return(smo.Run(computeError: false)); }
public void SequentialMinimalOptimizationConstructorTest() { double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] or = { 0, 0, 0, +1 }; // Create Kernel Support Vector Machine with a Polynomial Kernel of 2nd degree SupportVectorMachine machine = new SupportVectorMachine(inputs[0].Length); var learn = new SequentialMinimalOptimization(machine, inputs, or); learn.Run(); for (int i = 0; i < inputs.Length; i++) { bool actual = machine.Decide(inputs[i]); Assert.AreEqual(or[i] > 0, actual); } }
public void SequentialMinimalOptimizationConstructorTest() { double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] or = { 0, 0, 0, +1 }; // Create Kernel Support Vector Machine with a Polynomial Kernel of 2nd degree SupportVectorMachine machine = new SupportVectorMachine(inputs[0].Length); bool thrown = false; try { SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, or); } catch (ArgumentOutOfRangeException) { thrown = true; } Assert.IsTrue(thrown); }
public void ComputeTest2() { // XOR double[][] inputs = { new double[] { 0, 0 }, new double[] { 0, 1 }, new double[] { 1, 0 }, new double[] { 1, 1 } }; int[] labels = { -1, 1, 1, -1 }; KernelSupportVectorMachine machine = new KernelSupportVectorMachine(new Gaussian(0.1), inputs[0].Length); SequentialMinimalOptimization smo = new SequentialMinimalOptimization(machine, inputs, labels); smo.Complexity = 1; double error = smo.Run(); Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[0]))); Assert.AreEqual(+1, Math.Sign(machine.Compute(inputs[1]))); Assert.AreEqual(+1, Math.Sign(machine.Compute(inputs[2]))); Assert.AreEqual(-1, Math.Sign(machine.Compute(inputs[3]))); Assert.AreEqual(error, 0); }
public void Learn_UnspecifiedCacheSize_CacheSizeEqualsInputLength() { double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] xor = { -1, 1, 1, -1 }; KernelSupportVectorMachine svm = new KernelSupportVectorMachine(new Polynomial(2), inputs[0].Length); SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, xor); smo.Run(); Assert.AreEqual(smo.CacheSize, inputs.Length); }
public int[] ApplySVMByGussianKernel(double kernelDegree, double complexity, double epsilon, double tolerance, double[][] inputs, int[] outputs, int inputNumber, double[][] tests) { MulticlassSupportVectorMachine ksvm; int[] SVMoutputs = new Int32[tests.Length]; IKernel kernel; kernel = new Gaussian(kernelDegree); ksvm = new MulticlassSupportVectorMachine(inputNumber, kernel, 2); MulticlassSupportVectorLearning ml = new MulticlassSupportVectorLearning(ksvm, inputs, outputs); ml.Algorithm = (svm, classInputs, classOutputs, i, j) => { var smo = new SequentialMinimalOptimization(svm, classInputs, classOutputs); smo.Complexity = complexity; /// Cost parameter for SVM smo.Epsilon = epsilon; smo.Tolerance = tolerance; return(smo); }; double error = ml.Run(); for (int i = 0; i < tests.Length; i++) { SVMoutputs[i] = (int)ksvm.Compute(tests[i]); } return(SVMoutputs); }
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 }; // Create Kernel Support Vector Machine with a Polynomial Kernel of 2nd degree KernelSupportVectorMachine machine = new KernelSupportVectorMachine(new Polynomial(2), inputs[0].Length); // Create the sequential minimal optimization teacher SequentialMinimalOptimization learn = new SequentialMinimalOptimization(machine, inputs, xor); // Run the learning algorithm learn.Run(); int[] output = inputs.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])); } }
private List <CarViewModel> CalcMostRecommendedCars(List <CarViewModel> models) { List <Model> modelsList = dbContext.Model.ToList(); double[][] inputs = { new double[] { 250, 2017 }, new double[] { 300, 2015 }, new double[] { 200, 2016 }, new double[] { 100, 2016 }, }; int[] outputs = { 1, 0, 0, 1, }; var smo = new SequentialMinimalOptimization <Gaussian>() { Complexity = 100 }; var svm = smo.Learn(inputs, outputs); foreach (CarViewModel item in models) { double[] data = { item.PricePerDay, item.Year }; item.isRecommendedCar = svm.Decide(data); } return(models); }
public void Run() { var smo = new SequentialMinimalOptimization <Accord.Statistics.Kernels.Gaussian>() { Complexity = 100 }; var input = GetAnimal <Cat>().Concat(GetAnimal <Dog>()).ToArray(); var input_test = GetAnimal <Cat>(100).Concat(GetAnimal <Dog>(100)).ToArray(); var output = GetAnimalLabel <Cat>().Concat(GetAnimalLabel <Dog>()).ToArray(); var output_test = GetAnimalLabel <Cat>(100).Concat(GetAnimalLabel <Dog>(100)).ToArray(); Console.WriteLine($"Learn model on {input.Count()} inputs..."); var model = smo.Learn(input, output); Console.WriteLine($"Make a prediction for {input_test.Count()} inputs..."); var prediction = model.Decide(input_test); double accuracy = 1 - new AccuracyLoss(output_test).Loss(prediction); // Check if input_test equals output_test var testResults = prediction.ToZeroOne(); Console.WriteLine($"Accuracy in percentage: { accuracy * 100 }"); Console.WriteLine("Press any key to exit..."); Console.ReadKey(); }
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])); }
public void learn_precomputed() { #region doc_precomputed // As an example, we will try to learn a decision machine // that can replicate the "exclusive-or" logical function: 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) }; // Let's use a Gaussian kernel var kernel = new Gaussian(0.1); // Create a pre-computed Gaussian kernel matrix var precomputed = new Precomputed(kernel.ToJagged(inputs)); // Now, we can create the sequential minimal optimization teacher var learn = new SequentialMinimalOptimization <Precomputed, int>() { Kernel = precomputed // set the precomputed kernel we created }; // And then we can obtain the SVM by using Learn var svm = learn.Learn(precomputed.Indices, xor); // Finally, we can obtain the decisions predicted by the machine: bool[] prediction = svm.Decide(precomputed.Indices); // We can also compute the machine prediction to new samples double[][] sample = { new double[] { 0, 1 } }; // Update the precomputed kernel with the new samples precomputed = new Precomputed(kernel.ToJagged2(inputs, sample)); // Update the SVM kernel svm.Kernel = precomputed; // Compute the predictions to the new samples bool[] newPrediction = svm.Decide(precomputed.Indices); #endregion Assert.AreEqual(prediction, Classes.Decide(xor)); Assert.AreEqual(newPrediction.Length, 1); Assert.AreEqual(newPrediction[0], true); }
private static void SupportVectorMachineTraining(IEnumerable <MatchingPair> trainingData, IEnumerable <MatchingPair> testData, IDictionary <string, IndexableAttributeMetadata> actualMetadata) { var stopWatch = new Stopwatch(); stopWatch.Start(); var trainingInputs = trainingData.Select(data => data.ToVectorArray(actualMetadata)).ToArray(); var trainingOutputs = trainingData.Select(data => data.PercentMatch > 0).ToArray(); var testInputs = testData.Select(data => data.ToVectorArray(actualMetadata)).ToArray(); var testOutputs = testData.Select(data => data.PercentMatch > 0).ToArray(); var learn = new SequentialMinimalOptimization <Gaussian>() { UseComplexityHeuristic = true, UseKernelEstimation = true }; SupportVectorMachine <Gaussian> svm = learn.Learn(trainingInputs, trainingOutputs); var inSampleScore = svm.Score(trainingInputs); var outOfSampleScore = svm.Score(testInputs); Logger.InfoFormat("Result:\nIn-sample: {0}\nOut-of-sample:{1}", string.Join(", ", inSampleScore), string.Join(", ", outOfSampleScore)); var results = svm.Decide(trainingInputs); var inSampleErrors = trainingOutputs.Where((t, i) => results[i] != t).Count(); results = svm.Decide(testInputs); var outOfSampleErrors = testOutputs.Where((t, i) => results[i] != t).Count(); Logger.InfoFormat("Errors: In-sample: {0} Out-of-sample: {1}", inSampleErrors, outOfSampleErrors); stopWatch.Stop(); Logger.InfoFormat("Regression Tree learning took {0}", stopWatch.Elapsed); }
public void linear_without_threshold_doesnt_solve_xor() { double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] xor = { -1, 1, 1, -1 }; // Create the sequential minimal optimization teacher var learn = new SequentialMinimalOptimization() { Complexity = 1e-5 }; // Run the learning algorithm SupportVectorMachine machine = learn.Learn(inputs, xor); bool[] output = machine.Decide(inputs); for (int i = 0; i < output.Length; i++) { Assert.AreEqual(false, output[i]); } }
static void trainTwoClass(double[][] inputs, int[] outputs) { var teacher = new SequentialMinimalOptimization <Gaussian>() { Complexity = 10, Kernel = new Gaussian(3) // Estimate a suitable guess for the Gaussian kernel's parameters. // This estimate can serve as a starting point for a grid search. //UseKernelEstimation = true }; // Learn a machine var machine = teacher.Learn(inputs, outputs); bool[] predicted = machine.Decide(inputs); // Get class scores for each sample double[] scores = machine.Score(inputs); // Compute classification error double error = new ZeroOneLoss(outputs).Loss(predicted); }
public void Learn_CacheSizeZero_CacheSizeShouldBeZero() { double[][] inputs = { new double[] { -1, -1 }, new double[] { -1, 1 }, new double[] { 1, -1 }, new double[] { 1, 1 } }; int[] xor = { -1, 1, 1, -1 }; KernelSupportVectorMachine svm = new KernelSupportVectorMachine(new Polynomial(2), inputs[0].Length); SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, xor) { CacheSize = 0 }; smo.Run(); Assert.AreEqual(smo.CacheSize, 0); }
public static SupportVectorMachine <Gaussian> MakeDeserialization(string path) { var teacher = new SequentialMinimalOptimization <Gaussian>() { UseComplexityHeuristic = true, UseKernelEstimation = true }; double[][] inputs2 = new double[4][]; inputs2[0] = new[] { 1.0, 4 }; inputs2[1] = new[] { 6.0, 8 }; inputs2[2] = new[] { 60.0, 78 }; inputs2[3] = new[] { 60.0, 90 }; double[] outputs2 = { 1, 1, 0, 0 }; SupportVectorMachine <Gaussian> svmAfter = teacher.Learn(inputs2, outputs2); SVMGaussianData dataAfter = Deserialize <SVMGaussianData>(path); svmAfter.NumberOfInputs = dataAfter.NumberOfInputs; svmAfter.NumberOfOutputs = dataAfter.NumberOfOutputs; svmAfter.SupportVectors = dataAfter.SupportVectors; svmAfter.Threshold = dataAfter.Threshold; svmAfter.Weights = dataAfter.Weights; Gaussian g = new Gaussian(); g.Gamma = dataAfter.Gamma; g.Sigma = dataAfter.Sigma; g.SigmaSquared = dataAfter.SigmaSquared; svmAfter.Kernel = g; return(svmAfter); }
//public SupportVectorMachine SVM //{ // get { return svm; } // private set { svm = value; } //} public override void TrainningModel(TrainningData trainningData) { ContinuousDataTableAdapter continuousDataTableAdapter = new ContinuousDataTableAdapter(); DataTable continuousDataTable = continuousDataTableAdapter.GetData(); DataTable dataTable = continuousDataTable.DefaultView.ToTable(false, TableMetaData.TestingAttributes); string[] columnNames; double[][] inputs = dataTable.ToArray(out columnNames); int[] outputs = (int[])trainningData.ClassificationAttribute.Clone(); // Create output for SVM (-1 or 1) for (int index = 0; index < outputs.Length; index++) { if (outputs[index] == 0) { outputs[index] = -1; } } // Create a Support Vector Machine for the given inputs //this.svm = new SupportVectorMachine(inputs[0].Length); //// Create a Kernel Support Vector Machine for the given inputs this.svm = new KernelSupportVectorMachine(new Gaussian(0.1), inputs[0].Length); // Instantiate a new learning algorithm for SVMs SequentialMinimalOptimization smo = new SequentialMinimalOptimization(svm, inputs, outputs); // Set up the learning algorithm smo.Complexity = 1.0; // Run the learning algorithm double error = smo.Run(); }
public void learn() { #region doc_learn // Let's try to obtain a classifier for an // example 2D binary classification dataset: var iris = new DataSets.YinYang(); double[][] inputs = iris.Instances; bool[] outputs = iris.ClassLabels; // Create a learning algorithm with the tensor product kernel var smo = new SequentialMinimalOptimization <Tensor <IKernel> >() { // Combine multiple kernels using the tensor product Kernel = new Tensor <IKernel>(new Linear(), new Gaussian(0.01), new Gaussian(3.6)) }; // Use it to learn a new s.v. machine var svm = smo.Learn(inputs, outputs); // Now we can compute predicted values bool[] predicted = svm.Decide(inputs); // And check how far we are from the expected values double error = new ZeroOneLoss(outputs).Loss(predicted); // error will be 0.0 #endregion Assert.AreEqual(0.0, error, 1e-6); }
static void Main(string[] args) { 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 }; // Next, we create a linear Support Vector Machine with 4 inputs SupportVectorMachine machine = new SupportVectorMachine(inputs: 3); // Create the sequential minimal optimization learning algorithm var smo = new SequentialMinimalOptimization(machine, inputs, outputs); // We learn the machine double error = smo.Run(); // And then extract its predicted labels double[] predicted = new double[inputs.Length]; for (int i = 0; i < predicted.Length; i++) { predicted[i] = machine.Compute(inputs[i]); } // At this point, the output vector contains the labels which // should have been assigned by the machine, and the predicted // vector contains the labels which have been actually assigned. // 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 cut-off points roc.GetScatterplot(true); Console.WriteLine(roc.Area.ToString()); Console.Write(roc.StandardError.ToString()); }
public void Setup() { ksvm = new SupportVectorMachine <Polynomial>(inputs: 2, kernel: new Polynomial(2)); smo = new SequentialMinimalOptimization <Polynomial>() { Model = ksvm }; }
public SVM(string TrainedDataInputFile) //// Do training for all existing trained Data { _engine = new TesseractEngine(@"./tessdata3", "eng", EngineMode.TesseractAndCube); _engine.SetVariable("tessedit_char_whitelist", "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"); _engine.SetVariable("tessedit_char_blacklist", "¢§+~»~`!@#$%^&*()_+-={}[]|\\:\";\'<>?,./"); string[] TrainedData = Directory.GetFiles(TrainedDataInputFile, "*.png"); double[][] inputs = new double[TrainedData.Length][]; /// double[] InputArray = new double[784]; int[] Outputs = new int[TrainedData.Length]; for (int i = 0; i < TrainedData.Length; i++) { string filename = Path.GetFileNameWithoutExtension(TrainedData[i]); Bitmap TrainingImage = new Bitmap(TrainedData[i]); string[] split = filename.Split('.'); for (int j = 0; j < 28; j++) { for (int k = 0; k < 28; k++) { if ((!TrainingImage.GetPixel(j, k).Name.Equals("ffffffff"))) { InputArray[j * 28 + k] = 1; } else { InputArray[j * 28 + k] = 0; } } } inputs[i] = InputArray; Outputs[i] = Convert.ToInt32(split[0]); InputArray = new double[784]; } IKernel kernel; kernel = new Polynomial(2, 0); ksvm = new MulticlassSupportVectorMachine(784, kernel, 2); MulticlassSupportVectorLearning ml = new MulticlassSupportVectorLearning(ksvm, inputs, Outputs); double complexity = 1; ///// set these three parameters Carefuly later double epsilon = 0.001; double tolerance = 0.2; ml.Algorithm = (svm, classInputs, classOutputs, i, j) => { var smo = new SequentialMinimalOptimization(svm, classInputs, classOutputs); smo.Complexity = complexity; /// Cost parameter for SVM smo.Epsilon = epsilon; smo.Tolerance = tolerance; return(smo); }; // Train the machines. It should take a while. double error = ml.Run(); }
public void ComputeTest5() { var dataset = SequentialMinimalOptimizationTest.GetYingYang(); var inputs = dataset.Submatrix(null, 0, 1).ToJagged(); 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); } }