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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Linq;
|
---|
25 | using HeuristicLab.Common;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 |
|
---|
28 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
29 | [StorableClass]
|
---|
30 | public class ClassificationProblemData : DataAnalysisProblemData, IClassificationProblemData {
|
---|
31 | #region default data
|
---|
32 | private static string[] defaultVariableNames = new string[] { "sample", "clump thickness", "cell size", "cell shape", "marginal adhesion", "epithelial cell size", "bare nuclei", "chromatin", "nucleoli", "mitoses", "class" };
|
---|
33 | private static double[,] defaultData = new double[,]{
|
---|
34 | {1000025,5,1,1,1,2,1,3,1,1,2 },
|
---|
35 | {1002945,5,4,4,5,7,10,3,2,1,2 },
|
---|
36 | {1015425,3,1,1,1,2,2,3,1,1,2 },
|
---|
37 | {1016277,6,8,8,1,3,4,3,7,1,2 },
|
---|
38 | {1017023,4,1,1,3,2,1,3,1,1,2 },
|
---|
39 | {1017122,8,10,10,8,7,10,9,7,1,4 },
|
---|
40 | {1018099,1,1,1,1,2,10,3,1,1,2 },
|
---|
41 | {1018561,2,1,2,1,2,1,3,1,1,2 },
|
---|
42 | {1033078,2,1,1,1,2,1,1,1,5,2 },
|
---|
43 | {1033078,4,2,1,1,2,1,2,1,1,2 },
|
---|
44 | {1035283,1,1,1,1,1,1,3,1,1,2 },
|
---|
45 | {1036172,2,1,1,1,2,1,2,1,1,2 },
|
---|
46 | {1041801,5,3,3,3,2,3,4,4,1,4 },
|
---|
47 | {1043999,1,1,1,1,2,3,3,1,1,2 },
|
---|
48 | {1044572,8,7,5,10,7,9,5,5,4,4 },
|
---|
49 | {1047630,7,4,6,4,6,1,4,3,1,4 },
|
---|
50 | {1048672,4,1,1,1,2,1,2,1,1,2 },
|
---|
51 | {1049815,4,1,1,1,2,1,3,1,1,2 },
|
---|
52 | {1050670,10,7,7,6,4,10,4,1,2,4 },
|
---|
53 | {1050718,6,1,1,1,2,1,3,1,1,2 },
|
---|
54 | {1054590,7,3,2,10,5,10,5,4,4,4 },
|
---|
55 | {1054593,10,5,5,3,6,7,7,10,1,4 },
|
---|
56 | {1056784,3,1,1,1,2,1,2,1,1,2 },
|
---|
57 | {1057013,8,4,5,1,2,2,7,3,1,4 },
|
---|
58 | {1059552,1,1,1,1,2,1,3,1,1,2 },
|
---|
59 | {1065726,5,2,3,4,2,7,3,6,1,4 },
|
---|
60 | {1066373,3,2,1,1,1,1,2,1,1,2 },
|
---|
61 | {1066979,5,1,1,1,2,1,2,1,1,2 },
|
---|
62 | {1067444,2,1,1,1,2,1,2,1,1,2 },
|
---|
63 | {1070935,1,1,3,1,2,1,1,1,1,2 },
|
---|
64 | {1070935,3,1,1,1,1,1,2,1,1,2 },
|
---|
65 | {1071760,2,1,1,1,2,1,3,1,1,2 },
|
---|
66 | {1072179,10,7,7,3,8,5,7,4,3,4 },
|
---|
67 | {1074610,2,1,1,2,2,1,3,1,1,2 },
|
---|
68 | {1075123,3,1,2,1,2,1,2,1,1,2 },
|
---|
69 | {1079304,2,1,1,1,2,1,2,1,1,2 },
|
---|
70 | {1080185,10,10,10,8,6,1,8,9,1,4 },
|
---|
71 | {1081791,6,2,1,1,1,1,7,1,1,2 },
|
---|
72 | {1084584,5,4,4,9,2,10,5,6,1,4 },
|
---|
73 | {1091262,2,5,3,3,6,7,7,5,1,4 },
|
---|
74 | {1096800,6,6,6,9,6,4,7,8,1,2 },
|
---|
75 | {1099510,10,4,3,1,3,3,6,5,2,4 },
|
---|
76 | {1100524,6,10,10,2,8,10,7,3,3,4 },
|
---|
77 | {1102573,5,6,5,6,10,1,3,1,1,4 },
|
---|
78 | {1103608,10,10,10,4,8,1,8,10,1,4 },
|
---|
79 | {1103722,1,1,1,1,2,1,2,1,2,2 },
|
---|
80 | {1105257,3,7,7,4,4,9,4,8,1,4 },
|
---|
81 | {1105524,1,1,1,1,2,1,2,1,1,2 },
|
---|
82 | {1106095,4,1,1,3,2,1,3,1,1,2 },
|
---|
83 | {1106829,7,8,7,2,4,8,3,8,2,4 },
|
---|
84 | {1108370,9,5,8,1,2,3,2,1,5,4 },
|
---|
85 | {1108449,5,3,3,4,2,4,3,4,1,4 },
|
---|
86 | {1110102,10,3,6,2,3,5,4,10,2,4 },
|
---|
87 | {1110503,5,5,5,8,10,8,7,3,7,4 },
|
---|
88 | {1110524,10,5,5,6,8,8,7,1,1,4 },
|
---|
89 | {1111249,10,6,6,3,4,5,3,6,1,4 },
|
---|
90 | {1112209,8,10,10,1,3,6,3,9,1,4 },
|
---|
91 | {1113038,8,2,4,1,5,1,5,4,4,4 },
|
---|
92 | {1113483,5,2,3,1,6,10,5,1,1,4 },
|
---|
93 | {1113906,9,5,5,2,2,2,5,1,1,4 },
|
---|
94 | {1115282,5,3,5,5,3,3,4,10,1,4 },
|
---|
95 | {1115293,1,1,1,1,2,2,2,1,1,2 },
|
---|
96 | {1116116,9,10,10,1,10,8,3,3,1,4 },
|
---|
97 | {1116132,6,3,4,1,5,2,3,9,1,4 },
|
---|
98 | {1116192,1,1,1,1,2,1,2,1,1,2 },
|
---|
99 | {1116998,10,4,2,1,3,2,4,3,10,4 },
|
---|
100 | {1117152,4,1,1,1,2,1,3,1,1,2 },
|
---|
101 | {1118039,5,3,4,1,8,10,4,9,1,4 },
|
---|
102 | {1120559,8,3,8,3,4,9,8,9,8,4 },
|
---|
103 | {1121732,1,1,1,1,2,1,3,2,1,2 },
|
---|
104 | {1121919,5,1,3,1,2,1,2,1,1,2 },
|
---|
105 | {1123061,6,10,2,8,10,2,7,8,10,4 },
|
---|
106 | {1124651,1,3,3,2,2,1,7,2,1,2 },
|
---|
107 | {1125035,9,4,5,10,6,10,4,8,1,4 },
|
---|
108 | {1126417,10,6,4,1,3,4,3,2,3,4 },
|
---|
109 | {1131294,1,1,2,1,2,2,4,2,1,2 },
|
---|
110 | {1132347,1,1,4,1,2,1,2,1,1,2 },
|
---|
111 | {1133041,5,3,1,2,2,1,2,1,1,2 },
|
---|
112 | {1133136,3,1,1,1,2,3,3,1,1,2 },
|
---|
113 | {1136142,2,1,1,1,3,1,2,1,1,2 },
|
---|
114 | {1137156,2,2,2,1,1,1,7,1,1,2 },
|
---|
115 | {1143978,4,1,1,2,2,1,2,1,1,2 },
|
---|
116 | {1143978,5,2,1,1,2,1,3,1,1,2 },
|
---|
117 | {1147044,3,1,1,1,2,2,7,1,1,2 },
|
---|
118 | {1147699,3,5,7,8,8,9,7,10,7,4 },
|
---|
119 | {1147748,5,10,6,1,10,4,4,10,10,4 },
|
---|
120 | {1148278,3,3,6,4,5,8,4,4,1,4 },
|
---|
121 | {1148873,3,6,6,6,5,10,6,8,3,4 },
|
---|
122 | {1152331,4,1,1,1,2,1,3,1,1,2 },
|
---|
123 | {1155546,2,1,1,2,3,1,2,1,1,2 },
|
---|
124 | {1156272,1,1,1,1,2,1,3,1,1,2 },
|
---|
125 | {1156948,3,1,1,2,2,1,1,1,1,2 },
|
---|
126 | {1157734,4,1,1,1,2,1,3,1,1,2 },
|
---|
127 | {1158247,1,1,1,1,2,1,2,1,1,2 },
|
---|
128 | {1160476,2,1,1,1,2,1,3,1,1,2 },
|
---|
129 | {1164066,1,1,1,1,2,1,3,1,1,2 },
|
---|
130 | {1165297,2,1,1,2,2,1,1,1,1,2 },
|
---|
131 | {1165790,5,1,1,1,2,1,3,1,1,2 },
|
---|
132 | {1165926,9,6,9,2,10,6,2,9,10,4 },
|
---|
133 | {1166630,7,5,6,10,5,10,7,9,4,4 },
|
---|
134 | {1166654,10,3,5,1,10,5,3,10,2,4 },
|
---|
135 | {1167439,2,3,4,4,2,5,2,5,1,4 },
|
---|
136 | {1167471,4,1,2,1,2,1,3,1,1,2 },
|
---|
137 | {1168359,8,2,3,1,6,3,7,1,1,4 },
|
---|
138 | {1168736,10,10,10,10,10,1,8,8,8,4 },
|
---|
139 | {1169049,7,3,4,4,3,3,3,2,7,4 },
|
---|
140 | {1170419,10,10,10,8,2,10,4,1,1,4 },
|
---|
141 | {1170420,1,6,8,10,8,10,5,7,1,4 },
|
---|
142 | {1171710,1,1,1,1,2,1,2,3,1,2 },
|
---|
143 | {1171710,6,5,4,4,3,9,7,8,3,4 },
|
---|
144 | {1171795,1,3,1,2,2,2,5,3,2,2 },
|
---|
145 | {1171845,8,6,4,3,5,9,3,1,1,4 },
|
---|
146 | {1172152,10,3,3,10,2,10,7,3,3,4 },
|
---|
147 | {1173216,10,10,10,3,10,8,8,1,1,4 },
|
---|
148 | {1173235,3,3,2,1,2,3,3,1,1,2 },
|
---|
149 | {1173347,1,1,1,1,2,5,1,1,1,2 },
|
---|
150 | {1173347,8,3,3,1,2,2,3,2,1,2 },
|
---|
151 | {1173509,4,5,5,10,4,10,7,5,8,4 },
|
---|
152 | {1173514,1,1,1,1,4,3,1,1,1,2 },
|
---|
153 | {1173681,3,2,1,1,2,2,3,1,1,2 },
|
---|
154 | {1174057,1,1,2,2,2,1,3,1,1,2 },
|
---|
155 | {1174057,4,2,1,1,2,2,3,1,1,2 },
|
---|
156 | {1174131,10,10,10,2,10,10,5,3,3,4 },
|
---|
157 | {1174428,5,3,5,1,8,10,5,3,1,4 },
|
---|
158 | {1175937,5,4,6,7,9,7,8,10,1,4 },
|
---|
159 | {1176406,1,1,1,1,2,1,2,1,1,2 },
|
---|
160 | {1176881,7,5,3,7,4,10,7,5,5,4 }
|
---|
161 | };
|
---|
162 | private static Dataset defaultDataset;
|
---|
163 | private static IEnumerable<string> defaultAllowedInputVariables;
|
---|
164 | private static string defaultTargetVariable;
|
---|
165 | static ClassificationProblemData() {
|
---|
166 | defaultDataset = new Dataset(defaultVariableNames, defaultData);
|
---|
167 | defaultDataset.Name = "Wisconsin classification problem";
|
---|
168 | defaultDataset.Description = "subset from to ..";
|
---|
169 |
|
---|
170 | defaultAllowedInputVariables = defaultVariableNames.Except(new List<string>() { "sample", "class" });
|
---|
171 | defaultTargetVariable = "class";
|
---|
172 | }
|
---|
173 | #endregion
|
---|
174 |
|
---|
175 | #region propeties
|
---|
176 | [Storable]
|
---|
177 | private string targetVariable;
|
---|
178 | public string TargetVariable {
|
---|
179 | get { return targetVariable; }
|
---|
180 | set {
|
---|
181 | if (!Dataset.VariableNames.Contains(value))
|
---|
182 | throw new ArgumentException(string.Format("The target variable {0} is not present in the dataset", value));
|
---|
183 | if (targetVariable != value) {
|
---|
184 | targetVariable = value;
|
---|
185 |
|
---|
186 | InitializeTargetVariableDependentMembers();
|
---|
187 | OnChanged();
|
---|
188 | }
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | private List<double> classValues;
|
---|
193 | [Storable]
|
---|
194 | public IEnumerable<double> ClassValues {
|
---|
195 | get { return classValues; }
|
---|
196 | private set { classValues = new List<double>(value); }
|
---|
197 | }
|
---|
198 |
|
---|
199 | public int Classes {
|
---|
200 | get { return classValues.Count; }
|
---|
201 | }
|
---|
202 |
|
---|
203 | private Dictionary<double, string> classValueClassNameMapping;
|
---|
204 | private Dictionary<string, double> classNameClassValueMapping;
|
---|
205 | [Storable(Name = "ClassNames")]
|
---|
206 | private IEnumerable<KeyValuePair<double, string>> classValueClassNameMappingStorable {
|
---|
207 | get { return classValueClassNameMapping; }
|
---|
208 | set {
|
---|
209 | classValueClassNameMapping = new Dictionary<double, string>();
|
---|
210 | foreach (var mapping in value)
|
---|
211 | classValueClassNameMapping[mapping.Key] = mapping.Value;
|
---|
212 |
|
---|
213 | classNameClassValueMapping = classValueClassNameMapping.ToDictionary(x => x.Value, x => x.Key);
|
---|
214 | }
|
---|
215 | }
|
---|
216 | public IEnumerable<string> ClassNames {
|
---|
217 | get { return classValueClassNameMapping.Values; }
|
---|
218 | }
|
---|
219 |
|
---|
220 | [Storable]
|
---|
221 | private Dictionary<Tuple<double, double>, double> misclassificationMatrix;
|
---|
222 | #endregion
|
---|
223 |
|
---|
224 |
|
---|
225 | [StorableConstructor]
|
---|
226 | protected ClassificationProblemData(bool deserializing) : base(deserializing) { }
|
---|
227 | protected ClassificationProblemData(ClassificationProblemData original, Cloner cloner) : base(original, cloner) { }
|
---|
228 | public override IDeepCloneable Clone(Cloner cloner) { return new ClassificationProblemData(this, cloner); }
|
---|
229 |
|
---|
230 | public ClassificationProblemData()
|
---|
231 | : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) {
|
---|
232 | }
|
---|
233 |
|
---|
234 | public ClassificationProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)
|
---|
235 | : base(dataset, allowedInputVariables) {
|
---|
236 | TargetVariable = targetVariable;
|
---|
237 |
|
---|
238 | InitializeTargetVariableDependentMembers();
|
---|
239 | }
|
---|
240 |
|
---|
241 | private void InitializeTargetVariableDependentMembers() {
|
---|
242 | classValues = Dataset.GetEnumeratedVariableValues(targetVariable).Distinct().ToList();
|
---|
243 | classValues.Sort();
|
---|
244 |
|
---|
245 | classValueClassNameMapping = new Dictionary<double, string>();
|
---|
246 | foreach (double classValue in ClassValues) {
|
---|
247 | classValueClassNameMapping.Add(classValue, "Class " + classValue);
|
---|
248 | }
|
---|
249 | classNameClassValueMapping = classValueClassNameMapping.ToDictionary(x => x.Value, x => x.Key);
|
---|
250 | OnClassNamesChanged();
|
---|
251 |
|
---|
252 | //set default misclassification matrix
|
---|
253 | misclassificationMatrix = new Dictionary<Tuple<double, double>, double>();
|
---|
254 | foreach (double classValue1 in ClassValues) {
|
---|
255 | foreach (double classValue2 in ClassValues) {
|
---|
256 | double penalty = 1.0;
|
---|
257 | if (classValue1 == classValue2) penalty = 0.0;
|
---|
258 | SetClassificationPenalty(classValue1, classValue1, penalty);
|
---|
259 | }
|
---|
260 | }
|
---|
261 | OnClassificationPenaltyChanged();
|
---|
262 | }
|
---|
263 |
|
---|
264 | public string GetClassName(double classValue) {
|
---|
265 | return classValueClassNameMapping[classValue];
|
---|
266 | }
|
---|
267 | public double GetClassValue(string className) {
|
---|
268 | return classNameClassValueMapping[className];
|
---|
269 | }
|
---|
270 | public void SetClassName(double classValue, string className) {
|
---|
271 | if (!classValues.Contains(classValue)) throw new ArgumentException();
|
---|
272 | if (classValueClassNameMapping[classValue] != className) {
|
---|
273 | classValueClassNameMapping[classValue] = className;
|
---|
274 | classNameClassValueMapping = classValueClassNameMapping.ToDictionary(x => x.Value, x => x.Key);
|
---|
275 | }
|
---|
276 | }
|
---|
277 |
|
---|
278 | public double GetClassificationPenalty(string correctClassName, string estimatedClassName) {
|
---|
279 | return GetClassificationPenalty(GetClassValue(correctClassName), GetClassValue(estimatedClassName));
|
---|
280 | }
|
---|
281 | public double GetClassificationPenalty(double correctClassValue, double estimatedClassValue) {
|
---|
282 | var key = Tuple.Create(correctClassValue, estimatedClassValue);
|
---|
283 | return misclassificationMatrix[key];
|
---|
284 | }
|
---|
285 | public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) {
|
---|
286 | SetClassificationPenalty(GetClassValue(correctClassName), GetClassValue(estimatedClassName), penalty);
|
---|
287 | }
|
---|
288 | public void SetClassificationPenalty(double correctClassValue, double estimatedClassValue, double penalty) {
|
---|
289 | var key = Tuple.Create(correctClassValue, estimatedClassValue);
|
---|
290 | if (!misclassificationMatrix.ContainsKey(key) || misclassificationMatrix[key] != penalty) {
|
---|
291 | misclassificationMatrix[key] = penalty;
|
---|
292 | OnClassificationPenaltyChanged();
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 | public event EventHandler ClassNamesChanged;
|
---|
297 | protected virtual void OnClassNamesChanged() {
|
---|
298 | var handler = ClassNamesChanged;
|
---|
299 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
300 | }
|
---|
301 | public event EventHandler ClassificationPenaltyChanged;
|
---|
302 | protected virtual void OnClassificationPenaltyChanged() {
|
---|
303 | var handler = ClassificationPenaltyChanged;
|
---|
304 | if (handler != null) handler(this, EventArgs.Empty);
|
---|
305 | }
|
---|
306 | }
|
---|
307 | }
|
---|