Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleModel.cs @ 16628

Last change on this file since 16628 was 16628, checked in by gkronber, 5 years ago

#2971: made branch compile with current version of trunk

File size: 4.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HEAL.Attic;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  /// <summary>
31  /// Represents classification solutions that contain an ensemble of multiple classification models
32  /// </summary>
33  [StorableType("7EA6E802-999D-4432-A13F-0DCBC1A4F8FB")]
34  [Item("ClassificationEnsembleModel", "A classification model that contains an ensemble of multiple classification models")]
35  public class ClassificationEnsembleModel : ClassificationModel, IClassificationEnsembleModel {
36    public override IEnumerable<string> VariablesUsedForPrediction {
37      get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
38    }
39
40    [Storable]
41    private List<IClassificationModel> models;
42    public IEnumerable<IClassificationModel> Models {
43      get { return new List<IClassificationModel>(models); }
44    }
45
46    [StorableConstructor]
47    protected ClassificationEnsembleModel(StorableConstructorFlag _) : base(_) { }
48    protected ClassificationEnsembleModel(ClassificationEnsembleModel original, Cloner cloner)
49      : base(original, cloner) {
50      this.models = original.Models.Select(m => cloner.Clone(m)).ToList();
51    }
52
53    public ClassificationEnsembleModel() : this(Enumerable.Empty<IClassificationModel>()) { }
54    public ClassificationEnsembleModel(IEnumerable<IClassificationModel> models)
55      : base(string.Empty) {
56      this.name = ItemName;
57      this.description = ItemDescription;
58      this.models = new List<IClassificationModel>(models);
59
60      if (this.models.Any()) this.TargetVariable = this.models.First().TargetVariable;
61    }
62
63    public override IDeepCloneable Clone(Cloner cloner) {
64      return new ClassificationEnsembleModel(this, cloner);
65    }
66
67    public void Add(IClassificationModel model) {
68      if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = model.TargetVariable;
69      models.Add(model);
70    }
71    public void Remove(IClassificationModel model) {
72      models.Remove(model);
73      if (!models.Any()) TargetVariable = string.Empty;
74    }
75
76    public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(IDataset dataset, IEnumerable<int> rows) {
77      var estimatedValuesEnumerators = (from model in models
78                                        select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
79                                       .ToList();
80
81      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
82        yield return from enumerator in estimatedValuesEnumerators
83                     select enumerator.Current;
84      }
85    }
86
87
88    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
89      foreach (var estimatedValuesVector in GetEstimatedClassValueVectors(dataset, rows)) {
90        // return the class which is most often occuring
91        yield return
92          estimatedValuesVector
93          .GroupBy(x => x)
94          .OrderBy(g => -g.Count())
95          .Select(g => g.Key)
96          .First();
97      }
98    }
99
100    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
101      return new ClassificationEnsembleSolution(models, new ClassificationEnsembleProblemData(problemData));
102    }
103
104
105  }
106}
Note: See TracBrowser for help on using the repository browser.