Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis.Views/3.4/RandomForestModelEvaluationView.cs @ 15199

Last change on this file since 15199 was 15134, checked in by gkronber, 7 years ago

#2690: merged r14839,r15103:15106,r15123,r15124 from trunk to stable

File size: 4.1 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
21using System;
22using System.Drawing;
23using HeuristicLab.Common;
24using HeuristicLab.MainForm;
25using HeuristicLab.Problems.DataAnalysis;
26using HeuristicLab.Problems.DataAnalysis.Symbolic;
27using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
28using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression;
29using HeuristicLab.Problems.DataAnalysis.Views;
30
31namespace HeuristicLab.Algorithms.DataAnalysis.Views {
32  [View("RF Model Evaluation")]
33  [Content(typeof(IRandomForestRegressionSolution), false)]
34  [Content(typeof(IRandomForestClassificationSolution), false)]
35  public partial class RandomForestModelEvaluationView : DataAnalysisSolutionEvaluationView {
36
37    protected override void SetEnabledStateOfControls() {
38      base.SetEnabledStateOfControls();
39      listBox.Enabled = Content != null;
40      viewHost.Enabled = Content != null;
41    }
42
43    public RandomForestModelEvaluationView()
44      : base() {
45      InitializeComponent();
46    }
47
48    protected override void OnContentChanged() {
49      base.OnContentChanged();
50      if (Content == null) {
51        viewHost.Content = null;
52        listBox.Items.Clear();
53      } else {
54        viewHost.Content = null;
55        listBox.Items.Clear();
56        var classSol = Content as IRandomForestClassificationSolution;
57        var regSol = Content as IRandomForestRegressionSolution;
58        var numTrees = classSol != null ? classSol.NumberOfTrees : regSol != null ? regSol.NumberOfTrees : 0;
59        for (int i = 0; i < numTrees; i++) {
60          listBox.Items.Add(i + 1);
61        }
62      }
63    }
64
65    private void listBox_SelectedIndexChanged(object sender, System.EventArgs e) {
66      if (listBox.SelectedItem == null) viewHost.Content = null;
67      else {
68        var idx = (int)listBox.SelectedItem;
69        viewHost.Content = CreateModel(idx);
70      }
71    }
72
73    private void listBox_DoubleClick(object sender, System.EventArgs e) {
74      var selectedItem = listBox.SelectedItem;
75      if (selectedItem == null) return;
76      var idx = (int)listBox.SelectedItem;
77      MainFormManager.MainForm.ShowContent(CreateModel(idx));
78    }
79
80    private IContent CreateModel(int idx) {
81      idx -= 1;
82      var rfModel = Content.Model as RandomForestModel;
83      if (rfModel == null) return null;
84      var regProblemData = Content.ProblemData as IRegressionProblemData;
85      var classProblemData = Content.ProblemData as IClassificationProblemData;
86      if (idx < 0 || idx >= rfModel.NumberOfTrees)
87        return null;
88      if (regProblemData != null) {
89        var syModel = new SymbolicRegressionModel(regProblemData.TargetVariable, rfModel.ExtractTree(idx),
90          new SymbolicDataAnalysisExpressionTreeLinearInterpreter());
91        return syModel.CreateRegressionSolution(regProblemData);
92      } else if (classProblemData != null) {
93        var syModel = new SymbolicDiscriminantFunctionClassificationModel(classProblemData.TargetVariable, rfModel.ExtractTree(idx),
94          new SymbolicDataAnalysisExpressionTreeLinearInterpreter(), new NormalDistributionCutPointsThresholdCalculator());
95        syModel.RecalculateModelParameters(classProblemData, classProblemData.TrainingIndices);
96        return syModel.CreateClassificationSolution(classProblemData);
97      } else throw new InvalidProgramException();
98    }
99  }
100}
Note: See TracBrowser for help on using the repository browser.