Free cookie consent management tool by TermsFeed Policy Generator

source: branches/Async/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationSolutionComparisonView.cs @ 14180

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

#1998: fixed a problem in Classification/ClassificationSolutionComparisonView

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