Free cookie consent management tool by TermsFeed Policy Generator

Changeset 196 for trunk/sources


Ignore:
Timestamp:
04/25/08 16:09:38 (17 years ago)
Author:
gkronber
Message:

fixed caching of mean and range in dataset for another nice speed improvement in GP evaluation.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.DataAnalysis/Dataset.cs

    r195 r196  
    3939    private double[] samples;
    4040    private int rows;
     41    Dictionary<int, Dictionary<int, double>>[] cachedMeans;
     42    Dictionary<int, Dictionary<int, double>>[] cachedRanges;
    4143
    4244    public int Rows {
     
    5052      set { columns = value; }
    5153    }
    52     private Dictionary<int, double[]>[] ranges;
    53     private Dictionary<int, double[]>[] means;
    5454
    5555    public double GetValue(int i, int j) {
     
    9494      // keep a means and ranges dictionary for each column (possible target variable) of the dataset.
    9595
    96       means = new Dictionary<int, double[]>[columns];
    97       ranges = new Dictionary<int, double[]>[columns];
     96      cachedMeans = new Dictionary<int, Dictionary<int, double>>[columns];
     97      cachedRanges = new Dictionary<int, Dictionary<int, double>>[columns];
    9898
    9999      for(int i = 0; i < columns; i++) {
    100         means[i] = new Dictionary<int, double[]>();
    101         ranges[i] = new Dictionary<int, double[]>();
     100        cachedMeans[i] = new Dictionary<int, Dictionary<int, double>>();
     101        cachedRanges[i] = new Dictionary<int, Dictionary<int, double>>();
    102102      }
    103103    }
     
    201201
    202202    public double GetMean(int column, int from, int to) {
    203       double[] values = new double[to - from + 1];
    204       for(int sample = from; sample <= to; sample++) {
    205         values[sample - from] = GetValue(sample, column);
    206       }
    207 
    208       return Statistics.Mean(values);
     203      if(!cachedMeans[column].ContainsKey(from) || !cachedMeans[column][from].ContainsKey(to)) {
     204        double[] values = new double[to - from + 1];
     205        for(int sample = from; sample <= to; sample++) {
     206          values[sample - from] = GetValue(sample, column);
     207        }
     208        double mean = Statistics.Mean(values);
     209        if(!cachedMeans[column].ContainsKey(from)) cachedMeans[column][from] = new Dictionary<int, double>();
     210        cachedMeans[column][from][to] = mean;
     211        return mean;
     212      } else {
     213        return cachedMeans[column][from][to];
     214      }
    209215    }
    210216
     
    214220
    215221    public double GetRange(int column, int from, int to) {
    216       double[] values = new double[to - from + 1];
    217       for(int sample = from; sample <= to; sample++) {
    218         values[sample - from] = GetValue(sample, column);
    219       }
    220 
    221       return Statistics.Range(values);
     222      if(!cachedRanges[column].ContainsKey(from) || !cachedRanges[column][from].ContainsKey(to)) {
     223        double[] values = new double[to - from + 1];
     224        for(int sample = from; sample <= to; sample++) {
     225          values[sample - from] = GetValue(sample, column);
     226        }
     227        double range = Statistics.Range(values);
     228        if(!cachedRanges[column].ContainsKey(from)) cachedRanges[column][from] = new Dictionary<int, double>();
     229        cachedRanges[column][from][to] = range;
     230        return range;
     231      } else {
     232        return cachedRanges[column][from][to];
     233      }
    222234    }
    223235  }
Note: See TracChangeset for help on using the changeset viewer.