Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionEnsembleModel.cs @ 13042

Last change on this file since 13042 was 12509, checked in by mkommend, 9 years ago

#2276: Reintegrated branch for dataset refactoring.

File size: 4.1 KB
RevLine 
[5662]1#region License Information
2/* HeuristicLab
[12012]3 * Copyright (C) 2002-2015 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 regression solutions that contain an ensemble of multiple regression models
31  /// </summary>
32  [StorableClass]
33  [Item("RegressionEnsembleModel", "A regression model that contains an ensemble of multiple regression models")]
34  public class RegressionEnsembleModel : NamedItem, IRegressionEnsembleModel {
35
36    private List<IRegressionModel> models;
37    public IEnumerable<IRegressionModel> Models {
38      get { return new List<IRegressionModel>(models); }
39    }
[6603]40
41    [Storable(Name = "Models")]
42    private IEnumerable<IRegressionModel> StorableModels {
43      get { return models; }
44      set { models = value.ToList(); }
45    }
46
47    #region backwards compatiblity 3.3.5
48    [Storable(Name = "models", AllowOneWay = true)]
49    private List<IRegressionModel> OldStorableModels {
50      set { models = value; }
51    }
52    #endregion
53
[5662]54    [StorableConstructor]
55    protected RegressionEnsembleModel(bool deserializing) : base(deserializing) { }
56    protected RegressionEnsembleModel(RegressionEnsembleModel original, Cloner cloner)
57      : base(original, cloner) {
58      this.models = original.Models.Select(m => cloner.Clone(m)).ToList();
59    }
[6666]60
61    public RegressionEnsembleModel() : this(Enumerable.Empty<IRegressionModel>()) { }
[5662]62    public RegressionEnsembleModel(IEnumerable<IRegressionModel> models)
63      : base() {
64      this.name = ItemName;
65      this.description = ItemDescription;
66      this.models = new List<IRegressionModel>(models);
67    }
68
69    public override IDeepCloneable Clone(Cloner cloner) {
70      return new RegressionEnsembleModel(this, cloner);
71    }
72
73    #region IRegressionEnsembleModel Members
74
[6520]75    public void Add(IRegressionModel model) {
76      models.Add(model);
77    }
[6612]78    public void Remove(IRegressionModel model) {
79      models.Remove(model);
80    }
[6520]81
[12509]82    public IEnumerable<IEnumerable<double>> GetEstimatedValueVectors(IDataset dataset, IEnumerable<int> rows) {
[5662]83      var estimatedValuesEnumerators = (from model in models
84                                        select model.GetEstimatedValues(dataset, rows).GetEnumerator())
85                                       .ToList();
86
87      while (estimatedValuesEnumerators.All(en => en.MoveNext())) {
88        yield return from enumerator in estimatedValuesEnumerators
89                     select enumerator.Current;
90      }
91    }
92
93    #endregion
94
95    #region IRegressionModel Members
96
[12509]97    public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
[5662]98      foreach (var estimatedValuesVector in GetEstimatedValueVectors(dataset, rows)) {
99        yield return estimatedValuesVector.Average();
100      }
101    }
102
[6603]103    public RegressionEnsembleSolution CreateRegressionSolution(IRegressionProblemData problemData) {
[8528]104      return new RegressionEnsembleSolution(this.Models, new RegressionEnsembleProblemData(problemData));
[6603]105    }
106    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
107      return CreateRegressionSolution(problemData);
108    }
109
[5662]110    #endregion
111  }
112}
Note: See TracBrowser for help on using the repository browser.