Free cookie consent management tool by TermsFeed Policy Generator

source: branches/HeuristicLab.TimeSeries/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/AlglibUtil.cs @ 7461

Last change on this file since 7461 was 7268, checked in by gkronber, 13 years ago

#1081: merged r7214:7266 from trunk into time series branch.

File size: 2.5 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Problems.DataAnalysis;
25
26namespace HeuristicLab.Algorithms.DataAnalysis {
27  public static class AlglibUtil {
28    public static double[,] PrepareInputMatrix(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows) {
29      return PrepareInputMatrix(dataset, variables, rows, new int[] { 0 });
30    }
31
32    public static double[,] PrepareInputMatrix(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows, IEnumerable<int> lags) {
33      int maxLag = lags.Max();
34
35      // drop last variable (target variable)
36      List<string> inputVariablesList = variables
37        .Reverse()
38        .Skip(1)
39        .Reverse()
40        .ToList();
41      string targetVariable = variables.Last();
42      List<int> rowsList = rows.ToList();
43      int nRows = rowsList.Count - maxLag;
44      double[,] matrix = new double[nRows, inputVariablesList.Count * lags.Count() + 1];
45
46      int col = 0;
47      int row = 0;
48      // input variables
49      foreach (int lag in lags) {
50        foreach (string column in inputVariablesList) {
51          var values = dataset.GetDoubleValues(column, rows.Select(x => x - lag).Take(nRows));
52          row = 0;
53          foreach (var value in values) {
54            if (row >= 0) {
55              matrix[row, col] = value;
56            }
57            row++;
58          }
59          col++;
60        }
61      }
62      // target variable
63      row = 0;
64      foreach (var value in dataset.GetDoubleValues(targetVariable, rows).Take(nRows)) {
65        matrix[row, col] = value;
66        row++;
67      }
68      return matrix;
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.