machine learning libraries in C#

C#Machine Learning

C# Problem Overview


Are there any machine learning libraries in C#? I'm after something like WEKA. Thank you.

C# Solutions


Solution 1 - 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)

Solution 2 - C#

You can also http://weka.wikispaces.com/Use+WEKA+with+the+Microsoft+.NET+Framework">use Weka with C#. The best solution is to use http://weka.wikispaces.com/IKVM+with+Weka+tutorial"><b>IKVM</b>;, as in this tutorial, although you can also use bridging software.

Solution 3 - C#

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

Solution 4 - C#

I have created an http://machine.codeplex.com">ML library in C# that is designed to work with common POCO objects.

Solution 5 - C#

There's also a project called Encog that has C# code. It's maintained by Jeff Heaton, the author of an "Introduction to Neural Network" book I bought a while ago. The codebase Git is here: https://github.com/encog/encog-dotnet-core

Solution 6 - C#

I'm searching for machine learning libraries for .NET as well and found Infer.NET from Microsoft Research on [nuget.org/machine-learning][1]:

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDervin ThunkView Question on Stackoverflow
Solution 1 - C#Simon P StevensView Answer on Stackoverflow
Solution 2 - C#ShaneView Answer on Stackoverflow
Solution 3 - C#Gregor SlavecView Answer on Stackoverflow
Solution 4 - C#Seth JuarezView Answer on Stackoverflow
Solution 5 - C#vanni.torelliView Answer on Stackoverflow
Solution 6 - C#Ole LyngeView Answer on Stackoverflow