Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesModel.cs @ 17352

Last change on this file since 17352 was 17180, checked in by swagner, 5 years ago

#2875: Removed years in copyrights

File size: 4.1 KB
RevLine 
[12590]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12590]4 * and the BEACON Center for the Study of Evolution in Action.
5 *
6 * This file is part of HeuristicLab.
7 *
8 * HeuristicLab is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation, either version 3 of the License, or
11 * (at your option) any later version.
12 *
13 * HeuristicLab is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
20 */
21#endregion
22
23using System;
[12332]24using System.Collections.Generic;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
[16565]28using HEAL.Attic;
[12332]29using HeuristicLab.Problems.DataAnalysis;
30
[12590]31namespace HeuristicLab.Algorithms.DataAnalysis {
[16565]32  [StorableType("4EC1B359-D145-434C-A373-3EDD764D2D63")]
[15105]33  [Item("Gradient boosted trees model", "")]
[12590]34  // this is essentially a collection of weighted regression models
[13941]35  public sealed class GradientBoostedTreesModel : RegressionModel, IGradientBoostedTreesModel {
[12868]36    [Storable(Name = "models")]
37    private IList<IRegressionModel> __persistedModels {
38      set {
39        this.models.Clear();
40        foreach (var m in value) this.models.Add(m);
41      }
[17030]42      get { return models; }
[12868]43    }
44    [Storable(Name = "weights")]
45    private IList<double> __persistedWeights {
46      set {
47        this.weights.Clear();
48        foreach (var w in value) this.weights.Add(w);
49      }
[17030]50      get { return weights; }
[12868]51    }
52
[13941]53    public override IEnumerable<string> VariablesUsedForPrediction {
[13921]54      get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
55    }
56
[12332]57    private readonly IList<IRegressionModel> models;
[12372]58    public IEnumerable<IRegressionModel> Models { get { return models; } }
59
[12332]60    private readonly IList<double> weights;
[12372]61    public IEnumerable<double> Weights { get { return weights; } }
[12332]62
63    [StorableConstructor]
[16565]64    private GradientBoostedTreesModel(StorableConstructorFlag _) : base(_) {
[12868]65      models = new List<IRegressionModel>();
66      weights = new List<double>();
67    }
[12332]68    private GradientBoostedTreesModel(GradientBoostedTreesModel original, Cloner cloner)
69      : base(original, cloner) {
70      this.weights = new List<double>(original.weights);
71      this.models = new List<IRegressionModel>(original.models.Select(m => cloner.Clone(m)));
72    }
[17030]73
[13941]74    internal GradientBoostedTreesModel(IEnumerable<IRegressionModel> models, IEnumerable<double> weights)
75      : base(string.Empty, "Gradient boosted tree model", string.Empty) {
[12332]76      this.models = new List<IRegressionModel>(models);
77      this.weights = new List<double>(weights);
78
79      if (this.models.Count != this.weights.Count) throw new ArgumentException();
80    }
81
82    public override IDeepCloneable Clone(Cloner cloner) {
83      return new GradientBoostedTreesModel(this, cloner);
84    }
85
[13941]86    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
[12590]87      // allocate target array go over all models and add up weighted estimation for each row
[12660]88      if (!rows.Any()) return Enumerable.Empty<double>(); // return immediately if rows is empty. This prevents multiple iteration over lazy rows enumerable.
[12868]89      // (which essentially looks up indexes in a dictionary)
[12590]90      var res = new double[rows.Count()];
91      for (int i = 0; i < models.Count; i++) {
92        var w = weights[i];
93        var m = models[i];
94        int r = 0;
95        foreach (var est in m.GetEstimatedValues(dataset, rows)) {
96          res[r++] += w * est;
97        }
[12332]98      }
[12590]99      return res;
[12332]100    }
101
[13941]102    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
[12332]103      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
104    }
[13921]105
[12332]106  }
107}
Note: See TracBrowser for help on using the repository browser.