[5617] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[7268] | 3 | * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5617] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 25 |
|
---|
| 26 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[5658] | 27 | public static class AlglibUtil {
|
---|
| 28 | public static double[,] PrepareInputMatrix(Dataset dataset, IEnumerable<string> variables, IEnumerable<int> rows) {
|
---|
[7099] | 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();
|
---|
[7097] | 42 | List<int> rowsList = rows.ToList();
|
---|
[7099] | 43 | int nRows = rowsList.Count - maxLag;
|
---|
| 44 | double[,] matrix = new double[nRows, inputVariablesList.Count * lags.Count() + 1];
|
---|
[6802] | 45 |
|
---|
[6740] | 46 | int col = 0;
|
---|
[7099] | 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++;
|
---|
[5617] | 60 | }
|
---|
| 61 | }
|
---|
[7099] | 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 | }
|
---|
[5617] | 68 | return matrix;
|
---|
| 69 | }
|
---|
| 70 | }
|
---|
| 71 | }
|
---|