Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis/3.4/DatasetExtensions.cs @ 14396

Last change on this file since 14396 was 14393, checked in by gkronber, 7 years ago

#2697:

  • removed AlglibUtil.cs and added an extension method .ToArray(names, rows) to IDataset instead.
  • refactored transformation so that it is possible to apply an transformation without resetting the parameters
  • Used transformations instead of Scaling as far as possible.
  • Moved TakeEvery extension method to HL.Common
File size: 2.2 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2016 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;
23using System.Collections.Generic;
24using System.Linq;
25
26namespace HeuristicLab.Problems.DataAnalysis {
27  public static class DatasetExtensions {
28    public static double[,] ToArray(this IDataset dataset, IEnumerable<string> variables, IEnumerable<int> rows) {
29      return ToArray(dataset,
30        variables,
31        transformations: variables.Select(_ => (ITransformation<double>)null),  // no transform
32        rows: rows);
33    }
34    public static double[,] ToArray(this IDataset dataset, IEnumerable<string> variables, IEnumerable<ITransformation<double>> transformations, IEnumerable<int> rows) {
35      string[] variablesArr = variables.ToArray();
36      int[] rowsArr = rows.ToArray();
37      ITransformation<double>[] transformArr = transformations.ToArray();
38      if (transformArr.Length != variablesArr.Length)
39        throw new ArgumentException("Number of variables and number of transformations must match.");
40
41      double[,] matrix = new double[rowsArr.Length, variablesArr.Length];
42
43      for (int i = 0; i < variablesArr.Length; i++) {
44        var origValues = dataset.GetDoubleValues(variablesArr[i], rowsArr);
45        var values = transformArr[i] != null ? transformArr[i].Apply(origValues) : origValues;
46        int row = 0;
47        foreach (var value in values) {
48          matrix[row, i] = value;
49          row++;
50        }
51      }
52
53      return matrix;
54    }
55  }
56}
Note: See TracBrowser for help on using the repository browser.