Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 15470 was 15430, checked in by bwerth, 7 years ago

#2847 first implementation of M5'-regression

File size: 5.5 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  public class M5TreeModel : RegressionModel, IM5MetaModel {
35    private const string NoCurrentLeafesResultName = "Number of current Leafs";
36    #region Properties
37    [Storable]
38    internal M5NodeModel Root { get; private set; }
39    //[Storable]
40    //private M5Parameters M5Params { get; set; }
41    #endregion
42
43    #region HLConstructors & Cloning
44    [StorableConstructor]
45    protected M5TreeModel(bool deserializing) : base(deserializing) { }
46    protected M5TreeModel(M5TreeModel original, Cloner cloner) : base(original, cloner) {
47      Root = cloner.Clone(original.Root);
48    }
49    protected M5TreeModel(string targetVariable) : base(targetVariable) { }
50    public override IDeepCloneable Clone(Cloner cloner) {
51      return new M5TreeModel(this, cloner);
52    }
53    #endregion
54
55    internal static M5TreeModel CreateTreeModel(string targetAttr, M5CreationParameters m5CreationParams) {
56      return m5CreationParams.LeafType is ILeafType<IConfidenceRegressionModel> ? new ConfidenceM5TreeModel(targetAttr) : new M5TreeModel(targetAttr);
57    }
58
59    #region RegressionModel
60    public override IEnumerable<string> VariablesUsedForPrediction {
61      get { return Root.VariablesUsedForPrediction ?? new List<string>(); }
62    }
63    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
64      if (Root == null) throw new NotSupportedException("The classifier has not been built yet");
65      return Root.GetEstimatedValues(dataset, rows);
66    }
67    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
68      return new RegressionSolution(this, problemData);
69    }
70    #endregion
71
72    #region IM5Component
73    void IM5MetaModel.BuildClassifier(IReadOnlyList<int> trainingRows, IReadOnlyList<int> holdoutRows, M5CreationParameters m5CreationParams, CancellationToken cancellation) {
74      Root = null;
75      var globalStdDev = m5CreationParams.Data.GetDoubleValues(m5CreationParams.TargetVariable, trainingRows).StandardDeviationPop();
76      Root = M5NodeModel.CreateNode(m5CreationParams.TargetVariable, m5CreationParams);
77      Root.Split(trainingRows, m5CreationParams, globalStdDev);
78      InitializeLeafCounter(m5CreationParams);
79      if (!(m5CreationParams.Pruningtype is NoPruning)) Root.Prune(trainingRows, holdoutRows, m5CreationParams, cancellation, globalStdDev);
80      Root.InstallModels(trainingRows.Union(holdoutRows).ToArray(), m5CreationParams.Random, m5CreationParams.Data, m5CreationParams.LeafType, cancellation);
81    }
82
83    void IM5MetaModel.UpdateModel(IReadOnlyList<int> rows, M5UpdateParameters m5UpdateParameters, CancellationToken cancellation) {
84      Root.InstallModels(rows, m5UpdateParameters.Random, m5UpdateParameters.Data, m5UpdateParameters.LeafType, cancellation);
85    }
86    #endregion
87
88    #region Helpers
89    private void InitializeLeafCounter(M5CreationParameters m5CreationParams) {
90      if (!m5CreationParams.Results.ContainsKey(NoCurrentLeafesResultName))
91        m5CreationParams.Results.Add(new Result(NoCurrentLeafesResultName, new IntValue(Root.EnumerateNodes().Count(x => x.IsLeaf))));
92      else ((IntValue) m5CreationParams.Results[NoCurrentLeafesResultName].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 classifier 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.