Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis.Views/3.4/Classification/ClassificationEnsembleSolutionAccuracyToCoveredSamples.cs @ 8423

Last change on this file since 8423 was 8329, checked in by sforsten, 12 years ago

#1776:

  • added new diagram which shows correlation of accuracy and covered samples
File size: 7.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 System.Windows.Forms.DataVisualization.Charting;
27using HeuristicLab.Data;
28using HeuristicLab.MainForm;
29using HeuristicLab.Problems.DataAnalysis.Interfaces;
30
31namespace HeuristicLab.Problems.DataAnalysis.Views {
32  [View("Accuracy Covered Dependence")]
33  [Content(typeof(IClassificationEnsembleSolution))]
34  public partial class ClassificationEnsembleSolutionAccuracyToCoveredSamples : DataAnalysisSolutionEvaluationView {
35    private const string ACCURACYCOVERED = "Accuracy to Covered percentage";
36
37    private const string SamplesComboBoxAllSamples = "All Samples";
38    private const string SamplesComboBoxTrainingSamples = "Training Samples";
39    private const string SamplesComboBoxTestSamples = "Test Samples";
40
41    // zero is also a point
42    private const int maxPoints = 101;
43
44    public new ClassificationEnsembleSolution Content {
45      get { return (ClassificationEnsembleSolution)base.Content; }
46      set { base.Content = value; }
47    }
48
49    public ClassificationEnsembleSolutionAccuracyToCoveredSamples()
50      : base() {
51      InitializeComponent();
52
53      SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
54      SamplesComboBox.SelectedIndex = 0;
55      //configure axis
56      this.chart.CustomizeAllChartAreas();
57      this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
58      this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
59      this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
60      this.chart.ChartAreas[0].AxisX.Minimum = 0;
61      this.chart.ChartAreas[0].AxisX.Maximum = 1;
62      this.chart.ChartAreas[0].AxisX.Title = "Covered Samples in %";
63
64      this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
65      this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
66      this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
67      this.chart.ChartAreas[0].AxisY.Minimum = 0;
68      this.chart.ChartAreas[0].AxisY.Maximum = 1;
69      this.chart.ChartAreas[0].AxisY.Title = "Accuracy";
70    }
71
72    private void RedrawChart() {
73      this.chart.Series.Clear();
74      if (Content != null) {
75
76        double[] accuracy = new double[maxPoints + 1];
77        double[] covered = new double[maxPoints + 1];
78
79        IClassificationEnsembleSolutionWeightCalculator weightCalc = Content.WeightCalculator;
80        var solutions = Content.ClassificationSolutions;
81        double[] estimatedClassValues;
82        double[] classValues;
83        OnlineAccuracyCalculator accuracyCalc = new OnlineAccuracyCalculator();
84
85        int rows;
86        double[] confidences;
87
88        if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxAllSamples)) {
89          rows = Content.ProblemData.Dataset.Rows;
90          estimatedClassValues = Content.EstimatedClassValues.ToArray();
91          classValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray();
92          confidences = weightCalc.GetConfidence(solutions, Enumerable.Range(0, rows), estimatedClassValues).ToArray();
93        } else {
94          IntRange range;
95          if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxTrainingSamples)) {
96            range = Content.ProblemData.TrainingPartition;
97            estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
98          } else if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxTestSamples)) {
99            range = Content.ProblemData.TestPartition;
100            estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
101          } else {
102            return;
103          }
104          rows = range.End - range.Start;
105          classValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable)
106                                      .Skip(range.Start).Take(range.End - range.Start).ToArray();
107          confidences = new double[rows];
108          int index;
109          for (int i = 0; i < rows; i++) {
110            index = range.Start + i;
111            confidences[i] = weightCalc.GetConfidence(GetRelevantSolutions(SamplesComboBox.SelectedItem.ToString(), solutions, index),
112                                                      index, estimatedClassValues[i]);
113          }
114        }
115
116        for (int i = 0; i < maxPoints; i++) {
117          double confidenceValue = (1.0 / (maxPoints - 1)) * i;
118          int notCovered = 0;
119
120          for (int j = 0; j < rows; j++) {
121            if (confidences[j] >= confidenceValue) {
122              accuracyCalc.Add(classValues[j], estimatedClassValues[j]);
123            } else {
124              notCovered++;
125            }
126          }
127
128          accuracy[i + 1] = accuracyCalc.Accuracy;
129          covered[i] = 1.0 - (double)notCovered / (double)rows;
130          accuracyCalc.Reset();
131        }
132
133        accuracy[0] = accuracy[1];
134        covered[maxPoints] = 0.0;
135
136        accuracy = accuracy.Reverse().ToArray();
137        covered = covered.Reverse().ToArray();
138
139
140
141        Series serie = this.chart.Series.Add(ACCURACYCOVERED);
142        serie.LegendText = ACCURACYCOVERED;
143        serie.ChartType = SeriesChartType.StepLine;
144        //serie.MarkerStyle = MarkerStyle.Diamond;
145        //serie.MarkerSize = 5;
146        serie.Points.DataBindXY(covered, accuracy);
147      }
148    }
149
150    protected IEnumerable<IClassificationSolution> GetRelevantSolutions(string samplesSelection, IEnumerable<IClassificationSolution> solutions, int curRow) {
151      if (samplesSelection == SamplesComboBoxAllSamples)
152        return solutions;
153      else if (samplesSelection == SamplesComboBoxTrainingSamples)
154        return solutions.Where(s => s.ProblemData.IsTrainingSample(curRow));
155      else if (samplesSelection == SamplesComboBoxTestSamples)
156        return solutions.Where(s => s.ProblemData.IsTestSample(curRow));
157      else
158        return new List<IClassificationSolution>();
159    }
160
161    #region events
162    protected override void RegisterContentEvents() {
163      base.RegisterContentEvents();
164      Content.ModelChanged += new EventHandler(Content_ModelChanged);
165      Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
166    }
167    protected override void DeregisterContentEvents() {
168      base.DeregisterContentEvents();
169      Content.ModelChanged -= new EventHandler(Content_ModelChanged);
170      Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
171    }
172
173    protected override void OnContentChanged() {
174      base.OnContentChanged();
175      RedrawChart();
176    }
177    private void Content_ProblemDataChanged(object sender, EventArgs e) {
178      RedrawChart();
179    }
180    private void Content_ModelChanged(object sender, EventArgs e) {
181      RedrawChart();
182    }
183    private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
184      RedrawChart();
185    }
186    #endregion
187  }
188}
Note: See TracBrowser for help on using the repository browser.