machine learning libraries in C#

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

57763 次浏览

Check out 这个超赞的名单 on GitHub. Of the frameworks listed, Accord.NET is open-source and the most popular with over 2,000 stars.

另外,请查看 Microsoft 提供的官方.NET 机器学习库: https://github.com/dotnet/machinelearning


OLD

There's a neural network library called AForge.net on the codeproject. (Code hosted at 谷歌代码) (Also checkout the AForge 主页 - 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)

我不知道它是否像 WEKA,因为我从来没有用过。

(还有一篇关于 用途的文章)

您也可以使用 在 c # 中使用 Weka。最好的解决方案是使用 IKVM ,如本教程所示,尽管您也可以使用桥接软件。

我已经在 C # 中创建了一个 机器学习图书馆,用于处理常见的 POCO 对象。

正如 Shane 所说,Weka 可以很容易地从 C # 中使用,使用 IKVM 和一些“粘合代码”。按照 Weka Page上的教程创建 weka 的“ .Net 版本”,然后您可以尝试运行以下测试:

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

第一个测试展示了如何构建一个分类器并用它对一个新示例进行分类,第二个测试展示了如何使用文件中的持久化分类器对一个示例进行分类。如果您过于需要对离散属性的支持,则需要进行一些修改。上面的代码使用了两个助手类:

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

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

我也在为.NET 搜索机器学习库,在 Nuget.org/machine-learning上找到了来自微软研究院的地狱网: