Free cookie consent management tool by TermsFeed Policy Generator

source: branches/VNS/HeuristicLab.Problems.DataAnalysis.Classification/3.3/ClassificationProblemData.cs @ 5723

Last change on this file since 5723 was 5445, checked in by swagner, 14 years ago

Updated year of copyrights (#1406)

File size: 13.7 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.IO;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Parameters;
30using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
31
32namespace HeuristicLab.Problems.DataAnalysis.Classification {
33  [Item("ClassificationProblemData", "Represents an item containing all data defining a classification problem.")]
34  [StorableClass]
35  public class ClassificationProblemData : DataAnalysisProblemData {
36    #region default data
37    private static string[] defaultInputs = new string[] { "sample", "clump thickness", "cell size", "cell shape", "marginal adhesion", "epithelial cell size", "bare nuclei", "chromatin", "nucleoli", "mitoses", "class" };
38    private static double[,] defaultData = new double[,]{
39     {1000025,5,1,1,1,2,1,3,1,1,2      },
40     {1002945,5,4,4,5,7,10,3,2,1,2     },
41     {1015425,3,1,1,1,2,2,3,1,1,2      },
42     {1016277,6,8,8,1,3,4,3,7,1,2      },
43     {1017023,4,1,1,3,2,1,3,1,1,2      },
44     {1017122,8,10,10,8,7,10,9,7,1,4   },
45     {1018099,1,1,1,1,2,10,3,1,1,2     },
46     {1018561,2,1,2,1,2,1,3,1,1,2      },
47     {1033078,2,1,1,1,2,1,1,1,5,2      },
48     {1033078,4,2,1,1,2,1,2,1,1,2      },
49     {1035283,1,1,1,1,1,1,3,1,1,2      },
50     {1036172,2,1,1,1,2,1,2,1,1,2      },
51     {1041801,5,3,3,3,2,3,4,4,1,4      },
52     {1043999,1,1,1,1,2,3,3,1,1,2      },
53     {1044572,8,7,5,10,7,9,5,5,4,4     },
54     {1047630,7,4,6,4,6,1,4,3,1,4      },
55     {1048672,4,1,1,1,2,1,2,1,1,2      },
56     {1049815,4,1,1,1,2,1,3,1,1,2      },
57     {1050670,10,7,7,6,4,10,4,1,2,4    },
58     {1050718,6,1,1,1,2,1,3,1,1,2      },
59     {1054590,7,3,2,10,5,10,5,4,4,4    },
60     {1054593,10,5,5,3,6,7,7,10,1,4    },
61     {1056784,3,1,1,1,2,1,2,1,1,2      },
62     {1057013,8,4,5,1,2,2,7,3,1,4      },
63     {1059552,1,1,1,1,2,1,3,1,1,2      },
64     {1065726,5,2,3,4,2,7,3,6,1,4      },
65     {1066373,3,2,1,1,1,1,2,1,1,2      },
66     {1066979,5,1,1,1,2,1,2,1,1,2      },
67     {1067444,2,1,1,1,2,1,2,1,1,2      },
68     {1070935,1,1,3,1,2,1,1,1,1,2      },
69     {1070935,3,1,1,1,1,1,2,1,1,2      },
70     {1071760,2,1,1,1,2,1,3,1,1,2      },
71     {1072179,10,7,7,3,8,5,7,4,3,4     },
72     {1074610,2,1,1,2,2,1,3,1,1,2      },
73     {1075123,3,1,2,1,2,1,2,1,1,2      },
74     {1079304,2,1,1,1,2,1,2,1,1,2      },
75     {1080185,10,10,10,8,6,1,8,9,1,4   },
76     {1081791,6,2,1,1,1,1,7,1,1,2      },
77     {1084584,5,4,4,9,2,10,5,6,1,4     },
78     {1091262,2,5,3,3,6,7,7,5,1,4      },
79     {1096800,6,6,6,9,6,4,7,8,1,2      },
80     {1099510,10,4,3,1,3,3,6,5,2,4     },
81     {1100524,6,10,10,2,8,10,7,3,3,4   },
82     {1102573,5,6,5,6,10,1,3,1,1,4     },
83     {1103608,10,10,10,4,8,1,8,10,1,4  },
84     {1103722,1,1,1,1,2,1,2,1,2,2      },
85     {1105257,3,7,7,4,4,9,4,8,1,4      },
86     {1105524,1,1,1,1,2,1,2,1,1,2      },
87     {1106095,4,1,1,3,2,1,3,1,1,2      },
88     {1106829,7,8,7,2,4,8,3,8,2,4      },
89     {1108370,9,5,8,1,2,3,2,1,5,4      },
90     {1108449,5,3,3,4,2,4,3,4,1,4      },
91     {1110102,10,3,6,2,3,5,4,10,2,4    },
92     {1110503,5,5,5,8,10,8,7,3,7,4     },
93     {1110524,10,5,5,6,8,8,7,1,1,4     },
94     {1111249,10,6,6,3,4,5,3,6,1,4     },
95     {1112209,8,10,10,1,3,6,3,9,1,4    },
96     {1113038,8,2,4,1,5,1,5,4,4,4      },
97     {1113483,5,2,3,1,6,10,5,1,1,4     },
98     {1113906,9,5,5,2,2,2,5,1,1,4      },
99     {1115282,5,3,5,5,3,3,4,10,1,4     },
100     {1115293,1,1,1,1,2,2,2,1,1,2      },
101     {1116116,9,10,10,1,10,8,3,3,1,4   },
102     {1116132,6,3,4,1,5,2,3,9,1,4      },
103     {1116192,1,1,1,1,2,1,2,1,1,2      },
104     {1116998,10,4,2,1,3,2,4,3,10,4    },
105     {1117152,4,1,1,1,2,1,3,1,1,2      },
106     {1118039,5,3,4,1,8,10,4,9,1,4     },
107     {1120559,8,3,8,3,4,9,8,9,8,4      },
108     {1121732,1,1,1,1,2,1,3,2,1,2      },
109     {1121919,5,1,3,1,2,1,2,1,1,2      },
110     {1123061,6,10,2,8,10,2,7,8,10,4   },
111     {1124651,1,3,3,2,2,1,7,2,1,2      },
112     {1125035,9,4,5,10,6,10,4,8,1,4    },
113     {1126417,10,6,4,1,3,4,3,2,3,4     },
114     {1131294,1,1,2,1,2,2,4,2,1,2      },
115     {1132347,1,1,4,1,2,1,2,1,1,2      },
116     {1133041,5,3,1,2,2,1,2,1,1,2      },
117     {1133136,3,1,1,1,2,3,3,1,1,2      },
118     {1136142,2,1,1,1,3,1,2,1,1,2      },
119     {1137156,2,2,2,1,1,1,7,1,1,2      },
120     {1143978,4,1,1,2,2,1,2,1,1,2      },
121     {1143978,5,2,1,1,2,1,3,1,1,2      },
122     {1147044,3,1,1,1,2,2,7,1,1,2      },
123     {1147699,3,5,7,8,8,9,7,10,7,4     },
124     {1147748,5,10,6,1,10,4,4,10,10,4  },
125     {1148278,3,3,6,4,5,8,4,4,1,4      },
126     {1148873,3,6,6,6,5,10,6,8,3,4     },
127     {1152331,4,1,1,1,2,1,3,1,1,2      },
128     {1155546,2,1,1,2,3,1,2,1,1,2      },
129     {1156272,1,1,1,1,2,1,3,1,1,2      },
130     {1156948,3,1,1,2,2,1,1,1,1,2      },
131     {1157734,4,1,1,1,2,1,3,1,1,2      },
132     {1158247,1,1,1,1,2,1,2,1,1,2      },
133     {1160476,2,1,1,1,2,1,3,1,1,2      },
134     {1164066,1,1,1,1,2,1,3,1,1,2      },
135     {1165297,2,1,1,2,2,1,1,1,1,2      },
136     {1165790,5,1,1,1,2,1,3,1,1,2      },
137     {1165926,9,6,9,2,10,6,2,9,10,4    },
138     {1166630,7,5,6,10,5,10,7,9,4,4    },
139     {1166654,10,3,5,1,10,5,3,10,2,4   },
140     {1167439,2,3,4,4,2,5,2,5,1,4      },
141     {1167471,4,1,2,1,2,1,3,1,1,2      },
142     {1168359,8,2,3,1,6,3,7,1,1,4      },
143     {1168736,10,10,10,10,10,1,8,8,8,4 },
144     {1169049,7,3,4,4,3,3,3,2,7,4      },
145     {1170419,10,10,10,8,2,10,4,1,1,4  },
146     {1170420,1,6,8,10,8,10,5,7,1,4    },
147     {1171710,1,1,1,1,2,1,2,3,1,2      },
148     {1171710,6,5,4,4,3,9,7,8,3,4      },
149     {1171795,1,3,1,2,2,2,5,3,2,2      },
150     {1171845,8,6,4,3,5,9,3,1,1,4      },
151     {1172152,10,3,3,10,2,10,7,3,3,4   },
152     {1173216,10,10,10,3,10,8,8,1,1,4  },
153     {1173235,3,3,2,1,2,3,3,1,1,2      },
154     {1173347,1,1,1,1,2,5,1,1,1,2      },
155     {1173347,8,3,3,1,2,2,3,2,1,2      },
156     {1173509,4,5,5,10,4,10,7,5,8,4    },
157     {1173514,1,1,1,1,4,3,1,1,1,2      },
158     {1173681,3,2,1,1,2,2,3,1,1,2      },
159     {1174057,1,1,2,2,2,1,3,1,1,2      },
160     {1174057,4,2,1,1,2,2,3,1,1,2      },
161     {1174131,10,10,10,2,10,10,5,3,3,4 },
162     {1174428,5,3,5,1,8,10,5,3,1,4     },
163     {1175937,5,4,6,7,9,7,8,10,1,4     },
164     {1176406,1,1,1,1,2,1,2,1,1,2      },
165     {1176881,7,5,3,7,4,10,7,5,5,4        }
166};
167    #endregion
168
169    private const int MaximumClasses = 20;
170    private const string ClassNamesParameterName = "ClassNames";
171    private const string MisclassificationMatrixParameterName = "MisClassificationMatrix";
172
173    public StringArray ClassNames {
174      get { return ClassNamesParameter.Value; }
175      protected set { ClassNamesParameter.Value = value; }
176    }
177    public IValueParameter<StringArray> ClassNamesParameter {
178      get { return (IValueParameter<StringArray>)Parameters[ClassNamesParameterName]; }
179    }
180
181    public DoubleMatrix MisclassificationMatrix {
182      get { return MisclassificationMatrixParameter.Value; }
183      protected set { MisclassificationMatrixParameter.Value = value; }
184    }
185    public IValueParameter<DoubleMatrix> MisclassificationMatrixParameter {
186      get { return (IValueParameter<DoubleMatrix>)Parameters[MisclassificationMatrixParameterName]; }
187    }
188
189    [Storable]
190    private List<double> sortedClassValues;
191    public IEnumerable<double> SortedClassValues {
192      get { return sortedClassValues; }
193    }
194    public int NumberOfClasses {
195      get { return sortedClassValues.Count; }
196    }
197
198    [StorableConstructor]
199    protected ClassificationProblemData(bool deserializing) : base(deserializing) { }
200    protected ClassificationProblemData(ClassificationProblemData original, Cloner cloner)
201      : base(original, cloner) {
202      RegisterParameterEvents();
203      UpdateClassValues();
204    }
205    public ClassificationProblemData()
206      : base(new Dataset(defaultInputs, defaultData), defaultInputs, defaultInputs[defaultInputs.Length - 1], 0, 60, 60, 120) {
207      Parameters.Add(new ValueParameter<StringArray>(ClassNamesParameterName, "An array of the names for all class values."));
208      Parameters.Add(new ValueParameter<DoubleMatrix>(MisclassificationMatrixParameterName, "A matrix that describles the penalties for misclassifaction between the single classes."));
209      sortedClassValues = new List<double>();
210
211      InputVariables.SetItemCheckedState(InputVariables[InputVariables.Count - 1], false);
212      RegisterParameterEvents();
213      UpdateClassValues();
214    }
215
216    public override IDeepCloneable Clone(Cloner cloner) {
217      return new ClassificationProblemData(this, cloner);
218    }
219
220    [StorableHook(HookType.AfterDeserialization)]
221    private void AfterDeserialization() {
222      RegisterParameterEvents();
223      RegisterParameterValueEvents();
224    }
225
226    public override void ImportFromFile(string fileName) {
227      var csvFileParser = new TableFileParser();
228      csvFileParser.Parse(fileName);
229      suppressEvents = true;
230      Name = "Data imported from " + Path.GetFileName(fileName);
231      Dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
232      Dataset.Name = Path.GetFileName(fileName);
233      var variableNames = Dataset.VariableNames.Select(x => new StringValue(x).AsReadOnly()).ToList();
234
235      var validTargetVariables = from v in variableNames
236                                 let DistinctValues = Dataset.Rows > 50 ? Dataset.GetVariableValues(v.Value, 0, 50).Distinct().Count()
237                                                                        : Dataset.GetVariableValues(v.Value).Distinct().Count()
238                                 where DistinctValues < MaximumClasses
239                                 select v;
240
241      if (!validTargetVariables.Any())
242        throw new ArgumentException("Import of classification problem data was not successfull, because no target variable was found." +
243          " A target variable must have at most " + MaximumClasses + " distinct values to be applicable to classification.");
244
245      ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Clear();
246      foreach (var variableName in validTargetVariables)
247        ((ConstrainedValueParameter<StringValue>)TargetVariableParameter).ValidValues.Add(variableName);
248      TargetVariable = validTargetVariables.FirstOrDefault();
249
250      InputVariables = new CheckedItemList<StringValue>(variableNames).AsReadOnly();
251      if (TargetVariable != null) InputVariables.SetItemCheckedState(TargetVariable, false);
252      int middle = (int)(csvFileParser.Rows * 0.5);
253      TrainingSamplesEnd = new IntValue(middle);
254      TrainingSamplesStart = new IntValue(0);
255      TestSamplesEnd = new IntValue(csvFileParser.Rows);
256      TestSamplesStart = new IntValue(middle);
257
258      UpdateClassValues();
259      suppressEvents = false;
260      OnProblemDataChanged(EventArgs.Empty);
261    }
262
263    protected override void OnProblemDataChanged(EventArgs e) {
264      base.OnProblemDataChanged(e);
265      if (!suppressEvents)
266        UpdateClassValues();
267    }
268
269    private void UpdateClassValues() {
270      sortedClassValues = Dataset.GetVariableValues(TargetVariable.Value).Distinct().ToList();
271      sortedClassValues.Sort();
272      ResetMisclassificationMatrix();
273      UpdateClassNames();
274    }
275
276    private void UpdateClassNames() {
277      if (ClassNames != null) DeregisterParameterValueEvents();
278      StringArray array = new StringArray(NumberOfClasses);
279      int i = 0;
280      foreach (double classValue in SortedClassValues) {
281        array[i] = "Class " + classValue;
282        i++;
283      }
284      ClassNames = array;
285      UpdateMisclassifciationMatrixHeaders();
286      RegisterParameterValueEvents();
287    }
288
289    private void RegisterParameterEvents() {
290      ClassNamesParameter.ValueChanged += new EventHandler(ClassNamesChanged);
291    }
292    private void RegisterParameterValueEvents() {
293      ClassNames.ItemChanged += new EventHandler<EventArgs<int>>(ClassNamesChanged);
294    }
295    private void DeregisterParameterValueEvents() {
296      ClassNames.ItemChanged -= new EventHandler<EventArgs<int>>(ClassNamesChanged);
297    }
298
299    private void ClassNamesChanged(object sender, EventArgs e) {
300      UpdateMisclassifciationMatrixHeaders();
301    }
302
303    private void ResetMisclassificationMatrix() {
304      double[,] matrix = new double[NumberOfClasses, NumberOfClasses];
305      for (int i = 0; i < NumberOfClasses; i++) {
306        for (int j = 0; j < NumberOfClasses; j++)
307          if (i != j) matrix[i, j] = 1;
308      }
309
310      if (MisclassificationMatrix == null)
311        MisclassificationMatrix = new DoubleMatrix(matrix);
312      if (MisclassificationMatrix.Rows != NumberOfClasses)
313        MisclassificationMatrix = new DoubleMatrix(matrix);
314      if (MisclassificationMatrix.Columns != NumberOfClasses)
315        MisclassificationMatrix = new DoubleMatrix(matrix);
316    }
317
318    private void UpdateMisclassifciationMatrixHeaders() {
319      MisclassificationMatrix.RowNames = ClassNames.Select(name => "Estimated " + name).ToList();
320      MisclassificationMatrix.ColumnNames = ClassNames.Select(name => "Actual " + name).ToList();
321    }
322  }
323}
Note: See TracBrowser for help on using the repository browser.