Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
12/18/13 12:24:53 (11 years ago)
Author:
mleitner
Message:

Fix linear interpolation

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingDataManipulation.cs

    r10218 r10234  
    5959                if (index > 0 && index < countValues)
    6060                {
    61                     double prev = preprocessingData.GetCell<double>(variableName, index - 1);
    62                     double next = preprocessingData.GetCell<double>(variableName, index + 1);
     61                    int prevIndex = indexOfPrevPresentValue(variableName, index);
     62                    int nextIndex = indexOfNextPresentValue(variableName, index);
    6363
    64                     double interpolated = (prev + next) / 2;
     64                    // no neighbours found
     65                    if (prevIndex < 0 && nextIndex >= countValues)
     66                    {
     67                        continue;
     68                    }
     69                    double prev = preprocessingData.GetCell<double>(variableName, prevIndex);
     70                    double next = preprocessingData.GetCell<double>(variableName, nextIndex);
    6571
    66                     preprocessingData.SetCell<double>(variableName, index, interpolated);
     72                    int valuesToInterpolate = nextIndex - prevIndex;
     73
     74                    double interpolationStep = (prev + next) / valuesToInterpolate;
     75
     76                    for (int i = prevIndex; i < nextIndex; ++i) {
     77                        double interpolated = prev + (interpolationStep * (i-prevIndex));
     78                        preprocessingData.SetCell<double>(variableName, i, interpolated);
     79                    }
    6780                }
    6881            }
     82        }
     83
     84        private int indexOfPrevPresentValue(string variableName, int start) {
     85            int offset = start - 1;
     86            while(offset >= 0 && preprocessingData.IsMissingValue(variableName, offset)){
     87                offset--;
     88            }
     89
     90            return offset;
     91        }
     92
     93        private int indexOfNextPresentValue(string variableName, int start)
     94        {
     95            int offset = start + 1;
     96            while (offset < preprocessingData.Rows && preprocessingData.IsMissingValue(variableName,  offset))
     97            {
     98                offset++;
     99            }
     100
     101            return offset;
    69102        }
    70103
     
    96129            // process all given ranges - e.g. TrainingPartition, Trainingpartition
    97130            foreach (IntRange range in ranges) {
    98                 List<int> shuffledIndices = new List<int>();
     131                List<Tuple<int, int>> shuffledIndices = new List<Tuple<int,int>>();
    99132               
    100133                // generate random indices used for shuffeling each column
     
    102135                {
    103136                    int rand = random.Next(range.Start, i);
    104                     shuffledIndices[i] = rand;
     137                    shuffledIndices.Add(new Tuple<int,int>(i,rand));
    105138                }
    106139
     
    123156        }
    124157
    125         public void reOrderToIndices<T>(string variableName, List<int> indices) {
     158        public void reOrderToIndices<T>(string variableName, List<Tuple<int, int>> indices) {
    126159            // process all columns equally
    127             for (int i = 0; i < preprocessingData.Rows; i++)
     160            foreach(Tuple<int, int> index in indices)
    128161            {
    129                 int replaceIndex = indices[i];
     162                int originalIndex = index.Item1;
     163                int replaceIndex = index.Item2;
    130164
    131                 T tmp = preprocessingData.GetCell<T>(variableName, i);
     165                T tmp = preprocessingData.GetCell<T>(variableName, originalIndex);
    132166                T replaceValue = preprocessingData.GetCell<T>(variableName, replaceIndex);
    133167
    134                 preprocessingData.SetCell<T>(variableName, i, replaceValue);
     168                preprocessingData.SetCell<T>(variableName, originalIndex, replaceValue);
    135169                preprocessingData.SetCell<T>(variableName, replaceIndex, tmp);
    136170            }
Note: See TracChangeset for help on using the changeset viewer.