Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
01/22/14 16:13:42 (11 years ago)
Author:
mleitner
Message:

Add Media, Average, Variance, and StdDeviation support for DateTime

File:
1 edited

Legend:

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

    r10371 r10381  
    22using System.Linq;
    33using HeuristicLab.Common;
     4using System.Collections.Generic;
    45
    56namespace HeuristicLab.DataPreprocessing {
     
    7475    }
    7576
     77    public DateTime GetMedianDateTime(int columnIndex)
     78    {
     79      DateTime median = new DateTime();
     80      if (preprocessingData.IsType<DateTime>(columnIndex))
     81      {
     82          median = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex).Median());
     83      }
     84      return median;
     85    }
     86
     87    public DateTime GetAverageDateTime(int columnIndex)
     88    {
     89      DateTime avg = new DateTime();
     90      if (preprocessingData.IsType<DateTime>(columnIndex))
     91      {
     92          avg = GetSecondsAsDateTime(GetDateTimeAsSeconds(columnIndex).Average());   
     93      }
     94      return avg;
     95    }
     96
    7697    public T GetMostCommonValue<T>(int columnIndex) {
    7798      return preprocessingData.GetValues<T>(columnIndex)
     99
    78100                              .GroupBy(x => x)
    79101                              .OrderByDescending(g => g.Count())
     
    88110        stdDev = preprocessingData.GetValues<double>(columnIndex).StandardDeviation();
    89111      }
     112      else if (preprocessingData.IsType<DateTime>(variableName)) {
     113        stdDev = GetDateTimeAsSeconds(variableName).StandardDeviation();
     114      }
    90115      return stdDev;
    91116    }
    92117
    93118    public double GetVariance(int columnIndex) {
    94       double stdDev = double.NaN;
     119       double variance = double.NaN;
     120
    95121      if (preprocessingData.IsType<double>(columnIndex)) {
    96         stdDev = preprocessingData.GetValues<double>(columnIndex).Variance();
     122        variance = preprocessingData.GetValues<double>(columnIndex).Variance();
    97123      }
    98       return stdDev;
     124      else if (preprocessingData.IsType<DateTime>(variableName)) {
     125        variance = GetDateTimeAsSeconds(variableName).Variance();
     126      }
     127      return variance;
    99128    }
    100129
     
    112141      return count;
    113142    }
    114 
    115143
    116144    public string GetVariableName(int columnIndex) {
     
    133161    }
    134162
     163    private IEnumerable<double> GetDateTimeAsSeconds(int columnIndex)
     164    {
     165        return preprocessingData.GetValues<DateTime>(columnIndex).Select(x => (double)x.Ticks / TimeSpan.TicksPerSecond);
     166    }
     167
     168    private DateTime GetSecondsAsDateTime(double seconds)
     169    {
     170        DateTime dateTime = new DateTime();
     171        return dateTime.Add(new TimeSpan(0, 0, (int)seconds));
     172    }
    135173  }
    136174}
Note: See TracChangeset for help on using the changeset viewer.