The code

Here is the code that implements our sample program and produces the previous output. As you can see, the code is very simple and everything in here (save the shuffling of indices) is code we have already walked through before. We’ll keep the verbosity to a minimum so that you can concentrate on the code itself. This sample will read in our data, parse it into observations and target samples, and then create a learner using 1,000 trees. From there we will use the learner to learn and create our model. Once this is complete we will calculate our mean squared error metric and display it on the screen:

var parser = new CsvParser(() =>new StringReader(Resources.Glass));
var observations = parser.EnumerateRows(v => v != "Target").ToF64Matrix();
var targets = parser.EnumerateRows("Target").ToF64Vector();
int trees = 1000;
var sut = new RegressionExtremelyRandomizedTreesLearner(trees, 1, 100, 1, 0.0001, 1.0, 42, false);
var indices = Enumerable.Range(0, targets.Length).ToArray();
indices.Shuffle(new Random(42));
indices = indices.Take((int)(targets.Length * 0.7)).ToArray();
var model = sut.Learn(observations, targets, indices);
var predictions = model.Predict(observations);
var evaluator = new MeanSquaredErrorRegressionMetric();
var error = evaluator.Error(targets, predictions);
Console.WriteLine("Error: " + error);
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset