1. 程式人生 > >M03 利用Accord 進行機器學習的第一個小例子

M03 利用Accord 進行機器學習的第一個小例子

statistic decide blog cat studio mac eap strong cte

01 安裝 Visual studio 2017.

不具備安裝這個的話,也可安裝,Microsoft Visual Studio Express (or equivalent)

02 創建 C# 的 控制臺程序

技術分享圖片

03 添加 Accord 庫

技術分享圖片

03 讓機器學習『異或』的邏輯

不需要在代碼裏寫出來異或的程序邏輯,告訴機器 異或的輸入和輸出(其實就是一個監督學習,訓練的過程),機器就自己學習會了異或邏輯。

上代碼:

 1 using System;
 2 using Accord.Controls;
 3 using Accord.MachineLearning.VectorMachines;
4 using Accord.MachineLearning.VectorMachines.Learning; 5 using Accord.Math; 6 using Accord.Statistics.Kernels; 7 using Accord.Math.Optimization.Losses; 8 using Accord.Statistics; 9 10 namespace SampleApplication1 11 { 12 class Program 13 { 14 [MTAThread] 15 static
void Main(string[] args) 16 { 17 double[][] inputs = 18 { 19 /* 1.*/ new double[] { 0, 0 }, 20 /* 2.*/ new double[] { 1, 0 }, 21 /* 3.*/ new double[] { 0, 1 }, 22 /* 4.*/ new double[] { 1, 1 }, 23 };
24 25 int[] outputs = 26 { 27 /* 1. 0 xor 0 = 0: */ 0, 28 /* 2. 1 xor 0 = 1: */ 1, 29 /* 3. 0 xor 1 = 1: */ 1, 30 /* 4. 1 xor 1 = 0: */ 0, 31 }; 32 33 // Create the learning algorithm with the chosen kernel 34 var smo = new SequentialMinimalOptimization<Gaussian>() 35 { 36 Complexity = 100 // Create a hard-margin SVM 37 }; 38 39 // Use the algorithm to learn the svm 40 var svm = smo.Learn(inputs, outputs); 41 42 // Compute the machine‘s answers for the given inputs 43 bool[] prediction = svm.Decide(inputs); 44 45 // Compute the classification error between the expected 46 // values and the values actually predicted by the machine: 47 double error = new AccuracyLoss(outputs).Loss(prediction); 48 49 Console.WriteLine("Error: " + error); 50 51 // Show results on screen 52 ScatterplotBox.Show("Training data", inputs, outputs); 53 ScatterplotBox.Show("SVM results", inputs, prediction.ToZeroOne()); 54 55 Console.ReadKey(); 56 } 57 } 58 }

04 運行搞定

技術分享圖片

本文的源代碼:

鏈接:https://pan.baidu.com/s/1gfrQyPX

密碼: 關註微信輸入關鍵字: 異或

M03 利用Accord 進行機器學習的第一個小例子