Getting started - accord-net/framework GitHub Wiki

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
1 1 0

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 Accord.Controls;
using Accord.MachineLearning.VectorMachines.Learning;
using Accord.Math.Optimization.Losses;
using Accord.Statistics;
using Accord.Statistics.Kernels;
using System;

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();
        }
    }
}

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.

⚠️ **GitHub.com Fallback** ⚠️