Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationModelComparison/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionComparisonView.cs @ 13089

Last change on this file since 13089 was 13089, checked in by gkronber, 9 years ago

#1998

  • deleted obsolete version of OneR algorithm (also does perform worse than mkommend's implementation in my tests)
  • reused the ConstantRegressionModel as ConstantClassificationModel (OK?)
  • fixed a few strings here and there
File size: 6.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 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.Collections.Generic;
24using System.Linq;
25using System.Windows.Forms;
26using HeuristicLab.Algorithms.DataAnalysis;
27using HeuristicLab.MainForm;
28using HeuristicLab.Problems.DataAnalysis.OnlineCalculators;
29using HeuristicLab.Random;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views.Classification {
32  [View("Solution Comparions")]
33  [Content(typeof(IClassificationSolution))]
34  public partial class ClassificationSolutionComparisonView : DataAnalysisSolutionEvaluationView {
35    private List<IClassificationSolution> solutions;
36
37    public ClassificationSolutionComparisonView() {
38      InitializeComponent();
39    }
40
41    public new IClassificationSolution Content {
42      get { return (IClassificationSolution)base.Content; }
43      set { base.Content = value; }
44    }
45
46    protected override void RegisterContentEvents() {
47      base.RegisterContentEvents();
48      Content.ModelChanged += new EventHandler(Content_ModelChanged);
49      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
50    }
51    protected override void DeregisterContentEvents() {
52      base.DeregisterContentEvents();
53      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
54      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
55    }
56
57    protected virtual void Content_ModelChanged(object sender, EventArgs e) {
58      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ModelChanged, sender, e);
59      else UpdateDataGridView();
60    }
61    protected virtual void Content_ProblemDataChanged(object sender, EventArgs e) {
62      if (InvokeRequired) Invoke((Action<object, EventArgs>)Content_ProblemDataChanged, sender, e);
63      else {
64        UpdateDataGridView();
65      }
66    }
67    protected override void OnContentChanged() {
68      base.OnContentChanged();
69      UpdateDataGridView();
70    }
71
72    private void UpdateDataGridView() {
73      if (InvokeRequired) {
74        Invoke((Action)UpdateDataGridView);
75      } else {
76        if (Content == null) {
77          dataGridView.Rows.Clear();
78          dataGridView.Columns.Clear();
79          solutions.Clear();
80        } else {
81
82          IClassificationProblemData problemData = Content.ProblemData;
83          var dataset = problemData.Dataset;
84          solutions = new List<IClassificationSolution>() { Content };
85          solutions.AddRange(GenerateClassificationSolutions(problemData));
86
87          dataGridView.ColumnCount = 4;
88          dataGridView.RowCount = solutions.Count();
89          dataGridView.Columns[0].HeaderText = "Accuracy (training)";
90          dataGridView.Columns[1].HeaderText = "Accuracy (test)";
91          dataGridView.Columns[2].HeaderText = "Matthews Correlation Coefficient (training)";
92          dataGridView.Columns[3].HeaderText = "Matthews Correlation Coefficient (test)";
93          if (problemData.Classes == 2) {
94            dataGridView.ColumnCount = 6;
95            dataGridView.Columns[4].HeaderText = "F1 Score (training)";
96            dataGridView.Columns[5].HeaderText = "F1 Score (test)";
97          }
98
99          for (int row = 0; row < solutions.Count; row++) {
100            var solution = solutions[row];
101
102            dataGridView.Rows[row].HeaderCell.Value = solution.Name;
103            dataGridView[0, row].Value = solution.TrainingAccuracy;
104            dataGridView[1, row].Value = solution.TestAccuracy;
105
106            var trainingIndizes = problemData.TrainingIndices;
107            var originalTrainingValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, trainingIndizes);
108            var estimatedTrainingValues = solution.Model.GetEstimatedClassValues(dataset, trainingIndizes);
109
110            var testIndices = problemData.TestIndices;
111            var originalTestValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, testIndices);
112            var estimatedTestValues = solution.Model.GetEstimatedClassValues(dataset, testIndices);
113
114            OnlineCalculatorError errorState;
115            dataGridView[2, row].Value = MatthewsCorrelationCoefficientCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
116            dataGridView[3, row].Value = MatthewsCorrelationCoefficientCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
117            if (problemData.Classes == 2) {
118              dataGridView[4, row].Value = FOneScoreCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
119              dataGridView[5, row].Value = FOneScoreCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
120            }
121          }
122
123          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
124          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
125        }
126      }
127    }
128
129    private IEnumerable<IClassificationSolution> GenerateClassificationSolutions(IClassificationProblemData problemData) {
130      var newSolutions = new List<IClassificationSolution>();
131      var zeroR = ZeroR.CreateZeroRSolution(problemData);
132      zeroR.Name = "ZeroR Classification Solution";
133      newSolutions.Add(zeroR);
134      var oneR = OneRTest.CreateOneRSolution(problemData);
135      oneR.Name = "OneR Classification Solution";
136      newSolutions.Add(oneR);
137      try {
138        var lda = LinearDiscriminantAnalysis.CreateLinearDiscriminantAnalysisSolution(problemData);
139        lda.Name = "Linear Discriminant Analysis Solution";
140        newSolutions.Add(lda);
141      }
142      catch (NotSupportedException) { }
143      catch (ArgumentException) { }
144      return newSolutions;
145    }
146
147    private void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e) {
148      var hittestinfo = dataGridView.HitTest(e.X, e.Y);
149      if (hittestinfo.Type != DataGridViewHitTestType.RowHeader) { return; }
150      if (hittestinfo.RowIndex > solutions.Count) { return; }
151
152      MainFormManager.MainForm.ShowContent(solutions[hittestinfo.RowIndex]);
153    }
154  }
155}
Note: See TracBrowser for help on using the repository browser.