[15830] | 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.Linq;
|
---|
| 24 | using System.Threading;
|
---|
| 25 | using HeuristicLab.Algorithms.DataAnalysis.Glmnet;
|
---|
| 26 | using HeuristicLab.Common;
|
---|
| 27 | using HeuristicLab.Core;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
[16847] | 29 | using HEAL.Attic;
|
---|
[15830] | 30 |
|
---|
| 31 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[16847] | 32 | [StorableType("0AED959D-78C3-4927-BDCF-473D0AEE32AA")]
|
---|
| 33 | [Item("M5regLeaf", "A leaf type that uses regularized linear models as leaf models.")]
|
---|
[15830] | 34 | public class M5regLeaf : LeafBase {
|
---|
| 35 | #region Constructors & Cloning
|
---|
| 36 | [StorableConstructor]
|
---|
[16847] | 37 | private M5regLeaf(StorableConstructorFlag _) : base(_) { }
|
---|
[15830] | 38 | private M5regLeaf(M5regLeaf original, Cloner cloner) : base(original, cloner) { }
|
---|
| 39 | public M5regLeaf() { }
|
---|
| 40 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 41 | return new M5regLeaf(this, cloner);
|
---|
| 42 | }
|
---|
| 43 | #endregion
|
---|
| 44 |
|
---|
| 45 | #region IModelType
|
---|
| 46 | public override bool ProvidesConfidence {
|
---|
| 47 | get { return true; }
|
---|
| 48 | }
|
---|
[16847] | 49 |
|
---|
| 50 | public override IRegressionModel Build(IRegressionProblemData pd, IRandom random, CancellationToken cancellationToken, out int numberOfParameters) {
|
---|
[15830] | 51 | if (pd.Dataset.Rows < MinLeafSize(pd)) throw new ArgumentException("The number of training instances is too small to create a linear model");
|
---|
[16847] | 52 | numberOfParameters = pd.AllowedInputVariables.Count() + 1;
|
---|
[15830] | 53 |
|
---|
| 54 | double x1, x2;
|
---|
| 55 | var coeffs = ElasticNetLinearRegression.CalculateModelCoefficients(pd, 1, 0.2, out x1, out x2);
|
---|
[16847] | 56 | numberOfParameters = coeffs.Length;
|
---|
[15830] | 57 | return ElasticNetLinearRegression.CreateSymbolicSolution(coeffs, pd).Model;
|
---|
| 58 | }
|
---|
| 59 | public override int MinLeafSize(IRegressionProblemData pd) {
|
---|
| 60 | return pd.AllowedInputVariables.Count() + 2;
|
---|
| 61 | }
|
---|
| 62 | #endregion
|
---|
| 63 | }
|
---|
| 64 | } |
---|