.net随笔- vb.net Accord.Net机器学习库安装与SVM简单分类
新建一个窗口项目。
PM> Install-Package Accord -Version 3.8.0
正在尝试收集与目标为“.NETFramework,Version=v4.6”的项目“learnai”有关的包“Accord.3.8.0”的依赖项信息
正在尝试解析程序包“Accord.3.8.0”的依赖项,DependencyBehavior 为“Lowest”
正在解析操作以安装程序包“Accord.3.8.0”
已解析操作以安装程序包“Accord.3.8.0”
GET https://api.nuget.org/v3-flatcontainer/accord/3.8.0/accord.3.8.0.nupkg
OK https://api.nuget.org/v3-flatcontainer/accord/3.8.0/accord.3.8.0.nupkg 84ms
正在安装 Accord 3.8.0。
正在将程序包“Accord.3.8.0”添加到文件夹“E:\pro\books\AI_.net\src\AI_learn\learnai\packages”
已将程序包“Accord.3.8.0”添加到文件夹“E:\pro\books\AI_.net\src\AI_learn\learnai\packages”
已将程序包“Accord.3.8.0”添加到“packages.config”
已将“Accord 3.8.0”成功安装到 learnai
PM> Install-Package Accord.MachineLearning
PM> Install-Package Accord.Controls
先从C#程序开始,建立一个c#控制台程序,注意引入System.Windows.Forms
using System;
using Accord.Controls;
using Accord.MachineLearning.VectorMachines.Learning;
using Accord.Math.Optimization.Losses;
using Accord.Statistics;
using Accord.Statistics.Kernels;
namespace GettingStarted
{
class Program
{
[MTAThread]
static void Main(string[] args)
{
double[][] inputs =
{
/* 1.*/ new double[] { 0, 0 },
/* 2.*/ new double[] { 1, 0 },
/* 3.*/ new double[] { 0, 1 },
/* 4.*/ new double[] { 1, 1 },
};
int[] outputs =
{
/* 1. 0 xor 0 = 0: */ 0,
/* 2. 1 xor 0 = 1: */ 1,
/* 3. 0 xor 1 = 1: */ 1,
/* 4. 1 xor 1 = 0: */ 0,
};
// Create the learning algorithm with the chosen kernel
var smo = new SequentialMinimalOptimization<Gaussian>()
{
Complexity = 100 // Create a hard-margin SVM
};
// Use the algorithm to learn the svm
var svm = smo.Learn(inputs, outputs);
// Compute the machine's answers 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();
}
}
}
下面是vb.net版本的
Imports Accord.Controls
Imports Accord.MachineLearning.VectorMachines.Learning
Imports Accord.Math.Optimization.Losses
Imports Accord.Statistics
Imports Accord.Statistics.Kernels
Imports System
Public Class Form1
'异或运算的SVM分类
'定义样本
Dim inputs As Double()() =
{
New Double() {0, 0},
New Double() {1, 0},
New Double() {0, 1},
New Double() {1, 1}
}
Dim outputs As Integer() =
{
0,' 1. 0 Xor 0 = 0
1, ' 2. 1 Xor 0 = 1
1,' 3. 0 Xor 1 = 1
0' 4. 1 Xor 1 = 0
}
'创建高斯核的SVM分类
Dim smo As New SequentialMinimalOptimization(Of Gaussian)
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
smo.Complexity = 100
Dim svm = smo.Learn(inputs, outputs)
Dim prediction As Boolean() = svm.Decide(inputs)
Dim trainError As Double = New AccuracyLoss(outputs).Loss(prediction)
Label1.Text = "Error: " + Str(trainError)
ScatterplotBox.Show("Training data", inputs, outputs)
ScatterplotBox.Show("SVM results", inputs, prediction.ToZeroOne())
End Sub
下面的示例演示了泛型类的主干定义。
Public Class classHolder(Of t)
Public Sub processNewItem(ByVal newItem As t)
Dim tempItem As t
' Insert code that processes an item of data type t.
End Sub
End Class
在上面的主干中, t 是一个 类型形参,即你在声明此类时提供的数据类型的占位符。 在代码中的其他地方,可以通过为 classHolder 提供不同的数据类型来声明不同版本的 t 下面的示例演示了两个此类声明。
Public integerClass As New classHolder(Of Integer)
Friend stringClass As New classHolder(Of String)
上面的语句声明了 构造类,在这些类中,特定的类型替换了类型形参。 此类替换会在构造类中的代码内进行传播。 下面的示例显示了 processNewItem 过程在 integerClass中的外观。
Public Sub processNewItem(ByVal newItem As Integer)
Dim tempItem As Integer
' Inserted code now processes an Integer item.
End Sub