Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ProblemRefactoring/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaModel.cs @ 14728

Last change on this file since 14728 was 12509, checked in by mkommend, 10 years ago

#2276: Reintegrated branch for dataset refactoring.

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  [Item("NCA Model", "")]
31  [StorableClass]
32  public class NcaModel : NamedItem, INcaModel {
33
34    [Storable]
35    private double[,] transformationMatrix;
36    public double[,] TransformationMatrix {
37      get { return (double[,])transformationMatrix.Clone(); }
38    }
39    [Storable]
40    private string[] allowedInputVariables;
41    [Storable]
42    private string targetVariable;
43    [Storable]
44    private INearestNeighbourModel nnModel;
45    [Storable]
46    private double[] classValues;
47
48    [StorableConstructor]
49    protected NcaModel(bool deserializing) : base(deserializing) { }
50    protected NcaModel(NcaModel original, Cloner cloner)
51      : base(original, cloner) {
52      this.transformationMatrix = (double[,])original.transformationMatrix.Clone();
53      this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
54      this.targetVariable = original.targetVariable;
55      this.nnModel = cloner.Clone(original.nnModel);
56      this.classValues = (double[])original.classValues.Clone();
57    }
58    public NcaModel(int k, double[,] transformationMatrix, IDataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues) {
59      Name = ItemName;
60      Description = ItemDescription;
61      this.transformationMatrix = (double[,])transformationMatrix.Clone();
62      this.allowedInputVariables = allowedInputVariables.ToArray();
63      this.targetVariable = targetVariable;
64      this.classValues = (double[])classValues.Clone();
65
66      var ds = ReduceDataset(dataset, rows);
67      nnModel = new NearestNeighbourModel(ds, Enumerable.Range(0, ds.Rows), k, ds.VariableNames.Last(), ds.VariableNames.Take(transformationMatrix.GetLength(1)), classValues);
68    }
69
70    public override IDeepCloneable Clone(Cloner cloner) {
71      return new NcaModel(this, cloner);
72    }
73
74    public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
75      var ds = ReduceDataset(dataset, rows);
76      return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows));
77    }
78
79    public INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
80      return new NcaClassificationSolution(new ClassificationProblemData(problemData), this);
81    }
82
83    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
84      return CreateClassificationSolution(problemData);
85    }
86
87    public double[,] Reduce(IDataset dataset, IEnumerable<int> rows) {
88      var data = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
89
90      var targets = dataset.GetDoubleValues(targetVariable, rows).ToArray();
91      var result = new double[data.GetLength(0), transformationMatrix.GetLength(1) + 1];
92      for (int i = 0; i < data.GetLength(0); i++)
93        for (int j = 0; j < data.GetLength(1); j++) {
94          for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
95            result[i, x] += data[i, j] * transformationMatrix[j, x];
96          }
97          result[i, transformationMatrix.GetLength(1)] = targets[i];
98        }
99      return result;
100    }
101
102    public Dataset ReduceDataset(IDataset dataset, IEnumerable<int> rows) {
103      return new Dataset(Enumerable
104          .Range(0, transformationMatrix.GetLength(1))
105          .Select(x => "X" + x.ToString())
106          .Concat(targetVariable.ToEnumerable()),
107        Reduce(dataset, rows));
108    }
109  }
110}
Note: See TracBrowser for help on using the repository browser.