[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;
|
---|
[8441] | 24 | using HeuristicLab.Algorithms.DataAnalysis;
|
---|
[8412] | 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.NCA {
|
---|
| 31 | [Item("NCAModel", "")]
|
---|
| 32 | [StorableClass]
|
---|
| 33 | public class NCAModel : NamedItem, INCAModel {
|
---|
| 34 |
|
---|
| 35 | [Storable]
|
---|
[8454] | 36 | private Scaling scaling;
|
---|
[8412] | 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 string targetVariable;
|
---|
| 46 | [Storable]
|
---|
| 47 | private INearestNeighbourModel nnModel;
|
---|
| 48 | [Storable]
|
---|
| 49 | private Dictionary<double, double> nn2ncaClassMapping;
|
---|
| 50 | [Storable]
|
---|
| 51 | private Dictionary<double, double> nca2nnClassMapping;
|
---|
[8412] | 52 |
|
---|
| 53 | [StorableConstructor]
|
---|
| 54 | protected NCAModel(bool deserializing) : base(deserializing) { }
|
---|
| 55 | protected NCAModel(NCAModel original, Cloner cloner)
|
---|
| 56 | : base(original, cloner) {
|
---|
[8441] | 57 | this.scaling = cloner.Clone(original.scaling);
|
---|
[8454] | 58 | this.transformationMatrix = (double[,])original.transformationMatrix.Clone();
|
---|
| 59 | this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
| 60 | this.targetVariable = original.targetVariable;
|
---|
| 61 | this.nnModel = cloner.Clone(original.nnModel);
|
---|
| 62 | this.nn2ncaClassMapping = original.nn2ncaClassMapping.ToDictionary(x => x.Key, y => y.Value);
|
---|
| 63 | this.nca2nnClassMapping = original.nca2nnClassMapping.ToDictionary(x => x.Key, y => y.Value);
|
---|
[8412] | 64 | }
|
---|
[8454] | 65 | public NCAModel(int k, double[,] scaledData, Scaling scaling, double[,] transformationMatrix, string targetVariable, IEnumerable<double> targetVector, IEnumerable<string> allowedInputVariables) {
|
---|
| 66 | Name = ItemName;
|
---|
| 67 | Description = ItemDescription;
|
---|
[8441] | 68 | this.scaling = scaling;
|
---|
[8412] | 69 | this.transformationMatrix = transformationMatrix;
|
---|
[8454] | 70 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
[8412] | 71 | this.targetVariable = targetVariable;
|
---|
[8454] | 72 |
|
---|
| 73 | nca2nnClassMapping = targetVector.Distinct().OrderBy(x => x).Select((v, i) => new { Index = (double)i, Class = v }).ToDictionary(x => x.Class, y => y.Index);
|
---|
| 74 | nn2ncaClassMapping = nca2nnClassMapping.ToDictionary(x => x.Value, y => y.Key);
|
---|
| 75 |
|
---|
| 76 | var transformedData = ReduceWithTarget(scaledData, targetVector.Select(x => nca2nnClassMapping[x]));
|
---|
| 77 |
|
---|
| 78 | var kdtree = new alglib.nearestneighbor.kdtree();
|
---|
| 79 | alglib.nearestneighbor.kdtreebuild(transformedData, transformedData.GetLength(0), transformedData.GetLength(1) - 1, 1, 2, kdtree);
|
---|
| 80 |
|
---|
| 81 | nnModel = new NearestNeighbourModel(kdtree, k, targetVariable,
|
---|
| 82 | Enumerable.Range(0, transformationMatrix.GetLength(1)).Select(x => x.ToString()),
|
---|
| 83 | nn2ncaClassMapping.Keys.ToArray());
|
---|
[8412] | 84 | }
|
---|
| 85 |
|
---|
| 86 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 87 | return new NCAModel(this, cloner);
|
---|
| 88 | }
|
---|
| 89 |
|
---|
| 90 | public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
[8454] | 91 | var unknownClasses = dataset.GetDoubleValues(targetVariable, rows).Where(x => !nca2nnClassMapping.ContainsKey(x));
|
---|
| 92 | if (unknownClasses.Any())
|
---|
| 93 | foreach (var uc in unknownClasses) {
|
---|
| 94 | nca2nnClassMapping[uc] = nca2nnClassMapping.Count;
|
---|
| 95 | nn2ncaClassMapping[nca2nnClassMapping[uc]] = uc;
|
---|
[8412] | 96 | }
|
---|
[8454] | 97 | var transformedData = ReduceWithTarget(dataset, rows, dataset.GetDoubleValues(targetVariable, rows).Select(x => nca2nnClassMapping[x]));
|
---|
| 98 | var ds = new Dataset(Enumerable.Range(0, transformationMatrix.GetLength(1)).Select(x => x.ToString()).Concat(targetVariable.ToEnumerable()), transformedData);
|
---|
| 99 | return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows)).Select(x => nn2ncaClassMapping[x]);
|
---|
[8412] | 100 | }
|
---|
[8454] | 101 |
|
---|
[8412] | 102 | public NCAClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 103 | return new NCAClassificationSolution(problemData, this);
|
---|
| 104 | }
|
---|
[8454] | 105 |
|
---|
[8412] | 106 | IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 107 | return CreateClassificationSolution(problemData);
|
---|
| 108 | }
|
---|
[8437] | 109 |
|
---|
| 110 | public double[,] Reduce(Dataset dataset, IEnumerable<int> rows) {
|
---|
[8454] | 111 | var scaledData = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, scaling);
|
---|
| 112 | return Reduce(scaledData);
|
---|
| 113 | }
|
---|
| 114 |
|
---|
| 115 | private double[,] Reduce(double[,] scaledData) {
|
---|
| 116 | var result = new double[scaledData.GetLength(0), transformationMatrix.GetLength(1)];
|
---|
| 117 | for (int i = 0; i < scaledData.GetLength(0); i++)
|
---|
| 118 | for (int j = 0; j < scaledData.GetLength(1); j++)
|
---|
| 119 | for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
|
---|
| 120 | result[i, x] += scaledData[i, j] * transformationMatrix[j, x];
|
---|
| 121 | }
|
---|
[8437] | 122 | return result;
|
---|
| 123 | }
|
---|
[8454] | 124 |
|
---|
| 125 | private double[,] ReduceWithTarget(Dataset dataset, IEnumerable<int> rows, IEnumerable<double> targetValues) {
|
---|
| 126 | var scaledData = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, scaling);
|
---|
| 127 | return ReduceWithTarget(scaledData, targetValues);
|
---|
| 128 | }
|
---|
| 129 |
|
---|
| 130 | private double[,] ReduceWithTarget(double[,] scaledData, IEnumerable<double> targetValues) {
|
---|
| 131 | var result = new double[scaledData.GetLength(0), transformationMatrix.GetLength(1) + 1];
|
---|
| 132 | for (int i = 0; i < scaledData.GetLength(0); i++)
|
---|
| 133 | for (int j = 0; j < scaledData.GetLength(1); j++)
|
---|
| 134 | for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
|
---|
| 135 | result[i, x] += scaledData[i, j] * transformationMatrix[j, x];
|
---|
| 136 | }
|
---|
| 137 |
|
---|
| 138 | int r = 0;
|
---|
| 139 | foreach (var d in targetValues) result[r++, transformationMatrix.GetLength(1)] = d;
|
---|
| 140 |
|
---|
| 141 | return result;
|
---|
| 142 | }
|
---|
[8412] | 143 | }
|
---|
| 144 | }
|
---|