private static FoldValidationResult <TSolution> ProcessFold <TSolution>(string[][] folds, int testFoldIndex, ICaseFrame cases, ICaseTransformer pipeline, Reasoner reasoner) { var watch = Stopwatch.StartNew(); var result = new FoldValidationResult <TSolution>(testFoldIndex, folds[testFoldIndex].Length); var testFold = cases.FilterCases(folds[testFoldIndex]); var trainFold = cases.FilterCases(folds.Where((f, i) => i != testFoldIndex).SelectMany(f => f)); pipeline.Prepare(trainFold); testFold = pipeline.Transform(testFold); trainFold = pipeline.Transform(trainFold); reasoner.SetData(trainFold); using (var cursor = testFold.GetCaseCursor()) { while (cursor.MoveNext()) { var trueSolution = cursor.GetSolution <TSolution>(); var predictedSolution = reasoner.GetSolution <TSolution>(cursor); result.AddPrediction(trueSolution, predictedSolution); } } watch.Stop(); result.ValidationTime = watch.ElapsedMilliseconds; return(result); }
public static CrossValidationResult <TSolution> ValidateInParallel <TSolution>(string[][] folds, ICaseFrame cases, Func <Reasoner> reasonerBuilder, Func <ICaseTransformer> pipelineBuilder = null, ParallelOptions options = null) { if (folds == null) { throw new ArgumentNullException(nameof(folds)); } if (cases == null) { throw new ArgumentNullException(nameof(cases)); } if (reasonerBuilder == null) { throw new ArgumentNullException(nameof(reasonerBuilder)); } var results = new FoldValidationResult <TSolution> [folds.Length]; Parallel.For(0, folds.Length, options ?? new ParallelOptions(), i => { results[i] = ProcessFold <TSolution>(folds, i, cases, pipelineBuilder?.Invoke(), reasonerBuilder()); }); return(new CrossValidationResult <TSolution>(results)); }
public static CrossValidationResult <TSolution> Validate <TSolution>(string[][] folds, ICaseFrame cases, Reasoner reasoner, ICaseTransformer pipeline = null) { if (folds == null) { throw new ArgumentNullException(nameof(folds)); } if (cases == null) { throw new ArgumentNullException(nameof(cases)); } if (reasoner == null) { throw new ArgumentNullException(nameof(reasoner)); } var results = new FoldValidationResult <TSolution> [folds.Length]; for (var i = 0; i < folds.Length; i++) { results[i] = ProcessFold <TSolution>(folds, i, cases, pipeline, reasoner); } return(new CrossValidationResult <TSolution>(results)); }