cs-libsvm 1.0.1

dotnet add package cs-libsvm --version 1.0.1
NuGet\Install-Package cs-libsvm -Version 1.0.1
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="cs-libsvm" Version="1.0.1" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add cs-libsvm --version 1.0.1
#r "nuget: cs-libsvm, 1.0.1"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install cs-libsvm as a Cake Addin
#addin nuget:?package=cs-libsvm&version=1.0.1

// Install cs-libsvm as a Cake Tool
#tool nuget:?package=cs-libsvm&version=1.0.1

cs-libsvm

libsvm ported to C#

Install

Install-Package cs-libsvm

Usage

Binary Classification by SVC

The sample codes below shows how to do binary classification using SVC:

using System;
using System.Collections.Generic;

namespace LibSvmSharp
{
    class SVCDemo
    {
        private static Random random = new Random();

        public static void SolveBinaryClassificationProblem()
        {
            SVC svc = new SVC();


            List<KeyValuePair<double[], bool>> data = new List<KeyValuePair<double[], bool>>();
            for(int i=0; i < 100; ++i)
            {
                double[] x = new double[2];
                x[0] = i / 50.0;
                x[1] = (i+1) / 100.0;
                bool y = Math.Sin((x[0] + x[1]) * Math.PI) > 0;
                data.Add(new KeyValuePair<double[], bool>(x, y));
            }

            var tuple = TrainTestSplit(data);
            var train_data = tuple.Item1;
            var test_data = tuple.Item2;

            var metric = svc.Fit(train_data, test_data);

            Console.WriteLine("Train Acc: {0}, Test Acc: {1}", metric.TrainAccuracy, metric.TestAccuracy);
            
            foreach(KeyValuePair<double[], bool> entry in test_data.GetRange(0, 10)) {
                double[] x = entry.Key;
                bool y = entry.Value;
                double output = svc.Predict(x);
                bool prediction = output > 0;
                Console.WriteLine("Predicted: {0} Actual: {1}", prediction, y);
            }
        }

        private static Tuple<List<T>, List<T>> TrainTestSplit<T>(List<T> a, double test_size=0.2)
        {
            Shuffle(a);
            int splitIndex = (int)(a.Count * test_size);
            List<T> test_data = new List<T>();
            List<T> train_data = new List<T>();
            for(int i=0; i < a.Count; ++i)
            {
                if(i < splitIndex)
                {
                    test_data.Add(a[i]);
                } else
                {
                    train_data.Add(a[i]);
                }
            }
            return new Tuple<List<T>, List<T>>(train_data, test_data);
        }

        private static void Shuffle<T>(List<T> a)
        {
            int i = 0; 
            while(i < a.Count)
            {
                int j = random.Next(i + 1);
                Swap(a, i++, j);
            }
        }

        private static void Swap<T>(List<T> a, int i, int j)
        {
            T temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
}

Regression Problem by SVR

The sample codes below shows how to do regression using SVR:

using System;
using System.Collections.Generic;

namespace LibSvmSharp
{
    class SVRDemo
    {
        private static Random random = new Random();

        public static void SolveRegressionProblem()
        {
            SVR svr = new SVR();


            List<KeyValuePair<double[], double>> data = new List<KeyValuePair<double[], double>>();
            for(int i=0; i < 100; ++i)
            {
                double[] x = new double[2];
                x[0] = i / 50.0;
                x[1] = (i+1) / 100.0;
                double y = Math.Sin((x[0] + x[1]) * Math.PI);
                data.Add(new KeyValuePair<double[], double>(x, y));
            }

            var tuple = TrainTestSplit(data);
            var train_data = tuple.Item1;
            var test_data = tuple.Item2;

            var metric = svr.Fit(train_data, test_data);

            Console.WriteLine("Train MSE: {0}, Test MSE: {1}", metric.TrainMSE, metric.TestMSE);
            
            foreach(KeyValuePair<double[], double> entry in test_data.GetRange(0, 10)) {
                double[] x = entry.Key;
                double y = entry.Value;
                double output = svr.Predict(x);
                Console.WriteLine("Predicted: {0} Actual: {1}", output, y);
            }
        }

        private static Tuple<List<T>, List<T>> TrainTestSplit<T>(List<T> a, double test_size=0.2)
        {
            Shuffle(a);
            int splitIndex = (int)(a.Count * test_size);
            List<T> test_data = new List<T>();
            List<T> train_data = new List<T>();
            for(int i=0; i < a.Count; ++i)
            {
                if(i < splitIndex)
                {
                    test_data.Add(a[i]);
                } else
                {
                    train_data.Add(a[i]);
                }
            }
            return new Tuple<List<T>, List<T>>(train_data, test_data);
        }

        private static void Shuffle<T>(List<T> a)
        {
            int i = 0; 
            while(i < a.Count)
            {
                int j = random.Next(i + 1);
                Swap(a, i++, j);
            }
        }

        private static void Swap<T>(List<T> a, int i, int j)
        {
            T temp = a[i];
            a[i] = a[j];
            a[j] = temp;
        }
    }
}

Outlier Detection Problem by OneClassSVM

Please visit the github page for the details of the outlier detection problem demo codes

Product Compatible and additional computed target framework versions.
.NET Framework net461 is compatible.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

This package has no dependencies.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.1 1,104 5/4/2018

LibSVM Port in C# in .NET 4.6.1