Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 9135 was 9135, checked in by sforsten, 11 years ago

#1998:

  • OneR handles missing values separately
  • adapted OneRClassificationModelView to show the class of missing values
  • with a double-click on the row header in ClassificationSolutionComparisonView the selected solution opens in a new view
  • put a try catch block around linear discriminant analysis solution (it is only shown, if it doesn't throw an exception)
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          Dataset 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 = "Training Accuracy";
90          dataGridView.Columns[1].HeaderText = "Test Accuracy";
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 Trainin";
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.TrainingIndices;
111            var originalTestValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, testIndices);
112            var estimatedTestValues = solution.Model.GetEstimatedClassValues(dataset, testIndices);
113
114            OnlineCalculatorError errorState;
115
116            dataGridView[2, row].Value = MatthewsCorrelationCoefficientCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
117            dataGridView[3, row].Value = MatthewsCorrelationCoefficientCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
118            if (problemData.Classes == 2) {
119              dataGridView[4, row].Value = FOneScoreCalculator.Calculate(originalTrainingValues, estimatedTrainingValues, out errorState);
120              dataGridView[5, row].Value = FOneScoreCalculator.Calculate(originalTestValues, estimatedTestValues, out errorState);
121            }
122          }
123
124          dataGridView.AutoResizeColumns(DataGridViewAutoSizeColumnsMode.ColumnHeader);
125          dataGridView.AutoResizeRowHeadersWidth(DataGridViewRowHeadersWidthSizeMode.AutoSizeToAllHeaders);
126        }
127      }
128    }
129
130    private IEnumerable<IClassificationSolution> GenerateClassificationSolutions(IClassificationProblemData problemData) {
131      var solutions = new List<IClassificationSolution>();
132      var zeroR = ZeroR.CreateZeroRSolution(problemData);
133      zeroR.Name = "0R Classification Solution";
134      solutions.Add(zeroR);
135      var oneR = OneR.CreateOneRSolution(problemData, 6, new FastRandom());
136      oneR.Name = "1R Classification Solution";
137      solutions.Add(oneR);
138      try {
139        var lda = LinearDiscriminantAnalysis.CreateLinearDiscriminantAnalysisSolution(problemData);
140        lda.Name = "Linear Discriminant Analysis Solution";
141        solutions.Add(lda);
142      }
143      catch (NotSupportedException) { }
144      catch (ArgumentException) { }
145      return solutions;
146    }
147
148    private void dataGridView_MouseDoubleClick(object sender, MouseEventArgs e) {
149      var hittestinfo = dataGridView.HitTest(e.X, e.Y);
150      if (hittestinfo.Type != DataGridViewHitTestType.RowHeader) { return; }
151      if (hittestinfo.RowIndex > solutions.Count) { return; }
152
153      MainFormManager.MainForm.ShowContent(solutions[hittestinfo.RowIndex]);
154    }
155  }
156}
Note: See TracBrowser for help on using the repository browser.