Accord.NET 使用案例
阿新 • • 發佈:2019-01-04
例項一:訓練SVM,解決XOR分類問題
異或問題的原理:相同為真,不同為假。
需要使用的包:Accord.MachineLearning、Accord.Controls、Accord.Math、Accord.Statistics。
程式碼如下:C#、控制檯應用程式。
using Accord.Controls; using Accord.MachineLearning.VectorMachines.Learning; using Accord.Math.Optimization.Losses; using Accord.Statistics; using Accord.Statistics.Kernels; using System; namespace Support_Vector_Machines { class Program { static void Main(string[] args) { double[][] inputs = { new double[] { 0, 0 }, new double[] { 1, 0 }, new double[] { 0, 1 }, new double[] { 1, 1 }, }; int[] outputs = { 0, 1, 1, 0, }; //Create the learning algorithm with the chose kernel var smo = new SequentialMinimalOptimization<Gaussian>() { Complexity = 100 }; //Use the algorithm to learn the svm var svm = smo.Learn(inputs, outputs); //Compute the machina`s answer for the given inputs bool[] prediction = svm.Decide(inputs); //Compute the classification error between the expected //values and the values actually predicted by the machine double error = new AccuracyLoss(outputs).Loss(prediction); Console.WriteLine("Error:" + error); //Show results on screen ScatterplotBox.Show("Training data", inputs, outputs); ScatterplotBox.Show("SVM results", inputs, prediction.ToZeroOne()); Console.ReadKey(); } } }
結果如下: