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.IO;
|
---|
25 | using System.Linq;
|
---|
26 | using HeuristicLab.Common;
|
---|
27 | using HeuristicLab.Core;
|
---|
28 | using HeuristicLab.Data;
|
---|
29 | using HeuristicLab.Parameters;
|
---|
30 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
31 |
|
---|
32 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
33 | [StorableClass]
|
---|
34 | [Item("ClassificationProblemData", "Represents an item containing all data defining a classification problem.")]
|
---|
35 | public class ClassificationProblemData : DataAnalysisProblemData, IClassificationProblemData {
|
---|
36 | protected const string TargetVariableParameterName = "TargetVariable";
|
---|
37 | protected const string ClassNamesParameterName = "ClassNames";
|
---|
38 | protected const string ClassificationPenaltiesParameterName = "ClassificationPenalties";
|
---|
39 | protected const int MaximumNumberOfClasses = 20;
|
---|
40 | protected const int InspectedRowsToDetermineTargets = 500;
|
---|
41 |
|
---|
42 | #region default data
|
---|
43 | private static string[] defaultVariableNames = new string[] { "sample", "clump thickness", "cell size", "cell shape", "marginal adhesion", "epithelial cell size", "bare nuclei", "chromatin", "nucleoli", "mitoses", "class" };
|
---|
44 | private static double[,] defaultData = new double[,]{
|
---|
45 | {1000025,5,1,1,1,2,1,3,1,1,2 },
|
---|
46 | {1002945,5,4,4,5,7,10,3,2,1,2 },
|
---|
47 | {1015425,3,1,1,1,2,2,3,1,1,2 },
|
---|
48 | {1016277,6,8,8,1,3,4,3,7,1,2 },
|
---|
49 | {1017023,4,1,1,3,2,1,3,1,1,2 },
|
---|
50 | {1017122,8,10,10,8,7,10,9,7,1,4 },
|
---|
51 | {1018099,1,1,1,1,2,10,3,1,1,2 },
|
---|
52 | {1018561,2,1,2,1,2,1,3,1,1,2 },
|
---|
53 | {1033078,2,1,1,1,2,1,1,1,5,2 },
|
---|
54 | {1033078,4,2,1,1,2,1,2,1,1,2 },
|
---|
55 | {1035283,1,1,1,1,1,1,3,1,1,2 },
|
---|
56 | {1036172,2,1,1,1,2,1,2,1,1,2 },
|
---|
57 | {1041801,5,3,3,3,2,3,4,4,1,4 },
|
---|
58 | {1043999,1,1,1,1,2,3,3,1,1,2 },
|
---|
59 | {1044572,8,7,5,10,7,9,5,5,4,4 },
|
---|
60 | {1047630,7,4,6,4,6,1,4,3,1,4 },
|
---|
61 | {1048672,4,1,1,1,2,1,2,1,1,2 },
|
---|
62 | {1049815,4,1,1,1,2,1,3,1,1,2 },
|
---|
63 | {1050670,10,7,7,6,4,10,4,1,2,4 },
|
---|
64 | {1050718,6,1,1,1,2,1,3,1,1,2 },
|
---|
65 | {1054590,7,3,2,10,5,10,5,4,4,4 },
|
---|
66 | {1054593,10,5,5,3,6,7,7,10,1,4 },
|
---|
67 | {1056784,3,1,1,1,2,1,2,1,1,2 },
|
---|
68 | {1057013,8,4,5,1,2,2,7,3,1,4 },
|
---|
69 | {1059552,1,1,1,1,2,1,3,1,1,2 },
|
---|
70 | {1065726,5,2,3,4,2,7,3,6,1,4 },
|
---|
71 | {1066373,3,2,1,1,1,1,2,1,1,2 },
|
---|
72 | {1066979,5,1,1,1,2,1,2,1,1,2 },
|
---|
73 | {1067444,2,1,1,1,2,1,2,1,1,2 },
|
---|
74 | {1070935,1,1,3,1,2,1,1,1,1,2 },
|
---|
75 | {1070935,3,1,1,1,1,1,2,1,1,2 },
|
---|
76 | {1071760,2,1,1,1,2,1,3,1,1,2 },
|
---|
77 | {1072179,10,7,7,3,8,5,7,4,3,4 },
|
---|
78 | {1074610,2,1,1,2,2,1,3,1,1,2 },
|
---|
79 | {1075123,3,1,2,1,2,1,2,1,1,2 },
|
---|
80 | {1079304,2,1,1,1,2,1,2,1,1,2 },
|
---|
81 | {1080185,10,10,10,8,6,1,8,9,1,4 },
|
---|
82 | {1081791,6,2,1,1,1,1,7,1,1,2 },
|
---|
83 | {1084584,5,4,4,9,2,10,5,6,1,4 },
|
---|
84 | {1091262,2,5,3,3,6,7,7,5,1,4 },
|
---|
85 | {1096800,6,6,6,9,6,4,7,8,1,2 },
|
---|
86 | {1099510,10,4,3,1,3,3,6,5,2,4 },
|
---|
87 | {1100524,6,10,10,2,8,10,7,3,3,4 },
|
---|
88 | {1102573,5,6,5,6,10,1,3,1,1,4 },
|
---|
89 | {1103608,10,10,10,4,8,1,8,10,1,4 },
|
---|
90 | {1103722,1,1,1,1,2,1,2,1,2,2 },
|
---|
91 | {1105257,3,7,7,4,4,9,4,8,1,4 },
|
---|
92 | {1105524,1,1,1,1,2,1,2,1,1,2 },
|
---|
93 | {1106095,4,1,1,3,2,1,3,1,1,2 },
|
---|
94 | {1106829,7,8,7,2,4,8,3,8,2,4 },
|
---|
95 | {1108370,9,5,8,1,2,3,2,1,5,4 },
|
---|
96 | {1108449,5,3,3,4,2,4,3,4,1,4 },
|
---|
97 | {1110102,10,3,6,2,3,5,4,10,2,4 },
|
---|
98 | {1110503,5,5,5,8,10,8,7,3,7,4 },
|
---|
99 | {1110524,10,5,5,6,8,8,7,1,1,4 },
|
---|
100 | {1111249,10,6,6,3,4,5,3,6,1,4 },
|
---|
101 | {1112209,8,10,10,1,3,6,3,9,1,4 },
|
---|
102 | {1113038,8,2,4,1,5,1,5,4,4,4 },
|
---|
103 | {1113483,5,2,3,1,6,10,5,1,1,4 },
|
---|
104 | {1113906,9,5,5,2,2,2,5,1,1,4 },
|
---|
105 | {1115282,5,3,5,5,3,3,4,10,1,4 },
|
---|
106 | {1115293,1,1,1,1,2,2,2,1,1,2 },
|
---|
107 | {1116116,9,10,10,1,10,8,3,3,1,4 },
|
---|
108 | {1116132,6,3,4,1,5,2,3,9,1,4 },
|
---|
109 | {1116192,1,1,1,1,2,1,2,1,1,2 },
|
---|
110 | {1116998,10,4,2,1,3,2,4,3,10,4 },
|
---|
111 | {1117152,4,1,1,1,2,1,3,1,1,2 },
|
---|
112 | {1118039,5,3,4,1,8,10,4,9,1,4 },
|
---|
113 | {1120559,8,3,8,3,4,9,8,9,8,4 },
|
---|
114 | {1121732,1,1,1,1,2,1,3,2,1,2 },
|
---|
115 | {1121919,5,1,3,1,2,1,2,1,1,2 },
|
---|
116 | {1123061,6,10,2,8,10,2,7,8,10,4 },
|
---|
117 | {1124651,1,3,3,2,2,1,7,2,1,2 },
|
---|
118 | {1125035,9,4,5,10,6,10,4,8,1,4 },
|
---|
119 | {1126417,10,6,4,1,3,4,3,2,3,4 },
|
---|
120 | {1131294,1,1,2,1,2,2,4,2,1,2 },
|
---|
121 | {1132347,1,1,4,1,2,1,2,1,1,2 },
|
---|
122 | {1133041,5,3,1,2,2,1,2,1,1,2 },
|
---|
123 | {1133136,3,1,1,1,2,3,3,1,1,2 },
|
---|
124 | {1136142,2,1,1,1,3,1,2,1,1,2 },
|
---|
125 | {1137156,2,2,2,1,1,1,7,1,1,2 },
|
---|
126 | {1143978,4,1,1,2,2,1,2,1,1,2 },
|
---|
127 | {1143978,5,2,1,1,2,1,3,1,1,2 },
|
---|
128 | {1147044,3,1,1,1,2,2,7,1,1,2 },
|
---|
129 | {1147699,3,5,7,8,8,9,7,10,7,4 },
|
---|
130 | {1147748,5,10,6,1,10,4,4,10,10,4 },
|
---|
131 | {1148278,3,3,6,4,5,8,4,4,1,4 },
|
---|
132 | {1148873,3,6,6,6,5,10,6,8,3,4 },
|
---|
133 | {1152331,4,1,1,1,2,1,3,1,1,2 },
|
---|
134 | {1155546,2,1,1,2,3,1,2,1,1,2 },
|
---|
135 | {1156272,1,1,1,1,2,1,3,1,1,2 },
|
---|
136 | {1156948,3,1,1,2,2,1,1,1,1,2 },
|
---|
137 | {1157734,4,1,1,1,2,1,3,1,1,2 },
|
---|
138 | {1158247,1,1,1,1,2,1,2,1,1,2 },
|
---|
139 | {1160476,2,1,1,1,2,1,3,1,1,2 },
|
---|
140 | {1164066,1,1,1,1,2,1,3,1,1,2 },
|
---|
141 | {1165297,2,1,1,2,2,1,1,1,1,2 },
|
---|
142 | {1165790,5,1,1,1,2,1,3,1,1,2 },
|
---|
143 | {1165926,9,6,9,2,10,6,2,9,10,4 },
|
---|
144 | {1166630,7,5,6,10,5,10,7,9,4,4 },
|
---|
145 | {1166654,10,3,5,1,10,5,3,10,2,4 },
|
---|
146 | {1167439,2,3,4,4,2,5,2,5,1,4 },
|
---|
147 | {1167471,4,1,2,1,2,1,3,1,1,2 },
|
---|
148 | {1168359,8,2,3,1,6,3,7,1,1,4 },
|
---|
149 | {1168736,10,10,10,10,10,1,8,8,8,4 },
|
---|
150 | {1169049,7,3,4,4,3,3,3,2,7,4 },
|
---|
151 | {1170419,10,10,10,8,2,10,4,1,1,4 },
|
---|
152 | {1170420,1,6,8,10,8,10,5,7,1,4 },
|
---|
153 | {1171710,1,1,1,1,2,1,2,3,1,2 },
|
---|
154 | {1171710,6,5,4,4,3,9,7,8,3,4 },
|
---|
155 | {1171795,1,3,1,2,2,2,5,3,2,2 },
|
---|
156 | {1171845,8,6,4,3,5,9,3,1,1,4 },
|
---|
157 | {1172152,10,3,3,10,2,10,7,3,3,4 },
|
---|
158 | {1173216,10,10,10,3,10,8,8,1,1,4 },
|
---|
159 | {1173235,3,3,2,1,2,3,3,1,1,2 },
|
---|
160 | {1173347,1,1,1,1,2,5,1,1,1,2 },
|
---|
161 | {1173347,8,3,3,1,2,2,3,2,1,2 },
|
---|
162 | {1173509,4,5,5,10,4,10,7,5,8,4 },
|
---|
163 | {1173514,1,1,1,1,4,3,1,1,1,2 },
|
---|
164 | {1173681,3,2,1,1,2,2,3,1,1,2 },
|
---|
165 | {1174057,1,1,2,2,2,1,3,1,1,2 },
|
---|
166 | {1174057,4,2,1,1,2,2,3,1,1,2 },
|
---|
167 | {1174131,10,10,10,2,10,10,5,3,3,4 },
|
---|
168 | {1174428,5,3,5,1,8,10,5,3,1,4 },
|
---|
169 | {1175937,5,4,6,7,9,7,8,10,1,4 },
|
---|
170 | {1176406,1,1,1,1,2,1,2,1,1,2 },
|
---|
171 | {1176881,7,5,3,7,4,10,7,5,5,4 }
|
---|
172 | };
|
---|
173 | private static readonly Dataset defaultDataset;
|
---|
174 | private static readonly IEnumerable<string> defaultAllowedInputVariables;
|
---|
175 | private static readonly string defaultTargetVariable;
|
---|
176 |
|
---|
177 | private static readonly ClassificationProblemData emptyProblemData;
|
---|
178 | public static ClassificationProblemData EmptyProblemData {
|
---|
179 | get { return EmptyProblemData; }
|
---|
180 | }
|
---|
181 |
|
---|
182 | static ClassificationProblemData() {
|
---|
183 | defaultDataset = new Dataset(defaultVariableNames, defaultData);
|
---|
184 | defaultDataset.Name = "Wisconsin classification problem";
|
---|
185 | defaultDataset.Description = "subset from to ..";
|
---|
186 |
|
---|
187 | defaultAllowedInputVariables = defaultVariableNames.Except(new List<string>() { "sample", "class" });
|
---|
188 | defaultTargetVariable = "class";
|
---|
189 |
|
---|
190 | var problemData = new ClassificationProblemData();
|
---|
191 | problemData.Parameters.Clear();
|
---|
192 | problemData.Name = "Empty Classification ProblemData";
|
---|
193 | problemData.Description = "This ProblemData acts as place holder before the correct problem data is loaded.";
|
---|
194 | problemData.isEmpty = true;
|
---|
195 |
|
---|
196 | problemData.Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", new Dataset()));
|
---|
197 | problemData.Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, ""));
|
---|
198 | problemData.Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
|
---|
199 | problemData.Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
|
---|
200 | problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>()));
|
---|
201 | problemData.Parameters.Add(new FixedValueParameter<StringMatrix>(ClassNamesParameterName, "", new StringMatrix(0, 0).AsReadOnly()));
|
---|
202 | problemData.Parameters.Add(new FixedValueParameter<DoubleMatrix>(ClassificationPenaltiesParameterName, "", (DoubleMatrix)new DoubleMatrix(0, 0).AsReadOnly()));
|
---|
203 | emptyProblemData = problemData;
|
---|
204 | }
|
---|
205 | #endregion
|
---|
206 |
|
---|
207 | #region parameter properties
|
---|
208 | public ConstrainedValueParameter<StringValue> TargetVariableParameter {
|
---|
209 | get { return (ConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
|
---|
210 | }
|
---|
211 | public IFixedValueParameter<StringMatrix> ClassNamesParameter {
|
---|
212 | get { return (IFixedValueParameter<StringMatrix>)Parameters[ClassNamesParameterName]; }
|
---|
213 | }
|
---|
214 | public IFixedValueParameter<DoubleMatrix> ClassificationPenaltiesParameter {
|
---|
215 | get { return (IFixedValueParameter<DoubleMatrix>)Parameters[ClassificationPenaltiesParameterName]; }
|
---|
216 | }
|
---|
217 | #endregion
|
---|
218 |
|
---|
219 | #region properties
|
---|
220 | public string TargetVariable {
|
---|
221 | get { return TargetVariableParameter.Value.Value; }
|
---|
222 | }
|
---|
223 |
|
---|
224 | private List<double> classValues;
|
---|
225 | public List<double> ClassValues {
|
---|
226 | get {
|
---|
227 | if (classValues == null) {
|
---|
228 | classValues = Dataset.GetEnumeratedVariableValues(TargetVariableParameter.Value.Value).Distinct().ToList();
|
---|
229 | classValues.Sort();
|
---|
230 | }
|
---|
231 | return classValues;
|
---|
232 | }
|
---|
233 | }
|
---|
234 | IEnumerable<double> IClassificationProblemData.ClassValues {
|
---|
235 | get { return ClassValues; }
|
---|
236 | }
|
---|
237 |
|
---|
238 | public int Classes {
|
---|
239 | get { return ClassValues.Count; }
|
---|
240 | }
|
---|
241 |
|
---|
242 | private List<string> classNames;
|
---|
243 | public List<string> ClassNames {
|
---|
244 | get {
|
---|
245 | if (classNames == null) {
|
---|
246 | classNames = new List<string>();
|
---|
247 | for (int i = 0; i < ClassNamesParameter.Value.Rows; i++)
|
---|
248 | classNames.Add(ClassNamesParameter.Value[i, 0]);
|
---|
249 | }
|
---|
250 | return classNames;
|
---|
251 | }
|
---|
252 | }
|
---|
253 | IEnumerable<string> IClassificationProblemData.ClassNames {
|
---|
254 | get { return ClassNames; }
|
---|
255 | }
|
---|
256 |
|
---|
257 | private Dictionary<Tuple<double, double>, double> classificationPenaltiesCache = new Dictionary<Tuple<double, double>, double>();
|
---|
258 | #endregion
|
---|
259 |
|
---|
260 |
|
---|
261 | [StorableConstructor]
|
---|
262 | protected ClassificationProblemData(bool deserializing) : base(deserializing) { }
|
---|
263 | [StorableHook(HookType.AfterDeserialization)]
|
---|
264 | private void AfterDeserialization() {
|
---|
265 | RegisterParameterEvents();
|
---|
266 | }
|
---|
267 |
|
---|
268 | protected ClassificationProblemData(ClassificationProblemData original, Cloner cloner)
|
---|
269 | : base(original, cloner) {
|
---|
270 | RegisterParameterEvents();
|
---|
271 | }
|
---|
272 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
273 | if (this == emptyProblemData) return emptyProblemData;
|
---|
274 | return new ClassificationProblemData(this, cloner);
|
---|
275 | }
|
---|
276 |
|
---|
277 | public ClassificationProblemData() : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) { }
|
---|
278 | public ClassificationProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)
|
---|
279 | : base(dataset, allowedInputVariables) {
|
---|
280 | var validTargetVariableValues = CheckVariablesForPossibleTargetVariables(dataset).Select(x => new StringValue(x).AsReadOnly()).ToList();
|
---|
281 | var target = validTargetVariableValues.Where(x => x.Value == targetVariable).DefaultIfEmpty(validTargetVariableValues.First()).First();
|
---|
282 |
|
---|
283 | Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(validTargetVariableValues), target));
|
---|
284 | Parameters.Add(new FixedValueParameter<StringMatrix>(ClassNamesParameterName, ""));
|
---|
285 | Parameters.Add(new FixedValueParameter<DoubleMatrix>(ClassificationPenaltiesParameterName, ""));
|
---|
286 |
|
---|
287 | ResetTargetVariableDependentMembers();
|
---|
288 | RegisterParameterEvents();
|
---|
289 | }
|
---|
290 |
|
---|
291 | private static IEnumerable<string> CheckVariablesForPossibleTargetVariables(Dataset dataset) {
|
---|
292 | int maxSamples = Math.Min(InspectedRowsToDetermineTargets, dataset.Rows);
|
---|
293 | var validTargetVariables = (from v in dataset.VariableNames
|
---|
294 | let distinctValues = dataset.GetEnumeratedVariableValues(v)
|
---|
295 | .Take(maxSamples)
|
---|
296 | .Distinct()
|
---|
297 | .Count()
|
---|
298 | where distinctValues < MaximumNumberOfClasses
|
---|
299 | select v).ToArray();
|
---|
300 |
|
---|
301 | if (!validTargetVariables.Any())
|
---|
302 | throw new ArgumentException("Import of classification problem data was not successful, because no target variable was found." +
|
---|
303 | " A target variable must have at most " + MaximumNumberOfClasses + " distinct values to be applicable to classification.");
|
---|
304 | return validTargetVariables;
|
---|
305 | }
|
---|
306 |
|
---|
307 |
|
---|
308 | private void ResetTargetVariableDependentMembers() {
|
---|
309 | DeregisterParameterEvents();
|
---|
310 |
|
---|
311 | classNames = null;
|
---|
312 | ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1;
|
---|
313 | ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValues.Count;
|
---|
314 | for (int i = 0; i < Classes; i++)
|
---|
315 | ClassNamesParameter.Value[i, 0] = "Class " + ClassValues[i];
|
---|
316 | ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" };
|
---|
317 | ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s);
|
---|
318 |
|
---|
319 | classificationPenaltiesCache.Clear();
|
---|
320 | ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
|
---|
321 | ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes;
|
---|
322 | ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes;
|
---|
323 | ClassificationPenaltiesParameter.Value.RowNames = ClassNames.Select(name => "Actual " + name);
|
---|
324 | ClassificationPenaltiesParameter.Value.ColumnNames = ClassNames.Select(name => "Estimated " + name);
|
---|
325 | for (int i = 0; i < Classes; i++) {
|
---|
326 | for (int j = 0; j < Classes; j++) {
|
---|
327 | if (i != j) ClassificationPenaltiesParameter.Value[i, j] = 1;
|
---|
328 | else ClassificationPenaltiesParameter.Value[i, j] = 0;
|
---|
329 | }
|
---|
330 | }
|
---|
331 | ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = true;
|
---|
332 | RegisterParameterEvents();
|
---|
333 | }
|
---|
334 |
|
---|
335 | public string GetClassName(double classValue) {
|
---|
336 | if (!ClassValues.Contains(classValue)) throw new ArgumentException();
|
---|
337 | int index = ClassValues.IndexOf(classValue);
|
---|
338 | return ClassNames[index];
|
---|
339 | }
|
---|
340 | public double GetClassValue(string className) {
|
---|
341 | if (!ClassNames.Contains(className)) throw new ArgumentException();
|
---|
342 | int index = ClassNames.IndexOf(className);
|
---|
343 | return ClassValues[index];
|
---|
344 | }
|
---|
345 | public void SetClassName(double classValue, string className) {
|
---|
346 | if (!classValues.Contains(classValue)) throw new ArgumentException();
|
---|
347 | int index = ClassValues.IndexOf(classValue);
|
---|
348 | ClassNames[index] = className;
|
---|
349 | ClassNamesParameter.Value[index, 0] = className;
|
---|
350 | }
|
---|
351 |
|
---|
352 | public double GetClassificationPenalty(string correctClassName, string estimatedClassName) {
|
---|
353 | return GetClassificationPenalty(GetClassValue(correctClassName), GetClassValue(estimatedClassName));
|
---|
354 | }
|
---|
355 | public double GetClassificationPenalty(double correctClassValue, double estimatedClassValue) {
|
---|
356 | var key = Tuple.Create(correctClassValue, estimatedClassValue);
|
---|
357 | if (!classificationPenaltiesCache.ContainsKey(key)) {
|
---|
358 | int correctClassIndex = ClassValues.IndexOf(correctClassValue);
|
---|
359 | int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue);
|
---|
360 | classificationPenaltiesCache[key] = ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex];
|
---|
361 | }
|
---|
362 | return classificationPenaltiesCache[key];
|
---|
363 | }
|
---|
364 | public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) {
|
---|
365 | SetClassificationPenalty(GetClassValue(correctClassName), GetClassValue(estimatedClassName), penalty);
|
---|
366 | }
|
---|
367 | public void SetClassificationPenalty(double correctClassValue, double estimatedClassValue, double penalty) {
|
---|
368 | var key = Tuple.Create(correctClassValue, estimatedClassValue);
|
---|
369 | int correctClassIndex = ClassValues.IndexOf(correctClassValue);
|
---|
370 | int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue);
|
---|
371 |
|
---|
372 | ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty;
|
---|
373 | }
|
---|
374 |
|
---|
375 | #region events
|
---|
376 | private void RegisterParameterEvents() {
|
---|
377 | TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
|
---|
378 | ClassNamesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);
|
---|
379 | ClassNamesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
|
---|
380 | ClassificationPenaltiesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);
|
---|
381 | ClassificationPenaltiesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
|
---|
382 | }
|
---|
383 | private void DeregisterParameterEvents() {
|
---|
384 | TargetVariableParameter.ValueChanged -= new EventHandler(TargetVariableParameter_ValueChanged);
|
---|
385 | ClassNamesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);
|
---|
386 | ClassNamesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
|
---|
387 | ClassificationPenaltiesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);
|
---|
388 | ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
|
---|
389 | }
|
---|
390 |
|
---|
391 | private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
|
---|
392 | classValues = null;
|
---|
393 | ResetTargetVariableDependentMembers();
|
---|
394 | OnChanged();
|
---|
395 | }
|
---|
396 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
397 | OnChanged();
|
---|
398 | }
|
---|
399 | private void MatrixParameter_ItemChanged(object sender, EventArgs<int, int> e) {
|
---|
400 | OnChanged();
|
---|
401 | }
|
---|
402 | #endregion
|
---|
403 |
|
---|
404 | #region Import from file
|
---|
405 | public static ClassificationProblemData ImportFromFile(string fileName) {
|
---|
406 | TableFileParser csvFileParser = new TableFileParser();
|
---|
407 | csvFileParser.Parse(fileName);
|
---|
408 |
|
---|
409 | Dataset dataset = new Dataset(csvFileParser.VariableNames, csvFileParser.Values);
|
---|
410 | dataset.Name = Path.GetFileName(fileName);
|
---|
411 |
|
---|
412 | ClassificationProblemData problemData = new ClassificationProblemData(dataset, dataset.VariableNames.Skip(1), dataset.VariableNames.First());
|
---|
413 | problemData.Name = "Data imported from " + Path.GetFileName(fileName);
|
---|
414 | return problemData;
|
---|
415 | }
|
---|
416 | #endregion
|
---|
417 | }
|
---|
418 | }
|
---|