Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 17041 was 17030, checked in by mkommend, 5 years ago

#2883: Merged all changesets into trunk.

File size: 4.3 KB
RevLine 
[12590]1#region License Information
2/* HeuristicLab
[16565]3 * Copyright (C) 2002-2019 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    // BackwardsCompatibility3.4 for allowing deserialization & serialization of old models
37    #region Backwards compatible code, remove with 3.5
38
39    [Storable(Name = "models")]
40    private IList<IRegressionModel> __persistedModels {
41      set {
42        this.models.Clear();
43        foreach (var m in value) this.models.Add(m);
44      }
[17030]45      get { return models; }
[12868]46    }
47    [Storable(Name = "weights")]
48    private IList<double> __persistedWeights {
49      set {
50        this.weights.Clear();
51        foreach (var w in value) this.weights.Add(w);
52      }
[17030]53      get { return weights; }
[12868]54    }
55    #endregion
56
[13941]57    public override IEnumerable<string> VariablesUsedForPrediction {
[13921]58      get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
59    }
60
[12332]61    private readonly IList<IRegressionModel> models;
[12372]62    public IEnumerable<IRegressionModel> Models { get { return models; } }
63
[12332]64    private readonly IList<double> weights;
[12372]65    public IEnumerable<double> Weights { get { return weights; } }
[12332]66
67    [StorableConstructor]
[16565]68    private GradientBoostedTreesModel(StorableConstructorFlag _) : base(_) {
[12868]69      models = new List<IRegressionModel>();
70      weights = new List<double>();
71    }
[12332]72    private GradientBoostedTreesModel(GradientBoostedTreesModel original, Cloner cloner)
73      : base(original, cloner) {
74      this.weights = new List<double>(original.weights);
75      this.models = new List<IRegressionModel>(original.models.Select(m => cloner.Clone(m)));
76    }
[17030]77
[13941]78    internal GradientBoostedTreesModel(IEnumerable<IRegressionModel> models, IEnumerable<double> weights)
79      : base(string.Empty, "Gradient boosted tree model", string.Empty) {
[12332]80      this.models = new List<IRegressionModel>(models);
81      this.weights = new List<double>(weights);
82
83      if (this.models.Count != this.weights.Count) throw new ArgumentException();
84    }
85
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new GradientBoostedTreesModel(this, cloner);
88    }
89
[13941]90    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
[12590]91      // allocate target array go over all models and add up weighted estimation for each row
[12660]92      if (!rows.Any()) return Enumerable.Empty<double>(); // return immediately if rows is empty. This prevents multiple iteration over lazy rows enumerable.
[12868]93      // (which essentially looks up indexes in a dictionary)
[12590]94      var res = new double[rows.Count()];
95      for (int i = 0; i < models.Count; i++) {
96        var w = weights[i];
97        var m = models[i];
98        int r = 0;
99        foreach (var est in m.GetEstimatedValues(dataset, rows)) {
100          res[r++] += w * est;
101        }
[12332]102      }
[12590]103      return res;
[12332]104    }
105
[13941]106    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
[12332]107      return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
108    }
[13921]109
[12332]110  }
111}
Note: See TracBrowser for help on using the repository browser.