[15430] | 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")]
|
---|
[15830] | 36 | public class ComponentReductionLinearLeaf : LeafBase {
|
---|
[15430] | 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]
|
---|
[15830] | 47 | protected ComponentReductionLinearLeaf(bool deserializing) : base(deserializing) { }
|
---|
| 48 | protected ComponentReductionLinearLeaf(ComponentReductionLinearLeaf original, Cloner cloner) : base(original, cloner) { }
|
---|
[15430] | 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
|
---|
[15830] | 58 | public override bool ProvidesConfidence {
|
---|
[15967] | 59 | get { return false; }
|
---|
[15614] | 60 | }
|
---|
[15830] | 61 | public override IRegressionModel Build(IRegressionProblemData pd, IRandom random,
|
---|
[15614] | 62 | CancellationToken cancellationToken, out int noParameters) {
|
---|
[15470] | 63 | var pca = PrincipleComponentTransformation.CreateProjection(pd.Dataset, pd.TrainingIndices, pd.AllowedInputVariables, true);
|
---|
| 64 | var pcdata = pca.TransformProblemData(pd);
|
---|
[15430] | 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++) {
|
---|
[15614] | 69 | var pd2 = (IRegressionProblemData)pcdata.Clone();
|
---|
[15470] | 70 | var inputs = new HashSet<string>(pca.ComponentNames.Take(i));
|
---|
[15430] | 71 | foreach (var v in pd2.InputVariables.CheckedItems.ToArray())
|
---|
| 72 | pd2.InputVariables.SetItemCheckedState(v.Value, inputs.Contains(v.Value.Value));
|
---|
| 73 | double rmse;
|
---|
[15967] | 74 | var model = PreconstructedLinearModel.CreateLinearModel(pd2, out rmse);
|
---|
| 75 | if (rmse > bestCvrmse) continue;
|
---|
[15430] | 76 | bestModel = new ComponentReducedLinearModel(pd2.TargetVariable, model, pca);
|
---|
| 77 | noParameters = i + 1;
|
---|
[15967] | 78 | bestCvrmse = rmse;
|
---|
[15430] | 79 | }
|
---|
| 80 | return bestModel;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[15830] | 83 | public override int MinLeafSize(IRegressionProblemData pd) {
|
---|
[15430] | 84 | return NoComponents + 2;
|
---|
| 85 | }
|
---|
| 86 | #endregion
|
---|
| 87 | }
|
---|
| 88 | } |
---|