Skip to content
This repository has been archived by the owner on Nov 19, 2020. It is now read-only.

Getting started

César Souza edited this page Apr 19, 2015 · 9 revisions

Setting up your environment

The easiest way to get started is through NuGet. Search for "Accord.NET" in the package manager, then chose to install the modules you are interested. The framework has been divided into modules because not all users will want to use, for example, audio or video processing capabilities in their projects if they are doing purely statistics applications. In order to follow this tutorial, start by installing

    PM> Install-Package Accord.MachineLearning
    PM> Install-Package Accord.Controls

If you don't already have a working .NET development environment, install Microsoft Visual Studio Express (or equivalent)

Creating a new project

We are going to develop a sample application which uses Support Vector Machines. We will start this example by opening up Visual Studio.

After Visual Studio has finished loading, click the File->New Project menu item to start creating a new project.

In the New Project Dialog, select the item "Console Application" under the Visual C# category. Even if we are going to use C# in this example, please keep in mind that Accord.NET supports all .NET compatible languages such as VB.NET or C++/CLI.

At this point we can start adding references to our project. Either run the Install-Package commands on top of this tutorial, or right-click the project name on the Solution Browser and select "Manage NuGet packages".

Since we are going to develop a sample application which uses Support Vector Machines, we will want to install Accord.MachineLearning through NuGet. If you wish to report information using visual controls, install also Accord.Controls.

Coding

Now we are ready to start developing our application. The following code demonstrates how to create and teach a SVM to recognize the classic XOR problem. The XOR problem is a classical nonlinearly separable problem which can not be solved by a simple linear classifier (such as a Perceptron, or a Linear SVM). The problem consists on learning the following boolean function.

a b a ⊕ b
0 0 0
0 1 1
1 0 1

The inputs of the problem are given by the a and b columns. The output is the result on a ⊕ b. To solve it, we will use a Polynomial kernel machine. Support Vector Machines require the outputs to be either -1 or +1, so we will assign -1 to any zero values in the output columns.

The code for the sample application is shown below.

using System;
using Accord.Controls;
using Accord.MachineLearning.VectorMachines;
using Accord.MachineLearning.VectorMachines.Learning;
using Accord.Math; using Accord.Statistics.Kernels;

namespace SampleApplication1 
{ 
    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: */ -1,
                /* 2. 1 xor 0 = 1: */ +1,
                /* 3. 0 xor 1 = 1: */ +1,
                /* 4. 1 xor 1 = 0: */ -1,
            };
            
            // Create a new machine with a polynomial kernel and two inputs 
            var ksvm = new KernelSupportVectorMachine(new Gaussian(), 2);
            
            // Create the learning algorithm with the given inputs and outputs
            var smo = new SequentialMinimalOptimization(machine: ksvm, inputs: inputs, outputs: outputs)
            {
                Complexity = 100 // Create a hard-margin SVM 
            };
            
            
            // Teach the machine 
            double error = smo.Run();
            
            Console.WriteLine("error:" + error); 
            
            // Show results on screen 
            ScatterplotBox.Show("Training data", inputs, outputs); 
            
            ScatterplotBox.Show("SVM results", inputs, 
                inputs.Apply(p => System.Math.Sign(ksvm.Compute(p))));

            Console.ReadKey(); 
        }
    } 
}

In case you need to add any missing namespace references, you can always right-click the name of a class and let Visual Studio complete it for you.

Once the application code is finished, we can run it by hitting F5 (or equivalently, clicking on the Start Debugging button) on Visual Studio. The application will train the aforementioned Support Vector Machine and should display the training error obtained after learning. A training error of zero indicates that the machine has learned the problem perfectly.

  1. Accord.NET Framework
  2. Getting started
  3. Published books
  4. How to use
  5. Sample applications

Help improve this wiki! Those pages can be edited by anyone that would like to contribute examples and documentation to the framework.

Have you found this software useful? Consider donating only U$10 so it can get even better! This software is completely free and will always stay free. Enjoy!

Clone this wiki locally