1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
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 |
|
---|
23 | using System;
|
---|
24 | using System.Collections.Generic;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HEAL.Attic;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
32 | [StorableType("4EC1B359-D145-434C-A373-3EDD764D2D63")]
|
---|
33 | [Item("Gradient boosted trees model", "")]
|
---|
34 | // this is essentially a collection of weighted regression models
|
---|
35 | public sealed class GradientBoostedTreesModel : RegressionModel, IGradientBoostedTreesModel {
|
---|
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 | }
|
---|
42 | get { return models; }
|
---|
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 | }
|
---|
50 | get { return weights; }
|
---|
51 | }
|
---|
52 |
|
---|
53 | public override IEnumerable<string> VariablesUsedForPrediction {
|
---|
54 | get { return models.SelectMany(x => x.VariablesUsedForPrediction).Distinct().OrderBy(x => x); }
|
---|
55 | }
|
---|
56 |
|
---|
57 | private readonly IList<IRegressionModel> models;
|
---|
58 | public IEnumerable<IRegressionModel> Models { get { return models; } }
|
---|
59 |
|
---|
60 | private readonly IList<double> weights;
|
---|
61 | public IEnumerable<double> Weights { get { return weights; } }
|
---|
62 |
|
---|
63 | [StorableConstructor]
|
---|
64 | private GradientBoostedTreesModel(StorableConstructorFlag _) : base(_) {
|
---|
65 | models = new List<IRegressionModel>();
|
---|
66 | weights = new List<double>();
|
---|
67 | }
|
---|
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 | }
|
---|
73 |
|
---|
74 | internal GradientBoostedTreesModel(IEnumerable<IRegressionModel> models, IEnumerable<double> weights)
|
---|
75 | : base(string.Empty, "Gradient boosted tree model", string.Empty) {
|
---|
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 |
|
---|
86 | public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
87 | // allocate target array go over all models and add up weighted estimation for each row
|
---|
88 | if (!rows.Any()) return Enumerable.Empty<double>(); // return immediately if rows is empty. This prevents multiple iteration over lazy rows enumerable.
|
---|
89 | // (which essentially looks up indexes in a dictionary)
|
---|
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 | }
|
---|
98 | }
|
---|
99 | return res;
|
---|
100 | }
|
---|
101 |
|
---|
102 | public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
103 | return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
|
---|
104 | }
|
---|
105 |
|
---|
106 | }
|
---|
107 | }
|
---|