machine learning libraries in C#

Check out this awesome list on GitHub. Of the frameworks listed, Accord.NET is open-source and the most popular with over 2,000 stars.

Also, check out the official machine learning library for .NET provided by Microsoft: https://github.com/dotnet/machinelearning


OLD

There's a neural network library called AForge.net on the codeproject. (Code hosted at Google code) (Also checkout the AForge homepage - According to the homepage, the new version now supports genetic algorithms and machine learning as well. It looks like it's progressed a lot since I last played with it)

I don't know it's it's anything like WEKA as I've never used that.

(there's also an article on it's usage)


You can also use Weka with C#. The best solution is to use IKVM, as in this tutorial, although you can also use bridging software.


Weka can be used from C# very easily as Shane stated, using IKVM and some 'glue code'. Folow the tutorial on weka page to create the '.Net version' of weka, then you can try to run the following tests:

[Fact]
public void BuildAndClassify()
{
  var classifier = BuildClassifier();
  AssertCanClassify(classifier);
}

[Fact]
public void DeserializeAndClassify()
{
  BuildClassifier().Serialize("test.weka");
  var classifier = Classifier.Deserialize<LinearRegression>("test.weka");
  AssertCanClassify(classifier);
}

private static void AssertCanClassify(LinearRegression classifier)
{
  var result = classifier.Classify(-402, -1);
  Assert.InRange(result, 255.8d, 255.9d);
}

private static LinearRegression BuildClassifier()
{
  var trainingSet = new TrainingSet("attribute1", "attribute2", "class")
    .AddExample(-173, 3, -31)
    .AddExample(-901, 1, 807)
    .AddExample(-901, 1, 807)
    .AddExample(-94, -2, -86);

  return Classifier.Build<LinearRegression>(trainingSet);
}

First test shows, how you build a classifier and classify a new Example with it, the second one shows, how you can use a persisted classifier from a file to classify an example. If you need too support discrete attributes, some modification will be necessery. The code above uses 2 helper classes:

public class TrainingSet
{
    private readonly List<string> _attributes = new List<string>();
    private readonly List<List<object>> _examples = new List<List<object>>();

    public TrainingSet(params string[] attributes)
    {
      _attributes.AddRange(attributes);
    }

    public int AttributesCount
    {
      get { return _attributes.Count; }
    }

    public int ExamplesCount
    {
      get { return _examples.Count; }
    }

    public TrainingSet AddExample(params object[] example)
    {
      if (example.Length != _attributes.Count)
      {
        throw new InvalidOperationException(
          String.Format("Invalid number of elements in example. Should be {0}, was {1}.", _attributes.Count,
            _examples.Count));
      }


      _examples.Add(new List<object>(example));

      return this;
    }

    public static implicit operator Instances(TrainingSet trainingSet)
    {
      var attributes = trainingSet._attributes.Select(x => new Attribute(x)).ToArray();
      var featureVector = new FastVector(trainingSet.AttributesCount);

      foreach (var attribute in attributes)
      {
        featureVector.addElement(attribute);
      }

      var instances = new Instances("Rel", featureVector, trainingSet.ExamplesCount);
      instances.setClassIndex(trainingSet.AttributesCount - 1);

      foreach (var example in trainingSet._examples)
      {
        var instance = new Instance(trainingSet.AttributesCount);

        for (var i = 0; i < example.Count; i++)
        {
          instance.setValue(attributes[i], Convert.ToDouble(example[i]));
        }

        instances.add(instance);
      }

      return instances;
    }
}

public static class Classifier
{
    public static TClassifier Build<TClassifier>(TrainingSet trainingSet)
      where TClassifier : weka.classifiers.Classifier, new()
    {
      var classifier = new TClassifier();
      classifier.buildClassifier(trainingSet);
      return classifier;
    }

    public static TClassifier Deserialize<TClassifier>(string filename)
    {
      return (TClassifier)SerializationHelper.read(filename);
    }

    public static void Serialize(this weka.classifiers.Classifier classifier, string filename)
    {
      SerializationHelper.write(filename, classifier);
    }

    public static double Classify(this weka.classifiers.Classifier classifier, params object[] example)
    {
      // instance lenght + 1, because class variable is not included in example
      var instance = new Instance(example.Length + 1);

      for (int i = 0; i < example.Length; i++)
      {
        instance.setValue(i, Convert.ToDouble(example[i]));
      }

      return classifier.classifyInstance(instance);
    }
}