Free cookie consent management tool by TermsFeed Policy Generator

source: branches/TSNE/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/AlglibUtil.cs @ 14836

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

#2700 merged changesets from trunk to branch

File size: 5.3 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;
25using HeuristicLab.Problems.DataAnalysis;
26
27namespace HeuristicLab.Algorithms.DataAnalysis {
28  public static class AlglibUtil {
29    public static double[,] PrepareInputMatrix(IDataset dataset, IEnumerable<string> variables, IEnumerable<int> rows) {
30      // check input variables. Only double variables are allowed.
31      var invalidInputs =
32        variables.Where(name => !dataset.VariableHasType<double>(name));
33      if (invalidInputs.Any())
34        throw new NotSupportedException("Unsupported inputs: " + string.Join(", ", invalidInputs));
35
36      List<int> rowsList = rows.ToList();
37      double[,] matrix = new double[rowsList.Count, variables.Count()];
38
39      int col = 0;
40      foreach (string column in variables) {
41        var values = dataset.GetDoubleValues(column, rows);
42        int row = 0;
43        foreach (var value in values) {
44          matrix[row, col] = value;
45          row++;
46        }
47        col++;
48      }
49
50      return matrix;
51    }
52
53    public static double[,] PrepareAndScaleInputMatrix(IDataset dataset, IEnumerable<string> variables, IEnumerable<int> rows, Scaling scaling) {
54      // check input variables. Only double variables are allowed.
55      var invalidInputs =
56        variables.Where(name => !dataset.VariableHasType<double>(name));
57      if (invalidInputs.Any())
58        throw new NotSupportedException("Unsupported inputs: " + string.Join(", ", invalidInputs));
59
60      List<string> variablesList = variables.ToList();
61      List<int> rowsList = rows.ToList();
62
63      double[,] matrix = new double[rowsList.Count, variablesList.Count];
64
65      int col = 0;
66      foreach (string column in variables) {
67        var values = scaling.GetScaledValues(dataset, column, rows);
68        int row = 0;
69        foreach (var value in values) {
70          matrix[row, col] = value;
71          row++;
72        }
73        col++;
74      }
75
76      return matrix;
77    }
78
79    /// <summary>
80    /// Prepares a binary data matrix from a number of factors and specified factor values
81    /// </summary>
82    /// <param name="dataset">A dataset that contains the variable values</param>
83    /// <param name="factorVariables">An enumerable of categorical variables (factors). For each variable an enumerable of values must be specified.</param>
84    /// <param name="rows">An enumerable of row indices for the dataset</param>
85    /// <returns></returns>
86    /// <remarks>Factor variables (categorical variables) are split up into multiple binary variables one for each specified value.</remarks>
87    public static double[,] PrepareInputMatrix(
88      IDataset dataset,
89      IEnumerable<KeyValuePair<string, IEnumerable<string>>> factorVariables,
90      IEnumerable<int> rows) {
91      // check input variables. Only string variables are allowed.
92      var invalidInputs =
93        factorVariables.Select(kvp => kvp.Key).Where(name => !dataset.VariableHasType<string>(name));
94      if (invalidInputs.Any())
95        throw new NotSupportedException("Unsupported inputs: " + string.Join(", ", invalidInputs));
96
97      int numBinaryColumns = factorVariables.Sum(kvp => kvp.Value.Count());
98
99      List<int> rowsList = rows.ToList();
100      double[,] matrix = new double[rowsList.Count, numBinaryColumns];
101
102      int col = 0;
103      foreach (var kvp in factorVariables) {
104        var varName = kvp.Key;
105        var cats = kvp.Value;
106        if (!cats.Any()) continue;
107        foreach (var cat in cats) {
108          var values = dataset.GetStringValues(varName, rows);
109          int row = 0;
110          foreach (var value in values) {
111            matrix[row, col] = value == cat ? 1 : 0;
112            row++;
113          }
114          col++;
115        }
116      }
117      return matrix;
118    }
119
120    public static IEnumerable<KeyValuePair<string, IEnumerable<string>>> GetFactorVariableValues(IDataset ds, IEnumerable<string> factorVariables, IEnumerable<int> rows) {
121      return from factor in factorVariables
122             let distinctValues = ds.GetStringValues(factor, rows).Distinct().ToArray()
123             // 1 distinct value => skip (constant)
124             // 2 distinct values => only take one of the two values
125             // >=3 distinct values => create a binary value for each value
126             let reducedValues = distinctValues.Length <= 2
127               ? distinctValues.Take(distinctValues.Length - 1)
128               : distinctValues
129             select new KeyValuePair<string, IEnumerable<string>>(factor, reducedValues);
130    }
131  }
132}
Note: See TracBrowser for help on using the repository browser.