Changeset 8811 for branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs
- Timestamp:
- 10/16/12 09:44:07 (12 years ago)
- Location:
- branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
-
branches/ClassificationEnsembleVoting/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs
r8534 r8811 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 … … 284 280 TestPartition.Start = classificationProblemData.TestPartition.Start; 285 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 } 286 291 } 287 292 … … 295 300 Parameters.Add(new FixedValueParameter<DoubleMatrix>(ClassificationPenaltiesParameterName, "")); 296 301 302 RegisterParameterEvents(); 297 303 ResetTargetVariableDependentMembers(); 298 RegisterParameterEvents();299 304 } 300 305 … … 319 324 DeregisterParameterEvents(); 320 325 321 classNames = null;322 326 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1; 323 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValues .Count;327 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValuesCache.Count; 324 328 for (int i = 0; i < Classes; i++) 325 ClassNamesParameter.Value[i, 0] = "Class " + ClassValues [i];329 ClassNamesParameter.Value[i, 0] = "Class " + ClassValuesCache[i]; 326 330 ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" }; 327 331 ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s); 328 332 329 classificationPenaltiesCache.Clear();330 ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;331 333 ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes; 332 334 ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes; … … 339 341 } 340 342 } 341 ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = true;342 343 RegisterParameterEvents(); 343 344 } 344 345 345 346 public string GetClassName(double classValue) { 346 if (!ClassValues .Contains(classValue)) throw new ArgumentException();347 int index = ClassValues .IndexOf(classValue);348 return ClassNames [index];347 if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException(); 348 int index = ClassValuesCache.IndexOf(classValue); 349 return ClassNamesCache[index]; 349 350 } 350 351 public double GetClassValue(string className) { 351 if (!ClassNames .Contains(className)) throw new ArgumentException();352 int index = ClassNames .IndexOf(className);353 return ClassValues [index];352 if (!ClassNamesCache.Contains(className)) throw new ArgumentException(); 353 int index = ClassNamesCache.IndexOf(className); 354 return ClassValuesCache[index]; 354 355 } 355 356 public void SetClassName(double classValue, string className) { 356 if (!classValues.Contains(classValue)) throw new ArgumentException(); 357 int index = ClassValues.IndexOf(classValue); 358 ClassNames[index] = className; 357 if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException(); 358 int index = ClassValuesCache.IndexOf(classValue); 359 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 360 361 } 361 362 … … 364 365 } 365 366 public double GetClassificationPenalty(double correctClassValue, double estimatedClassValue) { 366 var key = Tuple.Create(correctClassValue, estimatedClassValue); 367 if (!classificationPenaltiesCache.ContainsKey(key)) { 368 int correctClassIndex = ClassValues.IndexOf(correctClassValue); 369 int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue); 370 classificationPenaltiesCache[key] = ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex]; 371 } 372 return classificationPenaltiesCache[key]; 367 int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue); 368 int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue); 369 return ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex]; 373 370 } 374 371 public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) { … … 376 373 } 377 374 public void SetClassificationPenalty(double correctClassValue, double estimatedClassValue, double penalty) { 378 var key = Tuple.Create(correctClassValue, estimatedClassValue); 379 int correctClassIndex = ClassValues.IndexOf(correctClassValue); 380 int estimatedClassIndex = ClassValues.IndexOf(estimatedClassValue); 375 int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue); 376 int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue); 381 377 382 378 ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty; … … 387 383 TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged); 388 384 ClassNamesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged); 389 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); 390 387 ClassificationPenaltiesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged); 391 ClassificationPenaltiesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);392 388 } 393 389 private void DeregisterParameterEvents() { 394 390 TargetVariableParameter.ValueChanged -= new EventHandler(TargetVariableParameter_ValueChanged); 395 391 ClassNamesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged); 396 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); 397 394 ClassificationPenaltiesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged); 398 ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);399 395 } 400 396 401 397 private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) { 402 classValues = null; 398 classValuesCache = null; 399 classNamesCache = null; 403 400 ResetTargetVariableDependentMembers(); 404 401 OnChanged(); 405 402 } 406 403 private void Parameter_ValueChanged(object sender, EventArgs e) { 407 OnChanged();408 }409 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); 410 407 OnChanged(); 411 408 }
Note: See TracChangeset
for help on using the changeset viewer.