Changeset 13103


Ignore:
Timestamp:
11/03/15 07:12:27 (4 years ago)
Author:
gkronber
Message:

#1998: code simplification of ConfusionMatrixCalculator

Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/ConfusionMatrixCalculator.cs

    r13102 r13103  
    2727  public class ConfusionMatrixCalculator {
    2828    public static double[,] Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    29       if (originalValues.Count() != estimatedValues.Count()) {
    30         throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
    31       }
    32 
    33       var  classValues = originalValues.Distinct().ToList();
    34       var estimatedClassValues = estimatedValues.Distinct().ToList();
    35 
    36       if (!estimatedClassValues.All(x => classValues.Contains(x))) {
    37         errorState = OnlineCalculatorError.InvalidValueAdded;
    38         return null;
    39       }
    40 
    41       int classes = classValues.Count;
    42       double[,] confusionMatrix = new double[classes, classes];
    4329
    4430      Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
    4531      int index = 0;
    46       foreach (double classValue in classValues.OrderBy(x => x)) {
    47         classValueIndexMapping.Add(classValue, index);
    48         index++;
     32      foreach (double classValue in originalValues.OrderBy(x => x)) {
     33        if (!classValueIndexMapping.ContainsKey(classValue)) {
     34          classValueIndexMapping.Add(classValue, index);
     35          index++;
     36        }
    4937      }
     38
     39      int classes = classValueIndexMapping.Count;
     40      double[,] confusionMatrix = new double[classes, classes];
     41
    5042
    5143      IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
     
    6658      }
    6759
     60      if (originalEnumerator.MoveNext() || estimatedEnumerator.MoveNext()) {
     61        throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
     62
     63      }
     64
    6865      errorState = OnlineCalculatorError.None;
    6966      return confusionMatrix;
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/OnlineCalculators/FOneScoreCalculator.cs

    r13102 r13103  
    2828    public static double Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
    2929      if (originalValues.Distinct().Skip(2).Any()) {
     30        // TODO: we could use ClassificationPerformanceMeasuresCalculator instead of the ConfusionMatrixCalculator below to handle multi-class problems
    3031        throw new ArgumentException("F1 score can only be calculated for binary classification.");
    3132      }
Note: See TracChangeset for help on using the changeset viewer.