Random Forest in .Net Core
Technology | C# |
---|---|
Editor of the Page | Latif Bahadır ALTUN |
Install-Package SharpLearning.RandomForest
var fs = new FileStream(@"C:\RandomForestLearner\data.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
string content;
using (StreamReader reader = new StreamReader(fs, Encoding.UTF8))
{
content = reader.ReadToEnd();
}
var parser = new CsvParser(() => new StringReader(content));
var targetName = "label";
var observations = parser.EnumerateRows(c => c != targetName).ToF64Matrix();
var targets = parser.EnumerateRows(targetName).ToF64Vector();
var learner = new ClassificationRandomForestLearner(trees: 100);
var model = learner.Learn(observations, targets);
var testFs = new FileStream(@"C:\RandomForestLearner\result.csv", FileMode.Open, FileAccess.Read, FileShare.ReadWrite | FileShare.Delete);
string testContent;
using (StreamReader testReader = new StreamReader(testFs, Encoding.UTF8))
{
testContent = testReader.ReadToEnd();
}
var testParser = new CsvParser(() => new StringReader(testContent));
var testObserve = testParser.EnumerateRows(c => c != targetName).ToF64Matrix();
var predictions = model.Predict(testObserve);
model.Save(() => new StreamWriter("randomforest.xml"));