Free cookie consent management tool by TermsFeed Policy Generator

source: branches/M5Regression/HeuristicLab.Algorithms.DataAnalysis/3.4/M5Regression/MetaModels/M5TreeModel.cs @ 15614

Last change on this file since 15614 was 15614, checked in by bwerth, 6 years ago

#2847 made changes to M5 according to review comments

File size: 5.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2017 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 System.Threading;
26using HeuristicLab.Common;
27using HeuristicLab.Data;
28using HeuristicLab.Optimization;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.DataAnalysis {
33  [StorableClass]
34  internal class M5TreeModel : RegressionModel, IM5Model {
35    public const string NumCurrentLeafsResultName = "Number of current leafs";
36    #region Properties
37    [Storable]
38    internal M5NodeModel Root { get; private set; }
39    #endregion
40
41    #region HLConstructors & Cloning
42    [StorableConstructor]
43    protected M5TreeModel(bool deserializing) : base(deserializing) { }
44    protected M5TreeModel(M5TreeModel original, Cloner cloner) : base(original, cloner) {
45      Root = cloner.Clone(original.Root);
46    }
47    protected M5TreeModel(string targetVariable) : base(targetVariable) { }
48    public override IDeepCloneable Clone(Cloner cloner) {
49      return new M5TreeModel(this, cloner);
50    }
51    #endregion
52
53    internal static M5TreeModel CreateTreeModel(string targetAttr, M5Parameters m5Params) {
54      return m5Params.LeafModel.ProvidesConfidence ? new ConfidenceM5TreeModel(targetAttr) : new M5TreeModel(targetAttr);
55    }
56
57    #region RegressionModel
58    public override IEnumerable<string> VariablesUsedForPrediction {
59      get { return Root.VariablesUsedForPrediction ?? new List<string>(); }
60    }
61    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
62      if (Root == null) throw new NotSupportedException("The model has not been built yet");
63      return Root.GetEstimatedValues(dataset, rows);
64    }
65    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
66      return new RegressionSolution(this, problemData);
67    }
68    #endregion
69
70    #region IM5Model
71    public void Build(IReadOnlyList<int> trainingRows, IReadOnlyList<int> pruningRows, M5Parameters m5Params, CancellationToken cancellationToken) {
72      Root = M5NodeModel.CreateNode(m5Params.TargetVariable, m5Params);
73      Root.Split(trainingRows, m5Params);
74
75      InitializeLeafCounter(m5Params);
76
77      var buPruner = m5Params.Pruning as BottomUpPruningBase;
78      if (buPruner != null) buPruner.Prune(this, trainingRows, pruningRows, m5Params, cancellationToken);
79
80      Root.BuildLeafModels(trainingRows.Union(pruningRows).ToArray(), m5Params, cancellationToken);
81    }
82
83    public void Update(IReadOnlyList<int> rows, M5Parameters m5Parameters, CancellationToken cancellationToken) {
84      Root.BuildLeafModels(rows, m5Parameters, cancellationToken);
85    }
86    #endregion
87
88    #region Helpers
89    private void InitializeLeafCounter(M5Parameters m5Params) {
90      if (!m5Params.Results.ContainsKey(NumCurrentLeafsResultName))
91        m5Params.Results.Add(new Result(NumCurrentLeafsResultName, new IntValue(Root.EnumerateNodes().Count(x => x.IsLeaf))));
92      else ((IntValue)m5Params.Results[NumCurrentLeafsResultName].Value).Value = Root.EnumerateNodes().Count(x => x.IsLeaf);
93    }
94    #endregion
95
96    [StorableClass]
97    private class ConfidenceM5TreeModel : M5TreeModel, IConfidenceRegressionModel {
98      #region HLConstructors & Cloning
99      [StorableConstructor]
100      protected ConfidenceM5TreeModel(bool deserializing) : base(deserializing) { }
101      private ConfidenceM5TreeModel(ConfidenceM5TreeModel original, Cloner cloner) : base(original, cloner) { }
102      public ConfidenceM5TreeModel(string targetVariable) : base(targetVariable) { }
103      public override IDeepCloneable Clone(Cloner cloner) {
104        return new ConfidenceM5TreeModel(this, cloner);
105      }
106      #endregion
107
108      public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) {
109        if (Root == null) throw new NotSupportedException("The model has not been built yet");
110        return ((IConfidenceRegressionModel)Root).GetEstimatedVariances(dataset, rows);
111      }
112      public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
113        return new ConfidenceRegressionSolution(this, problemData);
114      }
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.