#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
using HeuristicLab.Data;
using HeuristicLab.MainForm;
using HeuristicLab.Problems.DataAnalysis.Interfaces;
namespace HeuristicLab.Problems.DataAnalysis.Views {
[View("Accuracy Covered Dependence")]
[Content(typeof(IClassificationEnsembleSolution))]
public partial class ClassificationEnsembleSolutionAccuracyToCoveredSamples : DataAnalysisSolutionEvaluationView {
private const string ACCURACYCOVERED = "Accuracy to Covered percentage";
private const string SamplesComboBoxAllSamples = "All Samples";
private const string SamplesComboBoxTrainingSamples = "Training Samples";
private const string SamplesComboBoxTestSamples = "Test Samples";
// zero is also a point
private const int maxPoints = 101;
public new ClassificationEnsembleSolution Content {
get { return (ClassificationEnsembleSolution)base.Content; }
set { base.Content = value; }
}
public ClassificationEnsembleSolutionAccuracyToCoveredSamples()
: base() {
InitializeComponent();
SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
SamplesComboBox.SelectedIndex = 0;
//configure axis
this.chart.CustomizeAllChartAreas();
this.chart.ChartAreas[0].CursorX.IsUserSelectionEnabled = true;
this.chart.ChartAreas[0].AxisX.ScaleView.Zoomable = true;
this.chart.ChartAreas[0].AxisX.IsStartedFromZero = true;
this.chart.ChartAreas[0].AxisX.Minimum = 0;
this.chart.ChartAreas[0].AxisX.Maximum = 1;
this.chart.ChartAreas[0].AxisX.Title = "Covered Samples in %";
this.chart.ChartAreas[0].CursorY.IsUserSelectionEnabled = true;
this.chart.ChartAreas[0].AxisY.ScaleView.Zoomable = true;
this.chart.ChartAreas[0].AxisY.IsStartedFromZero = true;
this.chart.ChartAreas[0].AxisY.Minimum = 0;
this.chart.ChartAreas[0].AxisY.Maximum = 1;
this.chart.ChartAreas[0].AxisY.Title = "Accuracy";
}
private void RedrawChart() {
this.chart.Series.Clear();
if (Content != null) {
double[] accuracy = new double[maxPoints + 1];
double[] covered = new double[maxPoints + 1];
IClassificationEnsembleSolutionWeightCalculator weightCalc = Content.WeightCalculator;
var solutions = Content.ClassificationSolutions;
double[] estimatedClassValues;
double[] classValues;
OnlineAccuracyCalculator accuracyCalc = new OnlineAccuracyCalculator();
int rows;
double[] confidences;
if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxAllSamples)) {
rows = Content.ProblemData.Dataset.Rows;
estimatedClassValues = Content.EstimatedClassValues.ToArray();
classValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray();
confidences = weightCalc.GetConfidence(solutions, Enumerable.Range(0, rows), estimatedClassValues).ToArray();
} else {
IntRange range;
if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxTrainingSamples)) {
range = Content.ProblemData.TrainingPartition;
estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
} else if (SamplesComboBox.SelectedItem.ToString().Equals(SamplesComboBoxTestSamples)) {
range = Content.ProblemData.TestPartition;
estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
} else {
return;
}
rows = range.End - range.Start;
classValues = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable)
.Skip(range.Start).Take(range.End - range.Start).ToArray();
confidences = new double[rows];
int index;
for (int i = 0; i < rows; i++) {
index = range.Start + i;
confidences[i] = weightCalc.GetConfidence(GetRelevantSolutions(SamplesComboBox.SelectedItem.ToString(), solutions, index),
index, estimatedClassValues[i]);
}
}
for (int i = 0; i < maxPoints; i++) {
double confidenceValue = (1.0 / (maxPoints - 1)) * i;
int notCovered = 0;
for (int j = 0; j < rows; j++) {
if (confidences[j] >= confidenceValue) {
accuracyCalc.Add(classValues[j], estimatedClassValues[j]);
} else {
notCovered++;
}
}
accuracy[i + 1] = accuracyCalc.Accuracy;
covered[i] = 1.0 - (double)notCovered / (double)rows;
accuracyCalc.Reset();
}
accuracy[0] = accuracy[1];
covered[maxPoints] = 0.0;
accuracy = accuracy.Reverse().ToArray();
covered = covered.Reverse().ToArray();
Series serie = this.chart.Series.Add(ACCURACYCOVERED);
serie.LegendText = ACCURACYCOVERED;
serie.ChartType = SeriesChartType.StepLine;
//serie.MarkerStyle = MarkerStyle.Diamond;
//serie.MarkerSize = 5;
serie.Points.DataBindXY(covered, accuracy);
}
}
protected IEnumerable GetRelevantSolutions(string samplesSelection, IEnumerable solutions, int curRow) {
if (samplesSelection == SamplesComboBoxAllSamples)
return solutions;
else if (samplesSelection == SamplesComboBoxTrainingSamples)
return solutions.Where(s => s.ProblemData.IsTrainingSample(curRow));
else if (samplesSelection == SamplesComboBoxTestSamples)
return solutions.Where(s => s.ProblemData.IsTestSample(curRow));
else
return new List();
}
#region events
protected override void RegisterContentEvents() {
base.RegisterContentEvents();
Content.ModelChanged += new EventHandler(Content_ModelChanged);
Content.ProblemDataChanged += new EventHandler(Content_ProblemDataChanged);
}
protected override void DeregisterContentEvents() {
base.DeregisterContentEvents();
Content.ModelChanged -= new EventHandler(Content_ModelChanged);
Content.ProblemDataChanged -= new EventHandler(Content_ProblemDataChanged);
}
protected override void OnContentChanged() {
base.OnContentChanged();
RedrawChart();
}
private void Content_ProblemDataChanged(object sender, EventArgs e) {
RedrawChart();
}
private void Content_ModelChanged(object sender, EventArgs e) {
RedrawChart();
}
private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
RedrawChart();
}
#endregion
}
}