1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2015 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 HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
32 | [StorableClass]
|
---|
33 | [Item("Gradient boosted tree model", "")]
|
---|
34 | // this is essentially a collection of weighted regression models
|
---|
35 | public sealed class GradientBoostedTreesModel : NamedItem, IRegressionModel {
|
---|
36 | [Storable]
|
---|
37 | private readonly IList<IRegressionModel> models;
|
---|
38 | public IEnumerable<IRegressionModel> Models { get { return models; } }
|
---|
39 |
|
---|
40 | [Storable]
|
---|
41 | private readonly IList<double> weights;
|
---|
42 | public IEnumerable<double> Weights { get { return weights; } }
|
---|
43 |
|
---|
44 | [StorableConstructor]
|
---|
45 | private GradientBoostedTreesModel(bool deserializing) : base(deserializing) { }
|
---|
46 | private GradientBoostedTreesModel(GradientBoostedTreesModel original, Cloner cloner)
|
---|
47 | : base(original, cloner) {
|
---|
48 | this.weights = new List<double>(original.weights);
|
---|
49 | this.models = new List<IRegressionModel>(original.models.Select(m => cloner.Clone(m)));
|
---|
50 | }
|
---|
51 | public GradientBoostedTreesModel(IEnumerable<IRegressionModel> models, IEnumerable<double> weights)
|
---|
52 | : base("Gradient boosted tree model", string.Empty) {
|
---|
53 | this.models = new List<IRegressionModel>(models);
|
---|
54 | this.weights = new List<double>(weights);
|
---|
55 |
|
---|
56 | if (this.models.Count != this.weights.Count) throw new ArgumentException();
|
---|
57 | }
|
---|
58 |
|
---|
59 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
60 | return new GradientBoostedTreesModel(this, cloner);
|
---|
61 | }
|
---|
62 |
|
---|
63 | public IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
64 | // allocate target array go over all models and add up weighted estimation for each row
|
---|
65 | var res = new double[rows.Count()];
|
---|
66 | if (res.Length == 0) return res; // return immediately if rows is empty. This prevents multiple iteration over lazy rows enumerable.
|
---|
67 | // (which essentially looks up indexes in a dictionary)
|
---|
68 | for (int i = 0; i < models.Count; i++) {
|
---|
69 | var w = weights[i];
|
---|
70 | var m = models[i];
|
---|
71 | int r = 0;
|
---|
72 | foreach (var est in m.GetEstimatedValues(dataset, rows)) {
|
---|
73 | res[r++] += w * est;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | return res;
|
---|
77 | }
|
---|
78 |
|
---|
79 | public IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
|
---|
80 | return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
|
---|
81 | }
|
---|
82 | }
|
---|
83 | }
|
---|