Changeset 8554 for trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification
- Timestamp:
- 09/03/12 13:27:40 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Problems.DataAnalysis
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis
- Property svn:mergeinfo changed
/branches/DatasetFeatureCorrelation/HeuristicLab.Problems.DataAnalysis (added) merged: 8035,8038,8276,8294,8318,8483,8492,8526,8529,8538
- Property svn:mergeinfo changed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs
r8528 r8554 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 … … 319 315 DeregisterParameterEvents(); 320 316 321 classNames = null;322 317 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1; 323 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValues .Count;318 ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValuesCache.Count; 324 319 for (int i = 0; i < Classes; i++) 325 ClassNamesParameter.Value[i, 0] = "Class " + ClassValues [i];320 ClassNamesParameter.Value[i, 0] = "Class " + ClassValuesCache[i]; 326 321 ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" }; 327 322 ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s); 328 323 329 classificationPenaltiesCache.Clear();330 ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;331 324 ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes; 332 325 ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes; … … 339 332 } 340 333 } 341 ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = true;342 334 RegisterParameterEvents(); 343 335 } 344 336 345 337 public string GetClassName(double classValue) { 346 if (!ClassValues .Contains(classValue)) throw new ArgumentException();347 int index = ClassValues .IndexOf(classValue);348 return ClassNames [index];338 if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException(); 339 int index = ClassValuesCache.IndexOf(classValue); 340 return ClassNamesCache[index]; 349 341 } 350 342 public double GetClassValue(string className) { 351 if (!ClassNames .Contains(className)) throw new ArgumentException();352 int index = ClassNames .IndexOf(className);353 return ClassValues [index];343 if (!ClassNamesCache.Contains(className)) throw new ArgumentException(); 344 int index = ClassNamesCache.IndexOf(className); 345 return ClassValuesCache[index]; 354 346 } 355 347 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; 348 if (!ClassValuesCache.Contains(classValue)) throw new ArgumentException(); 349 int index = ClassValuesCache.IndexOf(classValue); 359 350 ClassNamesParameter.Value[index, 0] = className; 351 // updating of class names cache is not necessary here as the parameter value fires a changed event which updates the cache 360 352 } 361 353 … … 364 356 } 365 357 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]; 358 int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue); 359 int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue); 360 return ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex]; 373 361 } 374 362 public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) { … … 376 364 } 377 365 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); 366 int correctClassIndex = ClassValuesCache.IndexOf(correctClassValue); 367 int estimatedClassIndex = ClassValuesCache.IndexOf(estimatedClassValue); 381 368 382 369 ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty; … … 388 375 ClassNamesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged); 389 376 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);392 377 } 393 378 private void DeregisterParameterEvents() { … … 395 380 ClassNamesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged); 396 381 ClassNamesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged); 397 ClassificationPenaltiesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);398 ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);399 382 } 400 383 401 384 private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) { 402 classValues = null; 385 classValuesCache = null; 386 classNamesCache = null; 403 387 ResetTargetVariableDependentMembers(); 404 388 OnChanged(); 405 389 } 406 390 private void Parameter_ValueChanged(object sender, EventArgs e) { 391 classNamesCache = null; 407 392 OnChanged(); 408 393 } 409 394 private void MatrixParameter_ItemChanged(object sender, EventArgs<int, int> e) { 395 classNamesCache = null; 410 396 OnChanged(); 411 397 } -
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ThresholdCalculators/AccuracyMaximizationThresholdCalculator.cs
r8126 r8554 85 85 //all positives 86 86 if (pair.TargetClassValue.IsAlmost(classValues[i - 1])) { 87 if (pair.EstimatedValue > lowerThreshold && pair.EstimatedValue < actualThreshold)87 if (pair.EstimatedValue > lowerThreshold && pair.EstimatedValue <= actualThreshold) 88 88 //true positive 89 classificationScore += problemData.GetClassificationPenalty( classValues[i - 1], classValues[i - 1]);89 classificationScore += problemData.GetClassificationPenalty(pair.TargetClassValue, pair.TargetClassValue); 90 90 else 91 91 //false negative 92 classificationScore += problemData.GetClassificationPenalty( classValues[i], classValues[i - 1]);92 classificationScore += problemData.GetClassificationPenalty(pair.TargetClassValue, classValues[i]); 93 93 } 94 94 //all negatives 95 95 else { 96 if (pair.EstimatedValue > lowerThreshold && pair.EstimatedValue < actualThreshold)96 if (pair.EstimatedValue > lowerThreshold && pair.EstimatedValue <= actualThreshold) 97 97 //false positive 98 classificationScore += problemData.GetClassificationPenalty( classValues[i - 1], classValues[i]);98 classificationScore += problemData.GetClassificationPenalty(pair.TargetClassValue, classValues[i - 1]); 99 99 else 100 100 //true negative, consider only upper class 101 classificationScore += problemData.GetClassificationPenalty( classValues[i], classValues[i]);101 classificationScore += problemData.GetClassificationPenalty(pair.TargetClassValue, pair.TargetClassValue); 102 102 } 103 103 }
Note: See TracChangeset
for help on using the changeset viewer.