Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/10/11 12:38:43 (12 years ago)
Author:
gkronber
Message:

#1418 implemented wrapper for LDA (linear discriminant analysis) implemented in alglib.

File:
1 moved

Legend:

Unmodified
Added
Removed
  • branches/DataAnalysis Refactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/AlglibUtil.cs

    r5642 r5658  
    2525
    2626namespace HeuristicLab.Algorithms.DataAnalysis {
    27   public static class LinearRegressionUtil {
    28     public static double[,] PrepareInputMatrix(Dataset dataset, string targetVariable, IEnumerable<string> allowedInputVariables, int start, int end) {
    29       List<int> allowedRows = CalculateAllowedRows(dataset, targetVariable, allowedInputVariables, start, end);
     27  public static class AlglibUtil {
     28    public static double[,] PrepareInputMatrix(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows) {
     29      List<int> allowedRows = CalculateAllowedRows(dataset, variables, rows).ToList();
    3030
    31       double[,] matrix = new double[allowedRows.Count, allowedInputVariables.Count() + 1];
     31      double[,] matrix = new double[allowedRows.Count, variables.Count()];
    3232      for (int row = 0; row < allowedRows.Count; row++) {
    3333        int col = 0;
    34         foreach (string column in allowedInputVariables) {
     34        foreach (string column in variables) {
    3535          matrix[row, col] = dataset[column, row];
    3636          col++;
    3737        }
    38         matrix[row, allowedInputVariables.Count()] = dataset[targetVariable, row];
    3938      }
    4039      return matrix;
    4140    }
    4241
    43     private static List<int> CalculateAllowedRows(Dataset dataset, string targetVariable, IEnumerable<string> allowedInputVariables, int start, int end) {
    44       List<int> allowedRows = new List<int>();
    45       bool add = false;
    46 
    47       for (int row = start; row < end; row++) {
    48         add = true;
    49         foreach (string column in allowedInputVariables) {
    50           double value = dataset[column, row];
    51           if (double.IsInfinity(value) ||
    52             double.IsNaN(value))
    53             add = false;
    54         }
    55         if (double.IsNaN(dataset[targetVariable, row]))
    56           add = false;
    57         if (add)
    58           allowedRows.Add(row);
    59         add = true;
    60       }
    61       return allowedRows;
     42    private static IEnumerable<int> CalculateAllowedRows(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows) {
     43      // return only rows that contain no infinity or NaN values
     44      return from row in rows
     45             where (from variable in variables
     46                    let x = dataset[variable, row]
     47                    where double.IsInfinity(x) || double.IsNaN(x)
     48                    select 1)
     49                    .Any() == false
     50             select row;
    6251    }
    6352  }
Note: See TracChangeset for help on using the changeset viewer.