Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Classification/3.3/ClassificationProblemData.cs @ 6514

Last change on this file since 6514 was 5809, checked in by mkommend, 14 years ago

#1418: Reintegrated branch into trunk.

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