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 6, 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.

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.

!http://accord-framework.net/images/guides/starting/vs-1.png

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

!http://accord-framework.net/images/guides/starting/vs-2.png

    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.

!http://accord-framework.net/images/guides/starting/vs-3.png

At this point we can start adding references to our project. 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.

!http://accord-framework.net/images/guides/starting/vs-6.png" />

!http://accord-framework.net/images/guides/starting/vs-7.png" />

    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.

<table>
    <tr>
        <td colspan="2">
            <strong>inputs</strong>
        </td>
        <td>
            <strong>output</strong>
        </td>
    </tr>
    <tr>
        <td>
            <strong>a</strong>
        </td>
        <td>
            <strong>b</strong>
        </td>
        <td>
            <strong>a &oplus; b</strong>
        </td>
    </tr>
    <tr>
        <td>
            0
        </td>
        <td>
            0
        </td>
        <td>
            0
        </td>
    </tr>
    <tr>
        <td>
            0
        </td>
        <td>
            1
        </td>
        <td>
            1
        </td>
    </tr>
    <tr>
        <td>
            1
        </td>
        <td>
            0
        </td>
        <td>
            1
        </td>
    </tr>
    <tr>
        <td>
            1
        </td>
        <td>
            1
        </td>
        <td>
            0
        </td>
    </tr>
</table>


    The inputs of the problem are given by the <strong>a</strong> and <strong>b</strong>
    columns. The output is the result on <strong>a &oplus; b</strong>. 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.

!http://accord-framework.net/images/guides/starting/vs-9.png

    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.

!http://accord-framework.net/images/guides/starting/vs-10.png !http://accord-framework.net/images/guides/starting/vs-11.png

  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