Free cookie consent management tool by TermsFeed Policy Generator

source: branches/MemPRAlgorithm/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/RandomForestModelView.cs @ 14535

Last change on this file since 14535 was 14345, checked in by gkronber, 8 years ago

#2690: implemented methods to generate symbolic expression tree solutions for decision tree models (random forest and gradient boosted) as well as views which make it possible to inspect each of the individual trees in a GBT and RF solution

File size: 3.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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.Drawing;
23using HeuristicLab.MainForm;
24using HeuristicLab.Problems.DataAnalysis;
25using HeuristicLab.Problems.DataAnalysis.Symbolic;
26using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
28using HeuristicLab.Problems.DataAnalysis.Views;
29
30namespace HeuristicLab.Algorithms.DataAnalysis.Views {
31  [View("Random forest model")]
32  [Content(typeof(IRandomForestRegressionSolution), false)]
33  [Content(typeof(IRandomForestClassificationSolution), false)]
34  public partial class RandomForestModelView : DataAnalysisSolutionEvaluationView {
35    public override Image ViewImage {
36      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
37    }
38
39    protected override void SetEnabledStateOfControls() {
40      base.SetEnabledStateOfControls();
41      listBox.Enabled = Content != null;
42      viewHost.Enabled = Content != null;
43    }
44
45    public RandomForestModelView()
46      : base() {
47      InitializeComponent();
48    }
49
50    protected override void OnContentChanged() {
51      base.OnContentChanged();
52      if (Content == null) {
53        viewHost.Content = null;
54        listBox.Items.Clear();
55      } else {
56        viewHost.Content = null;
57        listBox.Items.Clear();
58        var classSol = Content as IRandomForestClassificationSolution;
59        var regSol = Content as IRandomForestRegressionSolution;
60        var numTrees = classSol != null ? classSol.NumberOfTrees : regSol != null ? regSol.NumberOfTrees : 0;
61        for (int i = 0; i < numTrees; i++) {
62          listBox.Items.Add(i + 1);
63        }
64      }
65    }
66
67    private void listBox_SelectedIndexChanged(object sender, System.EventArgs e) {
68      if (listBox.SelectedItem == null) viewHost.Content = null;
69      else {
70        var idx = (int)listBox.SelectedItem;
71        idx -= 1;
72        var rfModel = Content.Model as RandomForestModel;
73        var regProblemData = Content.ProblemData as IRegressionProblemData;
74        var classProblemData = Content.ProblemData as IClassificationProblemData;
75        if (rfModel != null) {
76          if (idx < 0 || idx >= rfModel.NumberOfTrees) return;
77          if (regProblemData != null) {
78            var syModel = new SymbolicRegressionModel(regProblemData.TargetVariable, rfModel.ExtractTree(idx),
79              new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
80            viewHost.Content = syModel.CreateRegressionSolution(regProblemData);
81          } else if (classProblemData != null) {
82            var syModel = new SymbolicDiscriminantFunctionClassificationModel(classProblemData.TargetVariable, rfModel.ExtractTree(idx),
83              new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), new NormalDistributionCutPointsThresholdCalculator());
84            syModel.RecalculateModelParameters(classProblemData, classProblemData.TrainingIndices);
85            viewHost.Content = syModel.CreateClassificationSolution(classProblemData);
86          }
87        }
88      }
89    }
90  }
91}
Note: See TracBrowser for help on using the repository browser.