[5678] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2011 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 | using System;
|
---|
[6672] | 22 | using System.Collections.Generic;
|
---|
[6680] | 23 | using System.Drawing;
|
---|
[5678] | 24 | using System.Linq;
|
---|
| 25 | using System.Windows.Forms;
|
---|
| 26 | using HeuristicLab.Data;
|
---|
| 27 | using HeuristicLab.MainForm;
|
---|
| 28 | using HeuristicLab.MainForm.WindowsForms;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Problems.DataAnalysis.Views {
|
---|
[5975] | 31 | [View("Estimated Class Values")]
|
---|
[6672] | 32 | [Content(typeof(ClassificationEnsembleSolution))]
|
---|
| 33 | public partial class ClassificationEnsembleSolutionEstimatedClassValuesView :
|
---|
| 34 | ClassificationSolutionEstimatedClassValuesView {
|
---|
| 35 | private const string RowColumnName = "Row";
|
---|
[6680] | 36 | private const string TargetClassValuesColumnName = "Target Variable";
|
---|
| 37 | private const string EstimatedClassValuesColumnName = "Estimated Class Values";
|
---|
| 38 | private const string CorrectClassificationColumnName = "Correct Classification";
|
---|
[6672] | 39 | private const string ConfidenceColumnName = "Confidence";
|
---|
[5678] | 40 |
|
---|
[6672] | 41 | private const string SamplesComboBoxAllSamples = "All Samples";
|
---|
| 42 | private const string SamplesComboBoxTrainingSamples = "Training Samples";
|
---|
| 43 | private const string SamplesComboBoxTestSamples = "Test Samples";
|
---|
| 44 |
|
---|
| 45 | public new ClassificationEnsembleSolution Content {
|
---|
| 46 | get { return (ClassificationEnsembleSolution)base.Content; }
|
---|
[6642] | 47 | set { base.Content = value; }
|
---|
[5678] | 48 | }
|
---|
| 49 |
|
---|
[6672] | 50 | public ClassificationEnsembleSolutionEstimatedClassValuesView()
|
---|
[5678] | 51 | : base() {
|
---|
| 52 | InitializeComponent();
|
---|
[6672] | 53 | SamplesComboBox.Items.AddRange(new string[] { SamplesComboBoxAllSamples, SamplesComboBoxTrainingSamples, SamplesComboBoxTestSamples });
|
---|
| 54 | SamplesComboBox.SelectedIndex = 0;
|
---|
[6680] | 55 | matrixView.DataGridView.RowPrePaint += new DataGridViewRowPrePaintEventHandler(DataGridView_RowPrePaint);
|
---|
[5678] | 56 | }
|
---|
| 57 |
|
---|
[6680] | 58 |
|
---|
| 59 |
|
---|
[6672] | 60 | private void SamplesComboBox_SelectedIndexChanged(object sender, EventArgs e) {
|
---|
| 61 | UpdateEstimatedValues();
|
---|
| 62 | }
|
---|
| 63 |
|
---|
[6642] | 64 | protected override void UpdateEstimatedValues() {
|
---|
[6672] | 65 | if (InvokeRequired) {
|
---|
| 66 | Invoke((Action)UpdateEstimatedValues);
|
---|
| 67 | return;
|
---|
| 68 | }
|
---|
| 69 | if (Content == null) {
|
---|
| 70 | matrixView.Content = null;
|
---|
| 71 | return;
|
---|
| 72 | }
|
---|
[5678] | 73 |
|
---|
[6672] | 74 | int[] indizes;
|
---|
| 75 | double[] estimatedClassValues;
|
---|
| 76 |
|
---|
| 77 | switch (SamplesComboBox.SelectedItem.ToString()) {
|
---|
| 78 | case SamplesComboBoxAllSamples: {
|
---|
| 79 | indizes = Enumerable.Range(0, Content.ProblemData.Dataset.Rows).ToArray();
|
---|
| 80 | estimatedClassValues = Content.EstimatedClassValues.ToArray();
|
---|
| 81 | break;
|
---|
[5678] | 82 | }
|
---|
[6672] | 83 | case SamplesComboBoxTrainingSamples: {
|
---|
| 84 | indizes = Content.ProblemData.TrainingIndizes.ToArray();
|
---|
| 85 | estimatedClassValues = Content.EstimatedTrainingClassValues.ToArray();
|
---|
| 86 | break;
|
---|
| 87 | }
|
---|
| 88 | case SamplesComboBoxTestSamples: {
|
---|
| 89 | indizes = Content.ProblemData.TestIndizes.ToArray();
|
---|
| 90 | estimatedClassValues = Content.EstimatedTestClassValues.ToArray();
|
---|
| 91 | break;
|
---|
| 92 | }
|
---|
| 93 | default:
|
---|
| 94 | throw new ArgumentException();
|
---|
| 95 | }
|
---|
[5678] | 96 |
|
---|
[6672] | 97 | int classValuesCount = Content.ProblemData.ClassValues.Count;
|
---|
| 98 | int modelCount = Content.Model.Models.Count();
|
---|
[6680] | 99 | string[,] values = new string[indizes.Length, 5 + classValuesCount + modelCount];
|
---|
[6740] | 100 | double[] target = Content.ProblemData.Dataset.GetDoubleValues(Content.ProblemData.TargetVariable).ToArray();
|
---|
[6672] | 101 | List<List<double?>> estimatedValuesVector = GetEstimatedValues(SamplesComboBox.SelectedItem.ToString(), indizes,
|
---|
| 102 | Content.ClassificationSolutions);
|
---|
| 103 |
|
---|
| 104 | for (int i = 0; i < indizes.Length; i++) {
|
---|
| 105 | int row = indizes[i];
|
---|
| 106 | values[i, 0] = row.ToString();
|
---|
| 107 | values[i, 1] = target[i].ToString();
|
---|
| 108 | values[i, 2] = estimatedClassValues[i].ToString();
|
---|
[6680] | 109 | values[i, 3] = (target[i] == estimatedClassValues[i]).ToString();
|
---|
[6672] | 110 | var groups = estimatedValuesVector[i].GroupBy(x => x).Select(g => new { Key = g.Key, Count = g.Count() }).ToList();
|
---|
| 111 | var estimationCount = groups.Where(g => g.Key != null).Select(g => g.Count).Sum();
|
---|
[6680] | 112 | values[i, 4] = (((double)groups.Where(g => g.Key == estimatedClassValues[i]).Single().Count) / estimationCount).ToString();
|
---|
[6672] | 113 | for (int classIndex = 0; classIndex < Content.ProblemData.ClassValues.Count; classIndex++) {
|
---|
| 114 | var group = groups.Where(g => g.Key == Content.ProblemData.ClassValues[classIndex]).SingleOrDefault();
|
---|
[6680] | 115 | if (group == null) values[i, 5 + classIndex] = 0.ToString();
|
---|
| 116 | else values[i, 5 + classIndex] = group.Count.ToString();
|
---|
[5678] | 117 | }
|
---|
[6672] | 118 | for (int modelIndex = 0; modelIndex < estimatedValuesVector[i].Count; modelIndex++) {
|
---|
[6680] | 119 | values[i, 5 + classValuesCount + modelIndex] = estimatedValuesVector[i][modelIndex] == null
|
---|
[6672] | 120 | ? string.Empty
|
---|
| 121 | : estimatedValuesVector[i][modelIndex].ToString();
|
---|
| 122 | }
|
---|
| 123 |
|
---|
[5678] | 124 | }
|
---|
[6672] | 125 |
|
---|
| 126 | StringMatrix matrix = new StringMatrix(values);
|
---|
[6680] | 127 | List<string> columnNames = new List<string>() { "Id", TargetClassValuesColumnName, EstimatedClassValuesColumnName, CorrectClassificationColumnName, ConfidenceColumnName };
|
---|
[6672] | 128 | columnNames.AddRange(Content.ProblemData.ClassNames);
|
---|
| 129 | columnNames.AddRange(Content.Model.Models.Select(m => m.Name));
|
---|
| 130 | matrix.ColumnNames = columnNames;
|
---|
| 131 | matrix.SortableView = true;
|
---|
| 132 | matrixView.Content = matrix;
|
---|
[6680] | 133 | UpdateColoringOfRows();
|
---|
[5678] | 134 | }
|
---|
[6672] | 135 |
|
---|
| 136 | private List<List<double?>> GetEstimatedValues(string samplesSelection, int[] rows, IEnumerable<IClassificationSolution> solutions) {
|
---|
| 137 | List<List<double?>> values = new List<List<double?>>();
|
---|
| 138 | int solutionIndex = 0;
|
---|
| 139 | foreach (var solution in solutions) {
|
---|
| 140 | double[] estimation = solution.GetEstimatedClassValues(rows).ToArray();
|
---|
| 141 | for (int i = 0; i < rows.Length; i++) {
|
---|
| 142 | var row = rows[i];
|
---|
| 143 | if (solutionIndex == 0) values.Add(new List<double?>());
|
---|
| 144 |
|
---|
| 145 | if (samplesSelection == SamplesComboBoxAllSamples)
|
---|
| 146 | values[i].Add(estimation[i]);
|
---|
| 147 | else if (samplesSelection == SamplesComboBoxTrainingSamples && solution.ProblemData.IsTrainingSample(row))
|
---|
| 148 | values[i].Add(estimation[i]);
|
---|
| 149 | else if (samplesSelection == SamplesComboBoxTestSamples && solution.ProblemData.IsTestSample(row))
|
---|
| 150 | values[i].Add(estimation[i]);
|
---|
| 151 | else
|
---|
| 152 | values[i].Add(null);
|
---|
| 153 | }
|
---|
| 154 | solutionIndex++;
|
---|
| 155 | }
|
---|
| 156 | return values;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[6680] | 159 | private void DataGridView_RowPrePaint(object sender, DataGridViewRowPrePaintEventArgs e) {
|
---|
| 160 | if (InvokeRequired) {
|
---|
| 161 | Invoke(new EventHandler<DataGridViewRowPrePaintEventArgs>(DataGridView_RowPrePaint), sender, e);
|
---|
| 162 | return;
|
---|
| 163 | }
|
---|
| 164 | bool correctClassified = bool.Parse(matrixView.DataGridView[3, e.RowIndex].Value.ToString());
|
---|
| 165 | matrixView.DataGridView.Rows[e.RowIndex].DefaultCellStyle.ForeColor = correctClassified ? Color.MediumSeaGreen : Color.Red;
|
---|
| 166 | }
|
---|
[6672] | 167 |
|
---|
[6680] | 168 | private void UpdateColoringOfRows() {
|
---|
| 169 | if (InvokeRequired) {
|
---|
| 170 | Invoke((Action)UpdateColoringOfRows);
|
---|
| 171 | return;
|
---|
| 172 | }
|
---|
| 173 | //matrixView.DataGridView.SuspendRepaint();
|
---|
| 174 | //for (int i = 0; i < matrixView.DataGridView.Rows.Count; i++) {
|
---|
| 175 | // bool correctClassified = bool.Parse(matrixView.Content.GetValue(i, 3));
|
---|
| 176 | // matrixView.DataGridView.Rows[i].DefaultCellStyle.ForeColor = correctClassified ? Color.MediumSeaGreen : Color.Red;
|
---|
| 177 | //}
|
---|
| 178 | //matrixView.DataGridView.ResumeRepaint(true);
|
---|
| 179 | }
|
---|
[5678] | 180 | }
|
---|
| 181 | }
|
---|