Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis.DecisionTrees/3.4/MetaModels/RegressionNodeModel.cs @ 17591

Last change on this file since 17591 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 7.4 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;                                                 
26using HeuristicLab.Problems.DataAnalysis;
27using HEAL.Attic;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [StorableType("C20C7DF1-CE33-4CCD-88D3-E145CFE239AC")]
31  public class RegressionNodeModel : RegressionModel {
32    #region Properties
33    [Storable]
34    public double PruningStrength = double.NaN;
35    private IReadOnlyList<string> Variables {
36      get {
37        if (IsLeaf && Model == null) return new List<string>();
38        if (IsLeaf) return Model.VariablesUsedForPrediction.ToList();
39        var set = new HashSet<string> {SplitAttribute};
40        var vl = Left.Variables;
41        var vr = Right.Variables;
42        for (var i = 0; i < vl.Count; i++) set.Add(vl[i]);
43        for (var i = 0; i < vr.Count; i++) set.Add(vr[i]);
44        return set.ToList();
45      }
46    }
47    [Storable]
48    internal int NumSamples { get; private set; }
49    [Storable]
50    internal bool IsLeaf { get; private set; }
51    [Storable]
52    private IRegressionModel Model { get; set; }
53
54    [Storable]
55    public string SplitAttribute { get; private set; }
56    [Storable]
57    public double SplitValue { get; private set; }
58    [Storable]
59    public RegressionNodeModel Left { get; private set; }
60    [Storable]
61    public RegressionNodeModel Right { get; private set; }
62    [Storable]
63    public RegressionNodeModel Parent { get; private set; }
64    #endregion
65
66    #region HLConstructors
67    [StorableConstructor]
68    protected RegressionNodeModel(StorableConstructorFlag _) : base(_) { }
69    protected RegressionNodeModel(RegressionNodeModel original, Cloner cloner) : base(original, cloner) {
70      IsLeaf = original.IsLeaf;
71      Model = cloner.Clone(original.Model);
72      SplitValue = original.SplitValue;
73      SplitAttribute = original.SplitAttribute;
74      Left = cloner.Clone(original.Left);
75      Right = cloner.Clone(original.Right);
76      Parent = cloner.Clone(original.Parent);
77      NumSamples = original.NumSamples;
78    }
79    private RegressionNodeModel(string targetAttr) : base(targetAttr) {
80      IsLeaf = true;
81    }
82    private RegressionNodeModel(RegressionNodeModel parent) : this(parent.TargetVariable) {
83      Parent = parent;
84      IsLeaf = true;
85    }
86    public override IDeepCloneable Clone(Cloner cloner) {
87      return new RegressionNodeModel(this, cloner);
88    }
89    public static RegressionNodeModel CreateNode(string targetAttr, RegressionTreeParameters regressionTreeParams) {
90      return regressionTreeParams.LeafModel.ProvidesConfidence ? new ConfidenceRegressionNodeModel(targetAttr) : new RegressionNodeModel(targetAttr);
91    }
92    private static RegressionNodeModel CreateNode(RegressionNodeModel parent, RegressionTreeParameters regressionTreeParams) {
93      return regressionTreeParams.LeafModel.ProvidesConfidence ? new ConfidenceRegressionNodeModel(parent) : new RegressionNodeModel(parent);
94    }
95    #endregion
96
97    #region RegressionModel
98    public override IEnumerable<string> VariablesUsedForPrediction {
99      get { return Variables; }
100    }
101    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
102      if (!IsLeaf) return rows.Select(row => GetEstimatedValue(dataset, row));
103      if (Model == null) throw new NotSupportedException("The model has not been built correctly");
104      return Model.GetEstimatedValues(dataset, rows);
105    }
106    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
107      return new RegressionSolution(this, problemData);
108    }
109    #endregion
110
111    internal void Split(RegressionTreeParameters regressionTreeParams, string splitAttribute, double splitValue, int numSamples) {
112      NumSamples = numSamples;
113      SplitAttribute = splitAttribute;
114      SplitValue = splitValue;
115      Left = CreateNode(this, regressionTreeParams);
116      Right = CreateNode(this, regressionTreeParams);
117      IsLeaf = false;
118    }
119
120    internal void ToLeaf() {
121      IsLeaf = true;
122      Right = null;
123      Left = null;
124    }
125
126    internal void SetLeafModel(IRegressionModel model) {
127      Model = model;
128    }
129
130    internal IEnumerable<RegressionNodeModel> EnumerateNodes() {
131      var queue = new Queue<RegressionNodeModel>();
132      queue.Enqueue(this);
133      while (queue.Count != 0) {
134        var cur = queue.Dequeue();
135        yield return cur;
136        if (cur.Left == null && cur.Right == null) continue;
137        if (cur.Left != null) queue.Enqueue(cur.Left);
138        if (cur.Right != null) queue.Enqueue(cur.Right);
139      }
140    }
141
142    #region Helpers
143    private double GetEstimatedValue(IDataset dataset, int row) {
144      if (!IsLeaf) return (dataset.GetDoubleValue(SplitAttribute, row) <= SplitValue ? Left : Right).GetEstimatedValue(dataset, row);
145      if (Model == null) throw new NotSupportedException("The model has not been built correctly");
146      return Model.GetEstimatedValues(dataset, new[] {row}).First();
147    }
148    #endregion
149
150    [StorableType("1FF9E216-6AF1-4282-A7EF-3FA0C1DB29C8")]
151    private sealed class ConfidenceRegressionNodeModel : RegressionNodeModel, IConfidenceRegressionModel {
152      #region HLConstructors
153      [StorableConstructor]
154      private ConfidenceRegressionNodeModel(StorableConstructorFlag _) : base(_) { }
155      private ConfidenceRegressionNodeModel(ConfidenceRegressionNodeModel original, Cloner cloner) : base(original, cloner) { }
156      public ConfidenceRegressionNodeModel(string targetAttr) : base(targetAttr) { }
157      public ConfidenceRegressionNodeModel(RegressionNodeModel parent) : base(parent) { }
158      public override IDeepCloneable Clone(Cloner cloner) {
159        return new ConfidenceRegressionNodeModel(this, cloner);
160      }
161      #endregion
162
163      public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) {
164        return IsLeaf ? ((IConfidenceRegressionModel)Model).GetEstimatedVariances(dataset, rows) : rows.Select(row => GetEstimatedVariance(dataset, row));
165      }
166
167      private double GetEstimatedVariance(IDataset dataset, int row) {
168        return !IsLeaf ? ((IConfidenceRegressionModel)(dataset.GetDoubleValue(SplitAttribute, row) <= SplitValue ? Left : Right)).GetEstimatedVariances(dataset, row.ToEnumerable()).Single() : ((IConfidenceRegressionModel)Model).GetEstimatedVariances(dataset, new[] {row}).First();
169      }
170
171      public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
172        return new ConfidenceRegressionSolution(this, problemData);
173      }
174    }
175  }
176}
Note: See TracBrowser for help on using the repository browser.