[5559] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[17180] | 3 | * Copyright (C) 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;
|
---|
[16565] | 29 | using HEAL.Attic;
|
---|
[5559] | 30 |
|
---|
| 31 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
[16565] | 32 | [StorableType("1C8DCCCF-4E2A-421D-9C61-7C017D584054")]
|
---|
[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";
|
---|
[11766] | 38 | protected const string PositiveClassParameterName = "PositiveClass";
|
---|
[7266] | 39 | protected const int MaximumNumberOfClasses = 100;
|
---|
| 40 | protected const int InspectedRowsToDetermineTargets = 2000;
|
---|
[5601] | 41 |
|
---|
[7134] | 42 | public string Filename { get; set; }
|
---|
| 43 |
|
---|
[5559] | 44 | #region default data
|
---|
| 45 | private static string[] defaultVariableNames = new string[] { "sample", "clump thickness", "cell size", "cell shape", "marginal adhesion", "epithelial cell size", "bare nuclei", "chromatin", "nucleoli", "mitoses", "class" };
|
---|
| 46 | private static double[,] defaultData = new double[,]{
|
---|
| 47 | {1000025,5,1,1,1,2,1,3,1,1,2 },
|
---|
| 48 | {1002945,5,4,4,5,7,10,3,2,1,2 },
|
---|
| 49 | {1015425,3,1,1,1,2,2,3,1,1,2 },
|
---|
| 50 | {1016277,6,8,8,1,3,4,3,7,1,2 },
|
---|
| 51 | {1017023,4,1,1,3,2,1,3,1,1,2 },
|
---|
| 52 | {1017122,8,10,10,8,7,10,9,7,1,4 },
|
---|
| 53 | {1018099,1,1,1,1,2,10,3,1,1,2 },
|
---|
| 54 | {1018561,2,1,2,1,2,1,3,1,1,2 },
|
---|
| 55 | {1033078,2,1,1,1,2,1,1,1,5,2 },
|
---|
| 56 | {1033078,4,2,1,1,2,1,2,1,1,2 },
|
---|
| 57 | {1035283,1,1,1,1,1,1,3,1,1,2 },
|
---|
| 58 | {1036172,2,1,1,1,2,1,2,1,1,2 },
|
---|
| 59 | {1041801,5,3,3,3,2,3,4,4,1,4 },
|
---|
| 60 | {1043999,1,1,1,1,2,3,3,1,1,2 },
|
---|
| 61 | {1044572,8,7,5,10,7,9,5,5,4,4 },
|
---|
| 62 | {1047630,7,4,6,4,6,1,4,3,1,4 },
|
---|
| 63 | {1048672,4,1,1,1,2,1,2,1,1,2 },
|
---|
| 64 | {1049815,4,1,1,1,2,1,3,1,1,2 },
|
---|
| 65 | {1050670,10,7,7,6,4,10,4,1,2,4 },
|
---|
| 66 | {1050718,6,1,1,1,2,1,3,1,1,2 },
|
---|
| 67 | {1054590,7,3,2,10,5,10,5,4,4,4 },
|
---|
| 68 | {1054593,10,5,5,3,6,7,7,10,1,4 },
|
---|
| 69 | {1056784,3,1,1,1,2,1,2,1,1,2 },
|
---|
| 70 | {1057013,8,4,5,1,2,2,7,3,1,4 },
|
---|
| 71 | {1059552,1,1,1,1,2,1,3,1,1,2 },
|
---|
| 72 | {1065726,5,2,3,4,2,7,3,6,1,4 },
|
---|
| 73 | {1066373,3,2,1,1,1,1,2,1,1,2 },
|
---|
| 74 | {1066979,5,1,1,1,2,1,2,1,1,2 },
|
---|
| 75 | {1067444,2,1,1,1,2,1,2,1,1,2 },
|
---|
| 76 | {1070935,1,1,3,1,2,1,1,1,1,2 },
|
---|
| 77 | {1070935,3,1,1,1,1,1,2,1,1,2 },
|
---|
| 78 | {1071760,2,1,1,1,2,1,3,1,1,2 },
|
---|
| 79 | {1072179,10,7,7,3,8,5,7,4,3,4 },
|
---|
| 80 | {1074610,2,1,1,2,2,1,3,1,1,2 },
|
---|
| 81 | {1075123,3,1,2,1,2,1,2,1,1,2 },
|
---|
| 82 | {1079304,2,1,1,1,2,1,2,1,1,2 },
|
---|
| 83 | {1080185,10,10,10,8,6,1,8,9,1,4 },
|
---|
| 84 | {1081791,6,2,1,1,1,1,7,1,1,2 },
|
---|
| 85 | {1084584,5,4,4,9,2,10,5,6,1,4 },
|
---|
| 86 | {1091262,2,5,3,3,6,7,7,5,1,4 },
|
---|
| 87 | {1096800,6,6,6,9,6,4,7,8,1,2 },
|
---|
| 88 | {1099510,10,4,3,1,3,3,6,5,2,4 },
|
---|
| 89 | {1100524,6,10,10,2,8,10,7,3,3,4 },
|
---|
| 90 | {1102573,5,6,5,6,10,1,3,1,1,4 },
|
---|
| 91 | {1103608,10,10,10,4,8,1,8,10,1,4 },
|
---|
| 92 | {1103722,1,1,1,1,2,1,2,1,2,2 },
|
---|
| 93 | {1105257,3,7,7,4,4,9,4,8,1,4 },
|
---|
| 94 | {1105524,1,1,1,1,2,1,2,1,1,2 },
|
---|
| 95 | {1106095,4,1,1,3,2,1,3,1,1,2 },
|
---|
| 96 | {1106829,7,8,7,2,4,8,3,8,2,4 },
|
---|
| 97 | {1108370,9,5,8,1,2,3,2,1,5,4 },
|
---|
| 98 | {1108449,5,3,3,4,2,4,3,4,1,4 },
|
---|
| 99 | {1110102,10,3,6,2,3,5,4,10,2,4 },
|
---|
| 100 | {1110503,5,5,5,8,10,8,7,3,7,4 },
|
---|
| 101 | {1110524,10,5,5,6,8,8,7,1,1,4 },
|
---|
| 102 | {1111249,10,6,6,3,4,5,3,6,1,4 },
|
---|
| 103 | {1112209,8,10,10,1,3,6,3,9,1,4 },
|
---|
| 104 | {1113038,8,2,4,1,5,1,5,4,4,4 },
|
---|
| 105 | {1113483,5,2,3,1,6,10,5,1,1,4 },
|
---|
| 106 | {1113906,9,5,5,2,2,2,5,1,1,4 },
|
---|
| 107 | {1115282,5,3,5,5,3,3,4,10,1,4 },
|
---|
| 108 | {1115293,1,1,1,1,2,2,2,1,1,2 },
|
---|
| 109 | {1116116,9,10,10,1,10,8,3,3,1,4 },
|
---|
| 110 | {1116132,6,3,4,1,5,2,3,9,1,4 },
|
---|
| 111 | {1116192,1,1,1,1,2,1,2,1,1,2 },
|
---|
| 112 | {1116998,10,4,2,1,3,2,4,3,10,4 },
|
---|
| 113 | {1117152,4,1,1,1,2,1,3,1,1,2 },
|
---|
| 114 | {1118039,5,3,4,1,8,10,4,9,1,4 },
|
---|
| 115 | {1120559,8,3,8,3,4,9,8,9,8,4 },
|
---|
| 116 | {1121732,1,1,1,1,2,1,3,2,1,2 },
|
---|
| 117 | {1121919,5,1,3,1,2,1,2,1,1,2 },
|
---|
| 118 | {1123061,6,10,2,8,10,2,7,8,10,4 },
|
---|
| 119 | {1124651,1,3,3,2,2,1,7,2,1,2 },
|
---|
| 120 | {1125035,9,4,5,10,6,10,4,8,1,4 },
|
---|
| 121 | {1126417,10,6,4,1,3,4,3,2,3,4 },
|
---|
| 122 | {1131294,1,1,2,1,2,2,4,2,1,2 },
|
---|
| 123 | {1132347,1,1,4,1,2,1,2,1,1,2 },
|
---|
| 124 | {1133041,5,3,1,2,2,1,2,1,1,2 },
|
---|
| 125 | {1133136,3,1,1,1,2,3,3,1,1,2 },
|
---|
| 126 | {1136142,2,1,1,1,3,1,2,1,1,2 },
|
---|
| 127 | {1137156,2,2,2,1,1,1,7,1,1,2 },
|
---|
| 128 | {1143978,4,1,1,2,2,1,2,1,1,2 },
|
---|
| 129 | {1143978,5,2,1,1,2,1,3,1,1,2 },
|
---|
| 130 | {1147044,3,1,1,1,2,2,7,1,1,2 },
|
---|
| 131 | {1147699,3,5,7,8,8,9,7,10,7,4 },
|
---|
| 132 | {1147748,5,10,6,1,10,4,4,10,10,4 },
|
---|
| 133 | {1148278,3,3,6,4,5,8,4,4,1,4 },
|
---|
| 134 | {1148873,3,6,6,6,5,10,6,8,3,4 },
|
---|
| 135 | {1152331,4,1,1,1,2,1,3,1,1,2 },
|
---|
| 136 | {1155546,2,1,1,2,3,1,2,1,1,2 },
|
---|
| 137 | {1156272,1,1,1,1,2,1,3,1,1,2 },
|
---|
| 138 | {1156948,3,1,1,2,2,1,1,1,1,2 },
|
---|
| 139 | {1157734,4,1,1,1,2,1,3,1,1,2 },
|
---|
| 140 | {1158247,1,1,1,1,2,1,2,1,1,2 },
|
---|
| 141 | {1160476,2,1,1,1,2,1,3,1,1,2 },
|
---|
| 142 | {1164066,1,1,1,1,2,1,3,1,1,2 },
|
---|
| 143 | {1165297,2,1,1,2,2,1,1,1,1,2 },
|
---|
| 144 | {1165790,5,1,1,1,2,1,3,1,1,2 },
|
---|
| 145 | {1165926,9,6,9,2,10,6,2,9,10,4 },
|
---|
| 146 | {1166630,7,5,6,10,5,10,7,9,4,4 },
|
---|
| 147 | {1166654,10,3,5,1,10,5,3,10,2,4 },
|
---|
| 148 | {1167439,2,3,4,4,2,5,2,5,1,4 },
|
---|
| 149 | {1167471,4,1,2,1,2,1,3,1,1,2 },
|
---|
| 150 | {1168359,8,2,3,1,6,3,7,1,1,4 },
|
---|
| 151 | {1168736,10,10,10,10,10,1,8,8,8,4 },
|
---|
| 152 | {1169049,7,3,4,4,3,3,3,2,7,4 },
|
---|
| 153 | {1170419,10,10,10,8,2,10,4,1,1,4 },
|
---|
| 154 | {1170420,1,6,8,10,8,10,5,7,1,4 },
|
---|
| 155 | {1171710,1,1,1,1,2,1,2,3,1,2 },
|
---|
| 156 | {1171710,6,5,4,4,3,9,7,8,3,4 },
|
---|
| 157 | {1171795,1,3,1,2,2,2,5,3,2,2 },
|
---|
| 158 | {1171845,8,6,4,3,5,9,3,1,1,4 },
|
---|
| 159 | {1172152,10,3,3,10,2,10,7,3,3,4 },
|
---|
| 160 | {1173216,10,10,10,3,10,8,8,1,1,4 },
|
---|
| 161 | {1173235,3,3,2,1,2,3,3,1,1,2 },
|
---|
| 162 | {1173347,1,1,1,1,2,5,1,1,1,2 },
|
---|
| 163 | {1173347,8,3,3,1,2,2,3,2,1,2 },
|
---|
| 164 | {1173509,4,5,5,10,4,10,7,5,8,4 },
|
---|
| 165 | {1173514,1,1,1,1,4,3,1,1,1,2 },
|
---|
| 166 | {1173681,3,2,1,1,2,2,3,1,1,2 },
|
---|
| 167 | {1174057,1,1,2,2,2,1,3,1,1,2 },
|
---|
| 168 | {1174057,4,2,1,1,2,2,3,1,1,2 },
|
---|
| 169 | {1174131,10,10,10,2,10,10,5,3,3,4 },
|
---|
| 170 | {1174428,5,3,5,1,8,10,5,3,1,4 },
|
---|
| 171 | {1175937,5,4,6,7,9,7,8,10,1,4 },
|
---|
| 172 | {1176406,1,1,1,1,2,1,2,1,1,2 },
|
---|
| 173 | {1176881,7,5,3,7,4,10,7,5,5,4 }
|
---|
| 174 | };
|
---|
[6672] | 175 | private static readonly Dataset defaultDataset;
|
---|
| 176 | private static readonly IEnumerable<string> defaultAllowedInputVariables;
|
---|
| 177 | private static readonly string defaultTargetVariable;
|
---|
[6666] | 178 |
|
---|
[6672] | 179 | private static readonly ClassificationProblemData emptyProblemData;
|
---|
[6666] | 180 | public static ClassificationProblemData EmptyProblemData {
|
---|
| 181 | get { return EmptyProblemData; }
|
---|
| 182 | }
|
---|
| 183 |
|
---|
[5559] | 184 | static ClassificationProblemData() {
|
---|
| 185 | defaultDataset = new Dataset(defaultVariableNames, defaultData);
|
---|
| 186 | defaultDataset.Name = "Wisconsin classification problem";
|
---|
| 187 | defaultDataset.Description = "subset from to ..";
|
---|
| 188 |
|
---|
| 189 | defaultAllowedInputVariables = defaultVariableNames.Except(new List<string>() { "sample", "class" });
|
---|
| 190 | defaultTargetVariable = "class";
|
---|
[6666] | 191 |
|
---|
| 192 | var problemData = new ClassificationProblemData();
|
---|
| 193 | problemData.Parameters.Clear();
|
---|
| 194 | problemData.Name = "Empty Classification ProblemData";
|
---|
| 195 | problemData.Description = "This ProblemData acts as place holder before the correct problem data is loaded.";
|
---|
| 196 | problemData.isEmpty = true;
|
---|
| 197 |
|
---|
| 198 | problemData.Parameters.Add(new FixedValueParameter<Dataset>(DatasetParameterName, "", new Dataset()));
|
---|
| 199 | problemData.Parameters.Add(new FixedValueParameter<ReadOnlyCheckedItemList<StringValue>>(InputVariablesParameterName, ""));
|
---|
| 200 | problemData.Parameters.Add(new FixedValueParameter<IntRange>(TrainingPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
|
---|
| 201 | problemData.Parameters.Add(new FixedValueParameter<IntRange>(TestPartitionParameterName, "", (IntRange)new IntRange(0, 0).AsReadOnly()));
|
---|
| 202 | problemData.Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>()));
|
---|
| 203 | problemData.Parameters.Add(new FixedValueParameter<StringMatrix>(ClassNamesParameterName, "", new StringMatrix(0, 0).AsReadOnly()));
|
---|
| 204 | problemData.Parameters.Add(new FixedValueParameter<DoubleMatrix>(ClassificationPenaltiesParameterName, "", (DoubleMatrix)new DoubleMatrix(0, 0).AsReadOnly()));
|
---|
| 205 | emptyProblemData = problemData;
|
---|
[5559] | 206 | }
|
---|
| 207 | #endregion
|
---|
| 208 |
|
---|
[5601] | 209 | #region parameter properties
|
---|
[8121] | 210 | public IConstrainedValueParameter<StringValue> TargetVariableParameter {
|
---|
| 211 | get { return (IConstrainedValueParameter<StringValue>)Parameters[TargetVariableParameterName]; }
|
---|
[5601] | 212 | }
|
---|
| 213 | public IFixedValueParameter<StringMatrix> ClassNamesParameter {
|
---|
| 214 | get { return (IFixedValueParameter<StringMatrix>)Parameters[ClassNamesParameterName]; }
|
---|
| 215 | }
|
---|
[11766] | 216 | public IConstrainedValueParameter<StringValue> PositiveClassParameter {
|
---|
| 217 | get { return (IConstrainedValueParameter<StringValue>)Parameters[PositiveClassParameterName]; }
|
---|
[11763] | 218 | }
|
---|
[5601] | 219 | public IFixedValueParameter<DoubleMatrix> ClassificationPenaltiesParameter {
|
---|
| 220 | get { return (IFixedValueParameter<DoubleMatrix>)Parameters[ClassificationPenaltiesParameterName]; }
|
---|
| 221 | }
|
---|
| 222 | #endregion
|
---|
| 223 |
|
---|
[5649] | 224 | #region properties
|
---|
[5559] | 225 | public string TargetVariable {
|
---|
[5601] | 226 | get { return TargetVariableParameter.Value.Value; }
|
---|
[10540] | 227 | set {
|
---|
| 228 | if (value == null) throw new ArgumentNullException("targetVariable", "The provided value for the targetVariable is null.");
|
---|
| 229 | if (value == TargetVariable) return;
|
---|
| 230 |
|
---|
| 231 |
|
---|
| 232 | var matchingParameterValue = TargetVariableParameter.ValidValues.FirstOrDefault(v => v.Value == value);
|
---|
| 233 | if (matchingParameterValue == null) throw new ArgumentException("The provided value is not valid as the targetVariable.", "targetVariable");
|
---|
| 234 | TargetVariableParameter.Value = matchingParameterValue;
|
---|
| 235 | }
|
---|
[5601] | 236 | }
|
---|
[5559] | 237 |
|
---|
[8554] | 238 | private List<double> classValuesCache;
|
---|
| 239 | private List<double> ClassValuesCache {
|
---|
[5601] | 240 | get {
|
---|
[8554] | 241 | if (classValuesCache == null) {
|
---|
| 242 | classValuesCache = Dataset.GetDoubleValues(TargetVariableParameter.Value.Value).Distinct().OrderBy(x => x).ToList();
|
---|
[5559] | 243 | }
|
---|
[8554] | 244 | return classValuesCache;
|
---|
[5559] | 245 | }
|
---|
| 246 | }
|
---|
[8554] | 247 | public IEnumerable<double> ClassValues {
|
---|
| 248 | get { return ClassValuesCache; }
|
---|
[5559] | 249 | }
|
---|
| 250 | public int Classes {
|
---|
[8554] | 251 | get { return ClassValuesCache.Count; }
|
---|
[5559] | 252 | }
|
---|
| 253 |
|
---|
[8554] | 254 | private List<string> classNamesCache;
|
---|
| 255 | private List<string> ClassNamesCache {
|
---|
[5601] | 256 | get {
|
---|
[8554] | 257 | if (classNamesCache == null) {
|
---|
| 258 | classNamesCache = new List<string>();
|
---|
[5601] | 259 | for (int i = 0; i < ClassNamesParameter.Value.Rows; i++)
|
---|
[8554] | 260 | classNamesCache.Add(ClassNamesParameter.Value[i, 0]);
|
---|
[5601] | 261 | }
|
---|
[8554] | 262 | return classNamesCache;
|
---|
[5559] | 263 | }
|
---|
| 264 | }
|
---|
[8554] | 265 | public IEnumerable<string> ClassNames {
|
---|
| 266 | get { return ClassNamesCache; }
|
---|
[5559] | 267 | }
|
---|
[11763] | 268 |
|
---|
[11766] | 269 | public string PositiveClass {
|
---|
| 270 | get { return PositiveClassParameter.Value.Value; }
|
---|
[11763] | 271 | set {
|
---|
[11766] | 272 | var matchingValue = PositiveClassParameter.ValidValues.SingleOrDefault(x => x.Value == value);
|
---|
[11763] | 273 | if (matchingValue == null) throw new ArgumentException(string.Format("{0} cannot be set as positive class.", value));
|
---|
[11766] | 274 | PositiveClassParameter.Value = matchingValue;
|
---|
[11763] | 275 | }
|
---|
| 276 | }
|
---|
[5559] | 277 | #endregion
|
---|
| 278 |
|
---|
| 279 |
|
---|
| 280 | [StorableConstructor]
|
---|
[16565] | 281 | protected ClassificationProblemData(StorableConstructorFlag _) : base(_) { }
|
---|
[5601] | 282 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 283 | private void AfterDeserialization() {
|
---|
| 284 | RegisterParameterEvents();
|
---|
[13446] | 285 |
|
---|
| 286 | classNamesCache = new List<string>();
|
---|
| 287 | for (int i = 0; i < ClassNamesParameter.Value.Rows; i++)
|
---|
| 288 | classNamesCache.Add(ClassNamesParameter.Value[i, 0]);
|
---|
| 289 |
|
---|
[11763] | 290 | // BackwardsCompatibility3.4
|
---|
| 291 | #region Backwards compatible code, remove with 3.5
|
---|
[11766] | 292 | if (!Parameters.ContainsKey(PositiveClassParameterName)) {
|
---|
[11763] | 293 | var validValues = new ItemSet<StringValue>(ClassNames.Select(s => new StringValue(s).AsReadOnly()));
|
---|
[11766] | 294 | Parameters.Add(new ConstrainedValueParameter<StringValue>(PositiveClassParameterName,
|
---|
[11763] | 295 | "The positive class which is used for quality measure calculation (e.g., specifity, sensitivity,...)", validValues, validValues.First()));
|
---|
| 296 | }
|
---|
| 297 | #endregion
|
---|
| 298 |
|
---|
[5601] | 299 | }
|
---|
[5559] | 300 |
|
---|
[5601] | 301 | protected ClassificationProblemData(ClassificationProblemData original, Cloner cloner)
|
---|
| 302 | : base(original, cloner) {
|
---|
| 303 | RegisterParameterEvents();
|
---|
[13446] | 304 | classNamesCache = new List<string>();
|
---|
| 305 | for (int i = 0; i < ClassNamesParameter.Value.Rows; i++)
|
---|
| 306 | classNamesCache.Add(ClassNamesParameter.Value[i, 0]);
|
---|
[5559] | 307 | }
|
---|
[17845] | 308 |
|
---|
[6666] | 309 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 310 | if (this == emptyProblemData) return emptyProblemData;
|
---|
| 311 | return new ClassificationProblemData(this, cloner);
|
---|
| 312 | }
|
---|
[5559] | 313 |
|
---|
[17835] | 314 | public ClassificationProblemData() : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable, Enumerable.Empty<string>()) { }
|
---|
[8528] | 315 |
|
---|
| 316 | public ClassificationProblemData(IClassificationProblemData classificationProblemData)
|
---|
[17845] | 317 | : this(classificationProblemData, classificationProblemData.Dataset) {
|
---|
| 318 | }
|
---|
| 319 |
|
---|
| 320 | /// <summary>
|
---|
| 321 | /// This method satisfies a common use case: making a copy of the problem but providing a different dataset.
|
---|
| 322 | /// One must be careful here that the dataset passed is not modified, as that would invalidate the problem data internals.
|
---|
| 323 | /// Passing a ModifiableDataset to this constructor is therefore discouraged.
|
---|
| 324 | /// </summary>
|
---|
| 325 | /// <param name="classificationProblemData">The original instance of classification problem data.</param>
|
---|
| 326 | /// <param name="dataset">The new dataset.</param>
|
---|
| 327 | public ClassificationProblemData(IClassificationProblemData classificationProblemData, IDataset dataset)
|
---|
| 328 | : this(classificationProblemData.Dataset, classificationProblemData.AllowedInputVariables, classificationProblemData.TargetVariable, classificationProblemData.ClassNames, classificationProblemData.PositiveClass) {
|
---|
| 329 |
|
---|
[8528] | 330 | TrainingPartition.Start = classificationProblemData.TrainingPartition.Start;
|
---|
| 331 | TrainingPartition.End = classificationProblemData.TrainingPartition.End;
|
---|
| 332 | TestPartition.Start = classificationProblemData.TestPartition.Start;
|
---|
| 333 | TestPartition.End = classificationProblemData.TestPartition.End;
|
---|
[17845] | 334 |
|
---|
[8717] | 335 | for (int i = 0; i < Classes; i++) {
|
---|
| 336 | for (int j = 0; j < Classes; j++) {
|
---|
[8745] | 337 | ClassificationPenaltiesParameter.Value[i, j] = classificationProblemData.GetClassificationPenalty(ClassValuesCache[i], ClassValuesCache[j]);
|
---|
[8717] | 338 | }
|
---|
| 339 | }
|
---|
[8528] | 340 | }
|
---|
| 341 |
|
---|
[17835] | 342 | public ClassificationProblemData(IDataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable,
|
---|
[17845] | 343 | IEnumerable<string> classNames = null,
|
---|
[17835] | 344 | string positiveClass = null, // can be null in which case it's set as the first class name
|
---|
| 345 | IEnumerable<ITransformation> transformations = null)
|
---|
[11114] | 346 | : base(dataset, allowedInputVariables, transformations ?? Enumerable.Empty<ITransformation>()) {
|
---|
[6186] | 347 | var validTargetVariableValues = CheckVariablesForPossibleTargetVariables(dataset).Select(x => new StringValue(x).AsReadOnly()).ToList();
|
---|
| 348 | var target = validTargetVariableValues.Where(x => x.Value == targetVariable).DefaultIfEmpty(validTargetVariableValues.First()).First();
|
---|
| 349 |
|
---|
| 350 | Parameters.Add(new ConstrainedValueParameter<StringValue>(TargetVariableParameterName, new ItemSet<StringValue>(validTargetVariableValues), target));
|
---|
[17835] | 351 | Parameters.Add(new FixedValueParameter<StringMatrix>(ClassNamesParameterName, "", new StringMatrix()));
|
---|
[11766] | 352 | Parameters.Add(new ConstrainedValueParameter<StringValue>(PositiveClassParameterName, "The positive class which is used for quality measure calculation (e.g., specifity, sensitivity,...)"));
|
---|
[5847] | 353 | Parameters.Add(new FixedValueParameter<DoubleMatrix>(ClassificationPenaltiesParameterName, ""));
|
---|
[5559] | 354 |
|
---|
[8802] | 355 | RegisterParameterEvents();
|
---|
[17835] | 356 | ResetTargetVariableDependentMembers(); // correctly set the values of the parameters added above
|
---|
| 357 |
|
---|
| 358 | // set the class names
|
---|
[17845] | 359 | if (classNames != null && classNames.Any()) {
|
---|
[17835] | 360 | // better to allocate lists because we use these multiple times below
|
---|
| 361 | var names = classNames.ToList();
|
---|
[17845] | 362 | var values = ClassValuesCache;
|
---|
[17835] | 363 |
|
---|
| 364 | if (names.Count != values.Count) {
|
---|
| 365 | throw new ArgumentException();
|
---|
| 366 | }
|
---|
| 367 |
|
---|
| 368 | ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1;
|
---|
| 369 | ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = names.Count;
|
---|
| 370 |
|
---|
| 371 | for (int i = 0; i < names.Count; ++i) {
|
---|
| 372 | SetClassName(values[i], names[i]);
|
---|
| 373 | }
|
---|
| 374 | }
|
---|
| 375 |
|
---|
| 376 | // set the positive class value
|
---|
| 377 | if (positiveClass != null) {
|
---|
| 378 | PositiveClass = positiveClass;
|
---|
| 379 | }
|
---|
[5559] | 380 | }
|
---|
| 381 |
|
---|
[12509] | 382 | public static IEnumerable<string> CheckVariablesForPossibleTargetVariables(IDataset dataset) {
|
---|
[6223] | 383 | int maxSamples = Math.Min(InspectedRowsToDetermineTargets, dataset.Rows);
|
---|
[6740] | 384 | var validTargetVariables = (from v in dataset.DoubleVariables
|
---|
| 385 | let distinctValues = dataset.GetDoubleValues(v)
|
---|
[6654] | 386 | .Take(maxSamples)
|
---|
| 387 | .Distinct()
|
---|
| 388 | .Count()
|
---|
[9449] | 389 | where distinctValues <= MaximumNumberOfClasses
|
---|
[6654] | 390 | select v).ToArray();
|
---|
[6186] | 391 |
|
---|
| 392 | if (!validTargetVariables.Any())
|
---|
[6223] | 393 | throw new ArgumentException("Import of classification problem data was not successful, because no target variable was found." +
|
---|
| 394 | " A target variable must have at most " + MaximumNumberOfClasses + " distinct values to be applicable to classification.");
|
---|
[6186] | 395 | return validTargetVariables;
|
---|
| 396 | }
|
---|
| 397 |
|
---|
| 398 |
|
---|
[5601] | 399 | private void ResetTargetVariableDependentMembers() {
|
---|
[6654] | 400 | DeregisterParameterEvents();
|
---|
[5559] | 401 |
|
---|
[5601] | 402 | ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1;
|
---|
[8554] | 403 | ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValuesCache.Count;
|
---|
[5601] | 404 | for (int i = 0; i < Classes; i++)
|
---|
[8554] | 405 | ClassNamesParameter.Value[i, 0] = "Class " + ClassValuesCache[i];
|
---|
[5601] | 406 | ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" };
|
---|
| 407 | ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s);
|
---|
[5559] | 408 |
|
---|
[11766] | 409 | PositiveClassParameter.ValidValues.Clear();
|
---|
[11763] | 410 | foreach (var className in ClassNames) {
|
---|
[11766] | 411 | PositiveClassParameter.ValidValues.Add(new StringValue(className).AsReadOnly());
|
---|
[11763] | 412 | }
|
---|
| 413 |
|
---|
[5601] | 414 | ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes;
|
---|
| 415 | ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes;
|
---|
| 416 | ClassificationPenaltiesParameter.Value.RowNames = ClassNames.Select(name => "Actual " + name);
|
---|
| 417 | ClassificationPenaltiesParameter.Value.ColumnNames = ClassNames.Select(name => "Estimated " + name);
|
---|
| 418 | for (int i = 0; i < Classes; i++) {
|
---|
| 419 | for (int j = 0; j < Classes; j++) {
|
---|
| 420 | if (i != j) ClassificationPenaltiesParameter.Value[i, j] = 1;
|
---|
| 421 | else ClassificationPenaltiesParameter.Value[i, j] = 0;
|
---|
[5559] | 422 | }
|
---|
| 423 | }
|
---|
[5601] | 424 | RegisterParameterEvents();
|
---|
[5559] | 425 | }
|
---|
| 426 |
|
---|
| 427 | public string GetClassName(double classValue) {
|
---|
[8554] | 428 | if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException();
|
---|
| 429 | int index = ClassValuesCache.IndexOf(classValue);
|
---|
| 430 | return ClassNamesCache[index];
|
---|
[5559] | 431 | }
|
---|
| 432 | public double GetClassValue(string className) {
|
---|
[8554] | 433 | if (!ClassNamesCache.Contains(className)) throw new ArgumentException();
|
---|
| 434 | int index = ClassNamesCache.IndexOf(className);
|
---|
| 435 | return ClassValuesCache[index];
|
---|
[5559] | 436 | }
|
---|
| 437 | public void SetClassName(double classValue, string className) {
|
---|
[8554] | 438 | if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException();
|
---|
| 439 | int index = ClassValuesCache.IndexOf(classValue);
|
---|
[5601] | 440 | ClassNamesParameter.Value[index, 0] = className;
|
---|
[8554] | 441 | // updating of class names cache is not necessary here as the parameter value fires a changed event which updates the cache
|
---|
[5559] | 442 | }
|
---|
| 443 |
|
---|
| 444 | public double GetClassificationPenalty(string correctClassName, string estimatedClassName) {
|
---|
| 445 | return GetClassificationPenalty(GetClassValue(correctClassName), GetClassValue(estimatedClassName));
|
---|
| 446 | }
|
---|
| 447 | public double GetClassificationPenalty(double correctClassValue, double estimatedClassValue) {
|
---|
[8554] | 448 | int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue);
|
---|
| 449 | int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue);
|
---|
| 450 | return ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex];
|
---|
[5559] | 451 | }
|
---|
| 452 | public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) {
|
---|
| 453 | SetClassificationPenalty(GetClassValue(correctClassName), GetClassValue(estimatedClassName), penalty);
|
---|
| 454 | }
|
---|
| 455 | public void SetClassificationPenalty(double correctClassValue, double estimatedClassValue, double penalty) {
|
---|
[8554] | 456 | int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue);
|
---|
| 457 | int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue);
|
---|
[5601] | 458 |
|
---|
| 459 | ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty;
|
---|
[5559] | 460 | }
|
---|
| 461 |
|
---|
[5601] | 462 | #region events
|
---|
| 463 | private void RegisterParameterEvents() {
|
---|
| 464 | TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
|
---|
| 465 | ClassNamesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);
|
---|
[8716] | 466 | ClassNamesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(Parameter_ValueChanged);
|
---|
[8717] | 467 | ClassificationPenaltiesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(Parameter_ValueChanged);
|
---|
| 468 | ClassificationPenaltiesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);
|
---|
[5559] | 469 | }
|
---|
[6654] | 470 | private void DeregisterParameterEvents() {
|
---|
[5601] | 471 | TargetVariableParameter.ValueChanged -= new EventHandler(TargetVariableParameter_ValueChanged);
|
---|
| 472 | ClassNamesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);
|
---|
[8716] | 473 | ClassNamesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(Parameter_ValueChanged);
|
---|
[8717] | 474 | ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(Parameter_ValueChanged);
|
---|
| 475 | ClassificationPenaltiesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);
|
---|
[5559] | 476 | }
|
---|
[5601] | 477 |
|
---|
| 478 | private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
|
---|
[8554] | 479 | classValuesCache = null;
|
---|
| 480 | classNamesCache = null;
|
---|
[5601] | 481 | ResetTargetVariableDependentMembers();
|
---|
| 482 | OnChanged();
|
---|
| 483 | }
|
---|
| 484 | private void Parameter_ValueChanged(object sender, EventArgs e) {
|
---|
[11766] | 485 | var oldPositiveClass = PositiveClass;
|
---|
[11763] | 486 | var oldClassNames = classNamesCache;
|
---|
| 487 | var index = oldClassNames.IndexOf(oldPositiveClass);
|
---|
| 488 |
|
---|
[8554] | 489 | classNamesCache = null;
|
---|
[8717] | 490 | ClassificationPenaltiesParameter.Value.RowNames = ClassNames.Select(name => "Actual " + name);
|
---|
| 491 | ClassificationPenaltiesParameter.Value.ColumnNames = ClassNames.Select(name => "Estimated " + name);
|
---|
[11763] | 492 |
|
---|
[11766] | 493 | PositiveClassParameter.ValidValues.Clear();
|
---|
[11763] | 494 | foreach (var className in ClassNames) {
|
---|
[11766] | 495 | PositiveClassParameter.ValidValues.Add(new StringValue(className).AsReadOnly());
|
---|
[11763] | 496 | }
|
---|
[11766] | 497 | PositiveClassParameter.Value = PositiveClassParameter.ValidValues.ElementAt(index);
|
---|
[11763] | 498 |
|
---|
[5601] | 499 | OnChanged();
|
---|
| 500 | }
|
---|
| 501 | #endregion
|
---|
[5559] | 502 | }
|
---|
| 503 | }
|
---|