Free cookie consent management tool by TermsFeed Policy Generator

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

Last change on this file since 10249 was 10249, checked in by rstoll, 11 years ago
  • Renamed StatisticInfo to StatisticsLogic
  • Fixed todo in PreprocessingDataManipulation
File size: 3.5 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      foreach (var variableName in preprocessingData.VariableNames) {
42        count += GetMissingValueCount(variableName);
43      }
44      return count;
45    }
46
47    public int GetMissingValueCount(string variableName) {
48      return searchLogic.GetMissingValueIndices(variableName).Count();
49    }
50
51    public T GetMin<T>(string variableName) where T : IComparable<T> {
52      return preprocessingData.GetValues<T>(variableName).Min();
53    }
54
55    public T GetMax<T>(string variableName) where T : IComparable<T> {
56      return preprocessingData.GetValues<T>(variableName).Max();
57    }
58
59    public double GetMedian(string variableName) {
60      double median = double.NaN;
61      if (preprocessingData.IsType<double>(variableName)) {
62        median = preprocessingData.GetValues<double>(variableName).Median();
63      }
64      return median;
65    }
66
67    public double GetAverage(string variableName) {
68      double avg = double.NaN;
69      if (preprocessingData.IsType<double>(variableName)) {
70        avg = preprocessingData.GetValues<double>(variableName).Average();
71      }
72      return avg;
73    }
74
75    public T GetMostCommonValue<T>(string variableName) {
76      return preprocessingData.GetValues<T>(variableName)
77                              .GroupBy(x => x)
78                              .OrderByDescending(g => g.Count())
79                              .Select(g => g.Key)
80                              .First();
81    }
82
83
84    public double GetStandardDeviation(string variableName) {
85      double stdDev = double.NaN;
86      if (preprocessingData.IsType<double>(variableName)) {
87        stdDev = preprocessingData.GetValues<double>(variableName).StandardDeviation();
88      }
89      return stdDev;
90    }
91
92    public double GetVariance(string variableName) {
93      double stdDev = double.NaN;
94      if (preprocessingData.IsType<double>(variableName)) {
95        stdDev = preprocessingData.GetValues<double>(variableName).Variance();
96      }
97      return stdDev;
98    }
99
100    public int GetDifferentValuesCount<T>(string variableName) {
101      return preprocessingData.GetValues<T>(variableName).GroupBy(x => x).Count();
102    }
103
104    public int GetRowMissingValueCount(int rowIndex) {
105      int count = 0;
106      foreach (var variableName in preprocessingData.VariableNames) {
107        if (searchLogic.IsMissingValue(variableName, rowIndex)) {
108          ++count;
109        }
110      }
111      return count;
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.