[8412] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 27 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 28 |
|
---|
[8471] | 29 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[8466] | 30 | [Item("NCA Model", "")]
|
---|
[8412] | 31 | [StorableClass]
|
---|
[8466] | 32 | public class NcaModel : NamedItem, INcaModel {
|
---|
[8412] | 33 |
|
---|
| 34 | [Storable]
|
---|
[8454] | 35 | private Scaling scaling;
|
---|
[8412] | 36 | [Storable]
|
---|
| 37 | private double[,] transformationMatrix;
|
---|
| 38 | public double[,] TransformationMatrix {
|
---|
| 39 | get { return (double[,])transformationMatrix.Clone(); }
|
---|
| 40 | }
|
---|
| 41 | [Storable]
|
---|
[8454] | 42 | private string[] allowedInputVariables;
|
---|
[8441] | 43 | [Storable]
|
---|
[8454] | 44 | private string targetVariable;
|
---|
| 45 | [Storable]
|
---|
| 46 | private INearestNeighbourModel nnModel;
|
---|
| 47 | [Storable]
|
---|
[8466] | 48 | private double[] classValues;
|
---|
[8412] | 49 |
|
---|
| 50 | [StorableConstructor]
|
---|
[8466] | 51 | protected NcaModel(bool deserializing) : base(deserializing) { }
|
---|
| 52 | protected NcaModel(NcaModel original, Cloner cloner)
|
---|
[8412] | 53 | : base(original, cloner) {
|
---|
[8441] | 54 | this.scaling = cloner.Clone(original.scaling);
|
---|
[8454] | 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);
|
---|
[8466] | 59 | this.classValues = (double[])original.classValues.Clone();
|
---|
[8412] | 60 | }
|
---|
[8466] | 61 | public NcaModel(int k, double[,] transformationMatrix, Dataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, Scaling scaling, double[] classValues) {
|
---|
[8454] | 62 | Name = ItemName;
|
---|
| 63 | Description = ItemDescription;
|
---|
[8441] | 64 | this.scaling = scaling;
|
---|
[8466] | 65 | this.transformationMatrix = (double[,])transformationMatrix.Clone();
|
---|
[8454] | 66 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
[8412] | 67 | this.targetVariable = targetVariable;
|
---|
[8466] | 68 | this.classValues = (double[])classValues.Clone();
|
---|
[8454] | 69 |
|
---|
[8466] | 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);
|
---|
[8412] | 72 | }
|
---|
| 73 |
|
---|
| 74 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8466] | 75 | return new NcaModel(this, cloner);
|
---|
[8412] | 76 | }
|
---|
| 77 |
|
---|
| 78 | public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
[8466] | 79 | var ds = ReduceDataset(dataset, rows);
|
---|
| 80 | return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows));
|
---|
[8412] | 81 | }
|
---|
[8454] | 82 |
|
---|
[8466] | 83 | public INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
[8528] | 84 | return new NcaClassificationSolution(new ClassificationProblemData(problemData), this);
|
---|
[8412] | 85 | }
|
---|
[8454] | 86 |
|
---|
[8412] | 87 | IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 88 | return CreateClassificationSolution(problemData);
|
---|
| 89 | }
|
---|
[8437] | 90 |
|
---|
| 91 | public double[,] Reduce(Dataset dataset, IEnumerable<int> rows) {
|
---|
[8454] | 92 | var scaledData = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, scaling);
|
---|
[8466] | 93 | var targets = dataset.GetDoubleValues(targetVariable, rows).ToArray();
|
---|
| 94 | var result = new double[scaledData.GetLength(0), transformationMatrix.GetLength(1) + 1];
|
---|
[8454] | 95 | for (int i = 0; i < scaledData.GetLength(0); i++)
|
---|
[8466] | 96 | for (int j = 0; j < scaledData.GetLength(1); j++) {
|
---|
[8454] | 97 | for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
|
---|
| 98 | result[i, x] += scaledData[i, j] * transformationMatrix[j, x];
|
---|
| 99 | }
|
---|
[8466] | 100 | result[i, transformationMatrix.GetLength(1)] = targets[i];
|
---|
| 101 | }
|
---|
[8437] | 102 | return result;
|
---|
| 103 | }
|
---|
[8454] | 104 |
|
---|
[8466] | 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));
|
---|
[8454] | 111 | }
|
---|
[8412] | 112 | }
|
---|
| 113 | }
|
---|