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