Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/PreprocessingDataManipulation.cs @ 10204

Last change on this file since 10204 was 10193, checked in by mleitner, 11 years ago

Move attribute replacement methods to separate class.

File size: 3.6 KB
Line 
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5
6namespace HeuristicLab.DataPreprocessing.Implementations
7{
8    class PreprocessingDataManipulation
9    {
10        private IPreprocessingData preprocessingData;
11        private StatisticInfo statisticInfo;
12
13        public PreprocessingDataManipulation(IPreprocessingData _prepocessingData) {
14            preprocessingData = _prepocessingData;
15            statisticInfo = new StatisticInfo(preprocessingData);
16        }
17
18        public void ReplaceIndicesByValue<T>(string variableName, IEnumerable<int> indices, T value)
19        {
20            foreach (int index in indices)
21            {
22                preprocessingData.SetCell<T>(variableName, index, value);
23            }
24        }
25
26        public void ReplaceIndicesByAverageValue(string variableName, IEnumerable<int> indices)
27        {
28            double average = statisticInfo.GetAverage(variableName);
29            ReplaceIndicesByValue<double>(variableName, indices, average);
30        }
31
32        public void ReplaceIndicesByMedianValue(string variableName, IEnumerable<int> indices)
33        {
34            double median = statisticInfo.GetMedian(variableName);
35            ReplaceIndicesByValue<double>(variableName, indices, median);
36        }
37
38        public void ReplaceIndicesByRandomValue(string variableName, IEnumerable<int> indices)
39        {
40            Random r = new Random();
41
42            double max = statisticInfo.GetMax<double>(variableName);
43            double min = statisticInfo.GetMin<double>(variableName);
44            double randMultiplier = (max - min);
45            foreach (int index in indices)
46            {
47                double rand = r.NextDouble() * randMultiplier + min;
48                preprocessingData.SetCell<double>(variableName, index, rand);
49            }
50        }
51
52        public void ReplaceIndicesByLinearInterpolationOfNeighbours(string variableName, IEnumerable<int> indices)
53        {
54            int countValues = preprocessingData.GetValues<double>(variableName).Count();
55            foreach (int index in indices)
56            {
57                // dont replace first or last values
58                if (index > 0 && index < countValues)
59                {
60                    double prev = preprocessingData.GetCell<double>(variableName, index - 1);
61                    double next = preprocessingData.GetCell<double>(variableName, index + 1);
62
63                    double interpolated = (prev + next) / 2;
64
65                    preprocessingData.SetCell<double>(variableName, index, interpolated);
66                }
67            }
68        }
69
70        public void ReplaceIndicesByMostCommonValue(string variableName, IEnumerable<int> indices)
71        {
72            if (preprocessingData.IsType<double>(variableName))
73            {
74                ReplaceIndicesByValue<double>(variableName, indices, statisticInfo.GetMostCommonValue<double>(variableName));
75            }
76            else if (preprocessingData.IsType<string>(variableName))
77            {
78                ReplaceIndicesByValue<string>(variableName, indices, statisticInfo.GetMostCommonValue<string>(variableName));
79            }
80            else if (preprocessingData.IsType<DateTime>(variableName))
81            {
82                ReplaceIndicesByValue<DateTime>(variableName, indices, statisticInfo.GetMostCommonValue<DateTime>(variableName));
83            }
84            else
85            {
86                throw new ArgumentException("column with index: " + variableName + " contains a non supported type.");
87            }
88        }
89    }
90}
Note: See TracBrowser for help on using the repository browser.