Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/03/12 13:27:40 (12 years ago)
Author:
mkommend
Message:

#1915:

  • Corrected class names and class values caching in ClassificationProblemData
  • Removed caching of classification penalties
  • Corrected AccuracyMaximizationThresholdCalculator (retrieving of penalties was switched)
  • Added asserts for the achieved accuracy to the classification sample unit test
Location:
trunk/sources/HeuristicLab.Problems.DataAnalysis
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis

  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs

    r8528 r8554  
    223223    }
    224224
    225     private List<double> classValues;
    226     public List<double> ClassValues {
     225    private List<double> classValuesCache;
     226    private List<double> ClassValuesCache {
    227227      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();
    231230        }
    232         return classValues;
     231        return classValuesCache;
    233232      }
    234233    }
    235     IEnumerable<double> IClassificationProblemData.ClassValues {
    236       get { return ClassValues; }
    237     }
    238 
     234    public IEnumerable<double> ClassValues {
     235      get { return ClassValuesCache; }
     236    }
    239237    public int Classes {
    240       get { return ClassValues.Count; }
    241     }
    242 
    243     private List<string> classNames;
    244     public List<string> ClassNames {
     238      get { return ClassValuesCache.Count; }
     239    }
     240
     241    private List<string> classNamesCache;
     242    private List<string> ClassNamesCache {
    245243      get {
    246         if (classNames == null) {
    247           classNames = new List<string>();
     244        if (classNamesCache == null) {
     245          classNamesCache = new List<string>();
    248246          for (int i = 0; i < ClassNamesParameter.Value.Rows; i++)
    249             classNames.Add(ClassNamesParameter.Value[i, 0]);
     247            classNamesCache.Add(ClassNamesParameter.Value[i, 0]);
    250248        }
    251         return classNames;
     249        return classNamesCache;
    252250      }
    253251    }
    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    }
    259255    #endregion
    260256
     
    319315      DeregisterParameterEvents();
    320316
    321       classNames = null;
    322317      ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1;
    323       ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValues.Count;
     318      ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValuesCache.Count;
    324319      for (int i = 0; i < Classes; i++)
    325         ClassNamesParameter.Value[i, 0] = "Class " + ClassValues[i];
     320        ClassNamesParameter.Value[i, 0] = "Class " + ClassValuesCache[i];
    326321      ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" };
    327322      ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s);
    328323
    329       classificationPenaltiesCache.Clear();
    330       ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
    331324      ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes;
    332325      ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes;
     
    339332        }
    340333      }
    341       ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = true;
    342334      RegisterParameterEvents();
    343335    }
    344336
    345337    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];
    349341    }
    350342    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];
    354346    }
    355347    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);
    359350      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
    360352    }
    361353
     
    364356    }
    365357    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];
    373361    }
    374362    public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) {
     
    376364    }
    377365    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);
    381368
    382369      ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty;
     
    388375      ClassNamesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);
    389376      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);
    392377    }
    393378    private void DeregisterParameterEvents() {
     
    395380      ClassNamesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);
    396381      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);
    399382    }
    400383
    401384    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
    402       classValues = null;
     385      classValuesCache = null;
     386      classNamesCache = null;
    403387      ResetTargetVariableDependentMembers();
    404388      OnChanged();
    405389    }
    406390    private void Parameter_ValueChanged(object sender, EventArgs e) {
     391      classNamesCache = null;
    407392      OnChanged();
    408393    }
    409394    private void MatrixParameter_ItemChanged(object sender, EventArgs<int, int> e) {
     395      classNamesCache = null;
    410396      OnChanged();
    411397    }
Note: See TracChangeset for help on using the changeset viewer.