Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis.DecisionTrees/3.4/LeafTypes/RegularizedLeaf.cs @ 17080

Last change on this file since 17080 was 17080, checked in by gkronber, 5 years ago

#2847: more changes for renaming the M5 plugin

File size: 2.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.Linq;
24using System.Threading;
25using HeuristicLab.Algorithms.DataAnalysis.Glmnet;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Problems.DataAnalysis;
29using HEAL.Attic;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableType("0AED959D-78C3-4927-BDCF-473D0AEE32AA")]
33  [Item("M5regLeaf", "A leaf type that uses regularized linear models as leaf models.")]
34  public sealed class M5regLeaf : LeafBase {
35    #region Constructors & Cloning
36    [StorableConstructor]
37    private M5regLeaf(StorableConstructorFlag _) : base(_) { }
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 false; }
48    }
49
50    public override IRegressionModel Build(IRegressionProblemData pd, IRandom random, CancellationToken cancellationToken, out int numberOfParameters) {
51      if (pd.Dataset.Rows < MinLeafSize(pd)) throw new ArgumentException("The number of training instances is too small to create a linear model");
52      numberOfParameters = pd.AllowedInputVariables.Count() + 1;
53
54      double x1, x2;
55      var coeffs = ElasticNetLinearRegression.CalculateModelCoefficients(pd, 1, 0.2, out x1, out x2);
56      numberOfParameters = coeffs.Length;
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}
Note: See TracBrowser for help on using the repository browser.