Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/05/12 11:58:17 (12 years ago)
Author:
mkommend
Message:

#1081: Merged trunk changes and fixed compilation errors due to the merge.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/HeuristicLab.TimeSeries/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationProblemData.cs

    r8430 r8742  
    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
     
    277273
    278274    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(i, j);
     289        }
     290      }
     291    }
     292
    279293    public ClassificationProblemData(Dataset dataset, IEnumerable<string> allowedInputVariables, string targetVariable)
    280294      : base(dataset, allowedInputVariables) {
     
    310324      DeregisterParameterEvents();
    311325
    312       classNames = null;
    313326      ((IStringConvertibleMatrix)ClassNamesParameter.Value).Columns = 1;
    314       ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValues.Count;
     327      ((IStringConvertibleMatrix)ClassNamesParameter.Value).Rows = ClassValuesCache.Count;
    315328      for (int i = 0; i < Classes; i++)
    316         ClassNamesParameter.Value[i, 0] = "Class " + ClassValues[i];
     329        ClassNamesParameter.Value[i, 0] = "Class " + ClassValuesCache[i];
    317330      ClassNamesParameter.Value.ColumnNames = new List<string>() { "ClassNames" };
    318331      ClassNamesParameter.Value.RowNames = ClassValues.Select(s => "ClassValue: " + s);
    319332
    320       classificationPenaltiesCache.Clear();
    321       ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = false;
    322333      ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Rows = Classes;
    323334      ((IStringConvertibleMatrix)ClassificationPenaltiesParameter.Value).Columns = Classes;
     
    330341        }
    331342      }
    332       ((ValueParameter<DoubleMatrix>)ClassificationPenaltiesParameter).ReactOnValueToStringChangedAndValueItemImageChanged = true;
    333343      RegisterParameterEvents();
    334344    }
    335345
    336346    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];
    340350    }
    341351    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];
    345355    }
    346356    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);
    350359      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
    351361    }
    352362
     
    355365    }
    356366    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];
    364370    }
    365371    public void SetClassificationPenalty(string correctClassName, string estimatedClassName, double penalty) {
     
    367373    }
    368374    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);
    372377
    373378      ClassificationPenaltiesParameter.Value[correctClassIndex, estimatedClassIndex] = penalty;
     
    378383      TargetVariableParameter.ValueChanged += new EventHandler(TargetVariableParameter_ValueChanged);
    379384      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);
    381387      ClassificationPenaltiesParameter.Value.Reset += new EventHandler(Parameter_ValueChanged);
    382       ClassificationPenaltiesParameter.Value.ItemChanged += new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
    383388    }
    384389    private void DeregisterParameterEvents() {
    385390      TargetVariableParameter.ValueChanged -= new EventHandler(TargetVariableParameter_ValueChanged);
    386391      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);
    388394      ClassificationPenaltiesParameter.Value.Reset -= new EventHandler(Parameter_ValueChanged);
    389       ClassificationPenaltiesParameter.Value.ItemChanged -= new EventHandler<EventArgs<int, int>>(MatrixParameter_ItemChanged);
    390395    }
    391396
    392397    private void TargetVariableParameter_ValueChanged(object sender, EventArgs e) {
    393       classValues = null;
     398      classValuesCache = null;
     399      classNamesCache = null;
    394400      ResetTargetVariableDependentMembers();
    395401      OnChanged();
    396402    }
    397403    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);
    401407      OnChanged();
    402408    }
Note: See TracChangeset for help on using the changeset viewer.