SVM分类器的介绍:https://en.wikipedia.org/wiki/Support_vector_machine
在本例中,调用的accrod-net来实现,使用的SMO算法,主要基于公式(拉格朗日的对偶形式):
Max(alpha[i], i∈[1,n]) - 1/2 * y[i]*y[j]*Kernal(x[i],x[j])*alpha[i]*alpha[j]
其中,alpha[i]为拉格朗日乘子,对上式进行迭代,在符合KKT条件(https://en.wikipedia.org/wiki/Karush–Kuhn–Tucker_conditions)下,进行迭代。调整alpha[i]和alpha[j],直到收敛。
核函数Kernal(x[i],x[j])的作用是将数据提升维度,达到线性可分。
实例代码:
public static class SVMDEmo
{
public static void Execute()
{
double[][] inputs =
{
new double[] { 1, 3 }, //-1
new double[] { -1, 3 },//1
new double[] { 2, 5 }, //-1
new double[] { 3, -4 },//1
new double[] { -3, 6 },//-1
new double[] { 7, 2 }, //1
new double[] { 2, 9 }, //-1
new double[] { 10, 1 },//1
new double[] { 1, 8 }, //-1
new double[] { 3, 10 },//1
};
// Dichotomy SVM outputs should be given as [-1;+1]
int[] labels =
{
-1,1,
-1,1,
-1,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 = Math.Sign(machine.Compute(new double[] { 10, 12 }));
Console.WriteLine(decision);
}
}
static void Main(string[] args)
{
SVMDEmo.Execute();
Console.ReadLine();
}