Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10369 was 10369, checked in by rstoll, 10 years ago
  • Renamed PreprocessingDataManipulation to ManipulationLogic
  • Proceeded with StatisticsView
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
28      for (int i = 0; i < preprocessingData.Columns; ++i) {
29        if (preprocessingData.IsType<double>(i)) {
30          ++count;
31        }
32      }
33      return count;
34    }
35
36    public int GetNominalColumnCount() {
37      return preprocessingData.Columns - GetNumericColumnCount();
38    }
39
40    public int GetMissingValueCount() {
41      int count = 0;
42      for (int i = 0; i < preprocessingData.Columns; ++i) {
43        count += GetMissingValueCount(i);
44      }
45      return count;
46    }
47
48    public int GetMissingValueCount(int columnIndex) {
49      return searchLogic.GetMissingValueIndices(columnIndex).Count();
50    }
51
52    public T GetMin<T>(int columnIndex) where T : IComparable<T> {
53      return preprocessingData.GetValues<T>(columnIndex).Min();
54    }
55
56    public T GetMax<T>(int columnIndex) where T : IComparable<T> {
57      return preprocessingData.GetValues<T>(columnIndex).Max();
58    }
59
60    public double GetMedian(int columnIndex) {
61      double median = double.NaN;
62      if (preprocessingData.IsType<double>(columnIndex)) {
63        median = preprocessingData.GetValues<double>(columnIndex).Median();
64      }
65      return median;
66    }
67
68    public double GetAverage(int columnIndex) {
69      double avg = double.NaN;
70      if (preprocessingData.IsType<double>(columnIndex)) {
71        avg = preprocessingData.GetValues<double>(columnIndex).Average();
72      }
73      return avg;
74    }
75
76    public T GetMostCommonValue<T>(int columnIndex) {
77      return preprocessingData.GetValues<T>(columnIndex)
78                              .GroupBy(x => x)
79                              .OrderByDescending(g => g.Count())
80                              .Select(g => g.Key)
81                              .First();
82    }
83
84
85    public double GetStandardDeviation(int columnIndex) {
86      double stdDev = double.NaN;
87      if (preprocessingData.IsType<double>(columnIndex)) {
88        stdDev = preprocessingData.GetValues<double>(columnIndex).StandardDeviation();
89      }
90      return stdDev;
91    }
92
93    public double GetVariance(int columnIndex) {
94      double stdDev = double.NaN;
95      if (preprocessingData.IsType<double>(columnIndex)) {
96        stdDev = preprocessingData.GetValues<double>(columnIndex).Variance();
97      }
98      return stdDev;
99    }
100
101    public int GetDifferentValuesCount<T>(int columnIndex) {
102      return preprocessingData.GetValues<T>(columnIndex).GroupBy(x => x).Count();
103    }
104
105    public int GetRowMissingValueCount(int rowIndex) {
106      int count = 0;
107      for (int i = 0; i < preprocessingData.Columns; ++i) {
108        if (searchLogic.IsMissingValue(i, rowIndex)) {
109          ++count;
110        }
111      }
112      return count;
113    }
114
115
116    public string GetVariableName(int columnIndex) {
117      return preprocessingData.GetVariableName(columnIndex);
118    }
119
120    public bool IsType<T>(int columnIndex) {
121      return preprocessingData.IsType<T>(columnIndex);
122    }
123  }
124}
Note: See TracBrowser for help on using the repository browser.