Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaModel.cs @ 18242

Last change on this file since 18242 was 17934, checked in by gkronber, 4 years ago

#3117: fixed build fail

File size: 4.7 KB
RevLine 
[8412]1#region License Information
2/* HeuristicLab
[17180]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[8412]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;
[16565]26using HEAL.Attic;
[8412]27using HeuristicLab.Problems.DataAnalysis;
28
[8471]29namespace HeuristicLab.Algorithms.DataAnalysis {
[8466]30  [Item("NCA Model", "")]
[16565]31  [StorableType("BB2F9ECA-CEAF-4983-A34C-9A42A132B7CD")]
[13941]32  public class NcaModel : ClassificationModel, INcaModel {
33    public override IEnumerable<string> VariablesUsedForPrediction {
[13921]34      get { return allowedInputVariables; }
35    }
[8412]36
37    [Storable]
38    private double[,] transformationMatrix;
39    public double[,] TransformationMatrix {
40      get { return (double[,])transformationMatrix.Clone(); }
41    }
42    [Storable]
[8454]43    private string[] allowedInputVariables;
[8441]44    [Storable]
[8454]45    private INearestNeighbourModel nnModel;
46    [Storable]
[8466]47    private double[] classValues;
[8412]48
49    [StorableConstructor]
[16565]50    protected NcaModel(StorableConstructorFlag _) : base(_) { }
[8466]51    protected NcaModel(NcaModel original, Cloner cloner)
[8412]52      : base(original, cloner) {
[8454]53      this.transformationMatrix = (double[,])original.transformationMatrix.Clone();
54      this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
55      this.nnModel = cloner.Clone(original.nnModel);
[8466]56      this.classValues = (double[])original.classValues.Clone();
[8412]57    }
[13941]58    public NcaModel(int k, double[,] transformationMatrix, IDataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues)
59      : base(targetVariable) {
[8454]60      Name = ItemName;
61      Description = ItemDescription;
[8466]62      this.transformationMatrix = (double[,])transformationMatrix.Clone();
[8454]63      this.allowedInputVariables = allowedInputVariables.ToArray();
[8466]64      this.classValues = (double[])classValues.Clone();
[8454]65
[8466]66      var ds = ReduceDataset(dataset, rows);
[17931]67      // the new implementation of kNN uses selfmatch=true by default
[17934]68      nnModel = new NearestNeighbourModelAlglib_3_7(ds, Enumerable.Range(0, ds.Rows), k, false, ds.VariableNames.Last(), ds.VariableNames.Take(transformationMatrix.GetLength(1)), classValues: classValues);
[8412]69    }
70
71    public override IDeepCloneable Clone(Cloner cloner) {
[8466]72      return new NcaModel(this, cloner);
[8412]73    }
74
[13941]75    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
[8466]76      var ds = ReduceDataset(dataset, rows);
77      return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows));
[8412]78    }
[8454]79
[13941]80    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
81      return new NcaClassificationSolution(this, new ClassificationProblemData(problemData));
[8412]82    }
[8454]83
[13941]84    INcaClassificationSolution INcaModel.CreateClassificationSolution(IClassificationProblemData problemData) {
85      return new NcaClassificationSolution(this, new ClassificationProblemData(problemData));
[8412]86    }
[8437]87
[12509]88    public double[,] Reduce(IDataset dataset, IEnumerable<int> rows) {
[14843]89      var data = dataset.ToArray(allowedInputVariables, rows);
[9272]90
[13941]91      var targets = dataset.GetDoubleValues(TargetVariable, rows).ToArray();
[9272]92      var result = new double[data.GetLength(0), transformationMatrix.GetLength(1) + 1];
93      for (int i = 0; i < data.GetLength(0); i++)
94        for (int j = 0; j < data.GetLength(1); j++) {
[8454]95          for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
[9272]96            result[i, x] += data[i, j] * transformationMatrix[j, x];
[8454]97          }
[8466]98          result[i, transformationMatrix.GetLength(1)] = targets[i];
99        }
[8437]100      return result;
101    }
[8454]102
[12509]103    public Dataset ReduceDataset(IDataset dataset, IEnumerable<int> rows) {
[8466]104      return new Dataset(Enumerable
105          .Range(0, transformationMatrix.GetLength(1))
106          .Select(x => "X" + x.ToString())
[13941]107          .Concat(TargetVariable.ToEnumerable()),
[8466]108        Reduce(dataset, rows));
[8454]109    }
[8412]110  }
111}
Note: See TracBrowser for help on using the repository browser.