Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2839_HiveProjectManagement/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationEnsembleModel.cs

Last change on this file was 16057, checked in by jkarder, 6 years ago

#2839:

File size: 4.2 KB
RevLine 
[5662]1#region License Information
2/* HeuristicLab
[16057]3 * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[5662]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;
27
28namespace HeuristicLab.Problems.DataAnalysis {
29  /// <summary>
30  /// Represents classification solutions that contain an ensemble of multiple classification models
31  /// </summary>
32  [StorableClass]
33  [Item("ClassificationEnsembleModel", "A classification model that contains an ensemble of multiple classification models")]
[13941]34  public class ClassificationEnsembleModel : ClassificationModel, IClassificationEnsembleModel {
35    public override IEnumerable<string> VariablesUsedForPrediction {
[13921]36      get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
37    }
[5662]38
39    [Storable]
40    private List<IClassificationModel> models;
41    public IEnumerable<IClassificationModel> Models {
42      get { return new List<IClassificationModel>(models); }
43    }
[6239]44
[5662]45    [StorableConstructor]
46    protected ClassificationEnsembleModel(bool deserializing) : base(deserializing) { }
47    protected ClassificationEnsembleModel(ClassificationEnsembleModel original, Cloner cloner)
48      : base(original, cloner) {
49      this.models = original.Models.Select(m => cloner.Clone(m)).ToList();
50    }
[6666]51
52    public ClassificationEnsembleModel() : this(Enumerable.Empty<IClassificationModel>()) { }
[5662]53    public ClassificationEnsembleModel(IEnumerable<IClassificationModel> models)
[13941]54      : base(string.Empty) {
[5662]55      this.name = ItemName;
56      this.description = ItemDescription;
[5777]57      this.models = new List<IClassificationModel>(models);
[13941]58
59      if (this.models.Any()) this.TargetVariable = this.models.First().TargetVariable;
[5662]60    }
61
62    public override IDeepCloneable Clone(Cloner cloner) {
63      return new ClassificationEnsembleModel(this, cloner);
64    }
65
[6520]66    public void Add(IClassificationModel model) {
[13941]67      if (string.IsNullOrEmpty(TargetVariable)) TargetVariable = model.TargetVariable;
[6520]68      models.Add(model);
69    }
[6613]70    public void Remove(IClassificationModel model) {
71      models.Remove(model);
[13941]72      if (!models.Any()) TargetVariable = string.Empty;
[6613]73    }
[5662]74
[12509]75    public IEnumerable<IEnumerable<double>> GetEstimatedClassValueVectors(IDataset dataset, IEnumerable<int> rows) {
[5662]76      var estimatedValuesEnumerators = (from model in models
77                                        select model.GetEstimatedClassValues(dataset, rows).GetEnumerator())
78                                       .ToList();
79
80      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
81        yield return from enumerator in estimatedValuesEnumerators
82                     select enumerator.Current;
83      }
84    }
85
86
[13941]87    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
[5662]88      foreach (var estimatedValuesVector in GetEstimatedClassValueVectors(dataset, rows)) {
89        // return the class which is most often occuring
90        yield return
91          estimatedValuesVector
92          .GroupBy(x => x)
93          .OrderBy(g => -g.Count())
94          .Select(g => g.Key)
95          .First();
96      }
97    }
98
[13941]99    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
[8528]100      return new ClassificationEnsembleSolution(models, new ClassificationEnsembleProblemData(problemData));
[6604]101    }
[13941]102
103
[5662]104  }
105}
Note: See TracBrowser for help on using the repository browser.