source: branches/M5Regression/HeuristicLab.Algorithms.DataAnalysis/3.4/M5Regression/LeafTypes/ComponentReductionLinearLeaf.cs @ 15967

Last change on this file since 15967 was 15967, checked in by bwerth, 12 months ago

#2847 added logistic dampening and some minor changes

File size: 3.7 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.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31using HeuristicLab.Problems.DataAnalysis;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  [StorableClass]
35  [Item("ComponentReductionLinearLeaf", "A leaf type that uses principle component analysis to create smaller linear models as leaf models")]
36  public class ComponentReductionLinearLeaf : LeafBase {
37    public const string NoComponentsParameterName = "NoComponents";
38    public IFixedValueParameter<IntValue> NoComponentsParameter {
39      get { return Parameters[NoComponentsParameterName] as IFixedValueParameter<IntValue>; }
40    }
41    public int NoComponents {
42      get { return NoComponentsParameter.Value.Value; }
43    }
44
45    #region Constructors & Cloning
46    [StorableConstructor]
47    protected ComponentReductionLinearLeaf(bool deserializing) : base(deserializing) { }
48    protected ComponentReductionLinearLeaf(ComponentReductionLinearLeaf original, Cloner cloner) : base(original, cloner) { }
49    public ComponentReductionLinearLeaf() {
50      Parameters.Add(new FixedValueParameter<IntValue>(NoComponentsParameterName, "The maximum number of principle components used", new IntValue(10)));
51    }
52    public override IDeepCloneable Clone(Cloner cloner) {
53      return new ComponentReductionLinearLeaf(this, cloner);
54    }
55    #endregion
56
57    #region IModelType
58    public override bool ProvidesConfidence {
59      get { return false; }
60    }
61    public override IRegressionModel Build(IRegressionProblemData pd, IRandom random,
62      CancellationToken cancellationToken, out int noParameters) {
63      var pca = PrincipleComponentTransformation.CreateProjection(pd.Dataset, pd.TrainingIndices, pd.AllowedInputVariables, true);
64      var pcdata = pca.TransformProblemData(pd);
65      ComponentReducedLinearModel bestModel = null;
66      var bestCvrmse = double.MaxValue;
67      noParameters = 1;
68      for (var i = 1; i <= Math.Min(NoComponents, pd.AllowedInputVariables.Count()); i++) {
69        var pd2 = (IRegressionProblemData)pcdata.Clone();
70        var inputs = new HashSet<string>(pca.ComponentNames.Take(i));
71        foreach (var v in pd2.InputVariables.CheckedItems.ToArray())
72          pd2.InputVariables.SetItemCheckedState(v.Value, inputs.Contains(v.Value.Value));
73        double rmse;
74        var model = PreconstructedLinearModel.CreateLinearModel(pd2, out rmse);
75        if (rmse > bestCvrmse) continue;
76        bestModel = new ComponentReducedLinearModel(pd2.TargetVariable, model, pca);
77        noParameters = i + 1;
78        bestCvrmse = rmse;
79      }
80      return bestModel;
81    }
82
83    public override int MinLeafSize(IRegressionProblemData pd) {
84      return NoComponents + 2;
85    }
86    #endregion
87  }
88}
Note: See TracBrowser for help on using the repository browser.