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