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