Changeset 9363 for branches/OaaS/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs
- Timestamp:
- 04/16/13 13:13:41 (11 years ago)
- Location:
- branches/OaaS
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/OaaS
- Property svn:ignore
-
old new 21 21 protoc.exe 22 22 _ReSharper.HeuristicLab 3.3 Tests 23 Google.ProtocolBuffers-2.4.1.473.dll 23 24 packages
-
- Property svn:mergeinfo changed
- Property svn:ignore
-
branches/OaaS/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
-
branches/OaaS/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs
r8121 r9363 223 223 } 224 224 225 private List<double> classValues ;226 p ublic List<double> ClassValues{225 private List<double> classValuesCache; 226 private List<double> ClassValuesCache { 227 227 get { 228 if (classValues == null) { 229 classValues = Dataset.GetDoubleValues(TargetVariableParameter.Value.Value).Distinct().ToList(); 230 classValues.Sort(); 228 if (classValuesCache == null) { 229 classValuesCache = Dataset.GetDoubleValues(TargetVariableParameter.Value.Value).Distinct().OrderBy(x => x).ToList(); 231 230 } 232 return classValues ;231 return classValuesCache; 233 232 } 234 233 } 235 IEnumerable<double> IClassificationProblemData.ClassValues { 236 get { return ClassValues; } 237 } 238 234 public IEnumerable<double> ClassValues { 235 get { return ClassValuesCache; } 236 } 239 237 public int Classes { 240 get { return ClassValues .Count; }241 } 242 243 private List<string> classNames ;244 p ublic List<string> ClassNames{238 get { return ClassValuesCache.Count; } 239 } 240 241 private List<string> classNamesCache; 242 private List<string> ClassNamesCache { 245 243 get { 246 if (classNames == null) {247 classNames = new List<string>();244 if (classNamesCache == null) { 245 classNamesCache = new List<string>(); 248 246 for (int i = 0; i < ClassNamesParameter.Value.Rows; i++) 249 classNames .Add(ClassNamesParameter.Value[i, 0]);247 classNamesCache.Add(ClassNamesParameter.Value[i, 0]); 250 248 } 251 return classNames ;249 return classNamesCache; 252 250 } 253 251 } 254 IEnumerable<string> IClassificationProblemData.ClassNames { 255 get { return ClassNames; } 256 } 257 258 private Dictionary<Tuple<double, double>, double> classificationPenaltiesCache = new Dictionary<Tuple<double, double>, double>(); 252 public IEnumerable<string> ClassNames { 253 get { return ClassNamesCache; } 254 } 259 255 #endregion 260 256 … … 277 273 278 274 public ClassificationProblemData() : this(defaultDataset, defaultAllowedInputVariables, defaultTargetVariable) { } 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; 282 283 for (int i = 0; i < classificationProblemData.ClassNames.Count(); i++) 284 ClassNamesParameter.Value[i, 0] = classificationProblemData.ClassNames.ElementAt(i); 285 286 for (int i = 0; i < Classes; i++) { 287 for (int j = 0; j < Classes; j++) { 288 ClassificationPenaltiesParameter.Value[i, j] = classificationProblemData.GetClassificationPenalty(ClassValuesCache[i], ClassValuesCache[j]); 289 } 290 } 291 } 292 279 293 public ClassificationProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable) 280 294 : base(dataset, allowedInputVariables) { … … 286 300 Parameters.Add(new FixedValueParameter<DoubleMatrix>(ClassificationPenaltiesParameterName, "")); 287 301 302 RegisterParameterEvents(); 288 303 ResetTargetVariableDependentMembers(); 289 RegisterParameterEvents(); 290 } 291 292 private static IEnumerable<string> CheckVariablesForPossibleTargetVariables(Dataset dataset) { 304 } 305 306 public static IEnumerable<string> CheckVariablesForPossibleTargetVariables(Dataset dataset) { 293 307 int maxSamples = Math.Min(InspectedRowsToDetermineTargets, dataset.Rows); 294 308 var validTargetVariables = (from v in dataset.DoubleVariables … … 310 324 DeregisterParameterEvents(); 311 325 312 classNames = null;313 326 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1; 314 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValues .Count;327 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValuesCache.Count; 315 328 for (int i = 0; i < Classes; i++) 316 ClassNamesParameter.Value[i, 0] = "Class " + ClassValues [i];329 ClassNamesParameter.Value[i, 0] = "Class " + ClassValuesCache[i]; 317 330 ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" }; 318 331 ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s); 319 332 320 classificationPenaltiesCache.Clear();321 ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;322 333 ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes; 323 334 ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes; … … 330 341 } 331 342 } 332 ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = true;333 343 RegisterParameterEvents(); 334 344 } 335 345 336 346 public string GetClassName(double classValue) { 337 if (!ClassValues .Contains(classValue)) throw new ArgumentException();338 int index = ClassValues .IndexOf(classValue);339 return ClassNames [index];347 if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException(); 348 int index = ClassValuesCache.IndexOf(classValue); 349 return ClassNamesCache[index]; 340 350 } 341 351 public double GetClassValue(string className) { 342 if (!ClassNames .Contains(className)) throw new ArgumentException();343 int index = ClassNames .IndexOf(className);344 return ClassValues [index];352 if (!ClassNamesCache.Contains(className)) throw new ArgumentException(); 353 int index = ClassNamesCache.IndexOf(className); 354 return ClassValuesCache[index]; 345 355 } 346 356 public void SetClassName(double classValue, string className) { 347 if (!classValues.Contains(classValue)) throw new ArgumentException(); 348 int index = ClassValues.IndexOf(classValue); 349 ClassNames[index] = className; 357 if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException(); 358 int index = ClassValuesCache.IndexOf(classValue); 350 359 ClassNamesParameter.Value[index, 0] = className; 360 // updating of class names cache is not necessary here as the parameter value fires a changed event which updates the cache 351 361 } 352 362 … … 355 365 } 356 366 public double GetClassificationPenalty(double correctClassValue, double estimatedClassValue) { 357 var key = Tuple.Create(correctClassValue, estimatedClassValue); 358 if (!classificationPenaltiesCache.ContainsKey(key)) { 359 int correctClassIndex = ClassValues.IndexOf(correctClassValue); 360 int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue); 361 classificationPenaltiesCache[key] = ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex]; 362 } 363 return classificationPenaltiesCache[key]; 367 int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue); 368 int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue); 369 return ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex]; 364 370 } 365 371 public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) { … … 367 373 } 368 374 public void SetClassificationPenalty(double correctClassValue, double estimatedClassValue, double penalty) { 369 var key = Tuple.Create(correctClassValue, estimatedClassValue); 370 int correctClassIndex = ClassValues.IndexOf(correctClassValue); 371 int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue); 375 int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue); 376 int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue); 372 377 373 378 ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty; … … 378 383 TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged); 379 384 ClassNamesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged); 380 ClassNamesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged); 385 ClassNamesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(Parameter_ValueChanged); 386 ClassificationPenaltiesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(Parameter_ValueChanged); 381 387 ClassificationPenaltiesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged); 382 ClassificationPenaltiesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);383 388 } 384 389 private void DeregisterParameterEvents() { 385 390 TargetVariableParameter.ValueChanged -= new EventHandler(TargetVariableParameter_ValueChanged); 386 391 ClassNamesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged); 387 ClassNamesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged); 392 ClassNamesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(Parameter_ValueChanged); 393 ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(Parameter_ValueChanged); 388 394 ClassificationPenaltiesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged); 389 ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);390 395 } 391 396 392 397 private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) { 393 classValues = null; 398 classValuesCache = null; 399 classNamesCache = null; 394 400 ResetTargetVariableDependentMembers(); 395 401 OnChanged(); 396 402 } 397 403 private void Parameter_ValueChanged(object sender, EventArgs e) { 398 OnChanged();399 }400 private void MatrixParameter_ItemChanged(object sender, EventArgs<int, int> e) {404 classNamesCache = null; 405 ClassificationPenaltiesParameter.Value.RowNames = ClassNames.Select(name => "Actual " + name); 406 ClassificationPenaltiesParameter.Value.ColumnNames = ClassNames.Select(name => "Estimated " + name); 401 407 OnChanged(); 402 408 }
Note: See TracChangeset
for help on using the changeset viewer.