Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaModel.cs @ 9074

Last change on this file since 9074 was 8528, checked in by mkommend, 12 years ago

#1920: Changed CreateSolution method in all DataAnalysisModels to transform the handed IProblemData to the correct type.

File size: 4.7 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.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 Scaling scaling;
36    [Storable]
37    private double[,] transformationMatrix;
38    public double[,] TransformationMatrix {
39      get { return (double[,])transformationMatrix.Clone(); }
40    }
41    [Storable]
42    private string[] allowedInputVariables;
43    [Storable]
44    private string targetVariable;
45    [Storable]
46    private INearestNeighbourModel nnModel;
47    [Storable]
48    private double[] classValues;
49
50    [StorableConstructor]
51    protected NcaModel(bool deserializing) : base(deserializing) { }
52    protected NcaModel(NcaModel original, Cloner cloner)
53      : base(original, cloner) {
54      this.scaling = cloner.Clone(original.scaling);
55      this.transformationMatrix = (double[,])original.transformationMatrix.Clone();
56      this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
57      this.targetVariable = original.targetVariable;
58      this.nnModel = cloner.Clone(original.nnModel);
59      this.classValues = (double[])original.classValues.Clone();
60    }
61    public NcaModel(int k, double[,] transformationMatrix, Dataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, Scaling scaling, double[] classValues) {
62      Name = ItemName;
63      Description = ItemDescription;
64      this.scaling = scaling;
65      this.transformationMatrix = (double[,])transformationMatrix.Clone();
66      this.allowedInputVariables = allowedInputVariables.ToArray();
67      this.targetVariable = targetVariable;
68      this.classValues = (double[])classValues.Clone();
69
70      var ds = ReduceDataset(dataset, rows);
71      nnModel = new NearestNeighbourModel(ds, Enumerable.Range(0, ds.Rows), k, ds.VariableNames.Last(), ds.VariableNames.Take(transformationMatrix.GetLength(1)), classValues);
72    }
73
74    public override IDeepCloneable Clone(Cloner cloner) {
75      return new NcaModel(this, cloner);
76    }
77
78    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
79      var ds = ReduceDataset(dataset, rows);
80      return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows));
81    }
82
83    public INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
84      return new NcaClassificationSolution(new ClassificationProblemData(problemData), this);
85    }
86
87    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
88      return CreateClassificationSolution(problemData);
89    }
90
91    public double[,] Reduce(Dataset dataset, IEnumerable<int> rows) {
92      var scaledData = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, scaling);
93      var targets = dataset.GetDoubleValues(targetVariable, rows).ToArray();
94      var result = new double[scaledData.GetLength(0), transformationMatrix.GetLength(1) + 1];
95      for (int i = 0; i < scaledData.GetLength(0); i++)
96        for (int j = 0; j < scaledData.GetLength(1); j++) {
97          for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
98            result[i, x] += scaledData[i, j] * transformationMatrix[j, x];
99          }
100          result[i, transformationMatrix.GetLength(1)] = targets[i];
101        }
102      return result;
103    }
104
105    public Dataset ReduceDataset(Dataset dataset, IEnumerable<int> rows) {
106      return new Dataset(Enumerable
107          .Range(0, transformationMatrix.GetLength(1))
108          .Select(x => "X" + x.ToString())
109          .Concat(targetVariable.ToEnumerable()),
110        Reduce(dataset, rows));
111    }
112  }
113}
Note: See TracBrowser for help on using the repository browser.