private Tree GroupByClass() { var groupings = Instances.DistinctBy(i => i.Output.Value) .ToDictionary(i => i.Features.First(), j => new Tree { Leaf = j.Output }); if (groupings.Count() > 1) { return(new Tree { Branches = groupings }); } return(new Tree { Leaf = groupings.First().Value.Leaf }); }