Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataPreprocessing/HeuristicLab.DataPreprocessing/3.3/Implementations/StatisticsLogic.cs @ 10367

Last change on this file since 10367 was 10367, checked in by rstoll, 10 years ago
  • modified PreprocessingData, uses columnIndex now instead of variableName (is faster and more convenient), set variabelName based methods to Obsolete
  • Already changed SearchLogic, DataGridLogic, StatisticLogic as well as PreprocessingDataManipulation

*

File size: 3.6 KB
Line 
1using System;
2using System.Linq;
3using HeuristicLab.Common;
4
5namespace HeuristicLab.DataPreprocessing {
6
7  public class StatisticsLogic : IStatisticsLogic {
8
9    private readonly IPreprocessingData preprocessingData;
10    private readonly ISearchLogic searchLogic;
11
12    public StatisticsLogic(IPreprocessingData thePreprocessingData, ISearchLogic theSearchLogic) {
13      preprocessingData = thePreprocessingData;
14      searchLogic = theSearchLogic;
15    }
16
17    public int GetColumnCount() {
18      return preprocessingData.Columns;
19    }
20
21    public int GetRowCount() {
22      return preprocessingData.Rows;
23    }
24
25    public int GetNumericColumnCount() {
26      int count = 0;
27      foreach (var variableName in preprocessingData.VariableNames) {
28        if (preprocessingData.IsType<double>(variableName)) {
29          ++count;
30        }
31      }
32      return count;
33    }
34
35    public int GetNominalColumnCount() {
36      return preprocessingData.Columns - GetNumericColumnCount();
37    }
38
39    public int GetMissingValueCount() {
40      int count = 0;
41      for(int i = 0; i < preprocessingData.Columns; ++i){
42        count += GetMissingValueCount(i);
43      }
44      return count;
45    }
46
47    public int GetMissingValueCount(int columnIndex) {
48      return searchLogic.GetMissingValueIndices(columnIndex).Count();
49    }
50
51    public T GetMin<T>(int columnIndex) where T : IComparable<T> {
52      return preprocessingData.GetValues<T>(columnIndex).Min();
53    }
54
55    public T GetMax<T>(int columnIndex) where T : IComparable<T> {
56      return preprocessingData.GetValues<T>(columnIndex).Max();
57    }
58
59    public double GetMedian(int columnIndex) {
60      double median = double.NaN;
61      if (preprocessingData.IsType<double>(columnIndex)) {
62        median = preprocessingData.GetValues<double>(columnIndex).Median();
63      }
64      return median;
65    }
66
67    public double GetAverage(int columnIndex) {
68      double avg = double.NaN;
69      if (preprocessingData.IsType<double>(columnIndex)) {
70        avg = preprocessingData.GetValues<double>(columnIndex).Average();
71      }
72      return avg;
73    }
74
75    public T GetMostCommonValue<T>(int columnIndex) {
76      return preprocessingData.GetValues<T>(columnIndex)
77                              .GroupBy(x => x)
78                              .OrderByDescending(g => g.Count())
79                              .Select(g => g.Key)
80                              .First();
81    }
82
83
84    public double GetStandardDeviation(int columnIndex) {
85      double stdDev = double.NaN;
86      if (preprocessingData.IsType<double>(columnIndex)) {
87        stdDev = preprocessingData.GetValues<double>(columnIndex).StandardDeviation();
88      }
89      return stdDev;
90    }
91
92    public double GetVariance(int columnIndex) {
93      double stdDev = double.NaN;
94      if (preprocessingData.IsType<double>(columnIndex)) {
95        stdDev = preprocessingData.GetValues<double>(columnIndex).Variance();
96      }
97      return stdDev;
98    }
99
100    public int GetDifferentValuesCount<T>(int columnIndex) {
101      return preprocessingData.GetValues<T>(columnIndex).GroupBy(x => x).Count();
102    }
103
104    public int GetRowMissingValueCount(int rowIndex) {
105      int count = 0;
106       for(int i = 0; i < preprocessingData.Columns; ++i){
107        if (searchLogic.IsMissingValue(i, rowIndex)) {
108          ++count;
109        }
110      }
111      return count;
112    }
113
114
115    public string GetVariableName(int columnIndex) {
116      return preprocessingData.GetVariableName(columnIndex);
117    }
118
119    public bool IsType<T>(int columnIndex) {
120      return preprocessingData.IsType<T>(columnIndex);
121    }
122  }
123}
Note: See TracBrowser for help on using the repository browser.