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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using System.Threading;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 | using HeuristicLab.Problems.DataAnalysis;
|
---|
32 |
|
---|
33 | namespace 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 true; }
|
---|
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 cvRmse;
|
---|
74 | double rmse;
|
---|
75 | var model = PreconstructedLinearModel.CreateConfidenceLinearModel(pd2, out rmse, out cvRmse);
|
---|
76 | if (cvRmse > bestCvrmse) continue;
|
---|
77 | bestModel = new ComponentReducedLinearModel(pd2.TargetVariable, model, pca);
|
---|
78 | noParameters = i + 1;
|
---|
79 | bestCvrmse = cvRmse;
|
---|
80 | }
|
---|
81 | return bestModel;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public override int MinLeafSize(IRegressionProblemData pd) {
|
---|
85 | return NoComponents + 2;
|
---|
86 | }
|
---|
87 | #endregion
|
---|
88 | }
|
---|
89 | } |
---|