[8412] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12012] | 3 | * Copyright (C) 2002-2015 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 |
|
---|
| 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 {
|
---|
[13921] | 33 | public IEnumerable<string> VariablesUsedForPrediction {
|
---|
| 34 | get { return allowedInputVariables; }
|
---|
| 35 | }
|
---|
[8412] | 36 |
|
---|
[13921] | 37 | public string TargetVariable {
|
---|
| 38 | get { return targetVariable; }
|
---|
| 39 | }
|
---|
| 40 |
|
---|
[8412] | 41 | [Storable]
|
---|
| 42 | private double[,] transformationMatrix;
|
---|
| 43 | public double[,] TransformationMatrix {
|
---|
| 44 | get { return (double[,])transformationMatrix.Clone(); }
|
---|
| 45 | }
|
---|
| 46 | [Storable]
|
---|
[8454] | 47 | private string[] allowedInputVariables;
|
---|
[8441] | 48 | [Storable]
|
---|
[8454] | 49 | private string targetVariable;
|
---|
| 50 | [Storable]
|
---|
| 51 | private INearestNeighbourModel nnModel;
|
---|
| 52 | [Storable]
|
---|
[8466] | 53 | private double[] classValues;
|
---|
[8412] | 54 |
|
---|
| 55 | [StorableConstructor]
|
---|
[8466] | 56 | protected NcaModel(bool deserializing) : base(deserializing) { }
|
---|
| 57 | protected NcaModel(NcaModel original, Cloner cloner)
|
---|
[8412] | 58 | : base(original, cloner) {
|
---|
[8454] | 59 | this.transformationMatrix = (double[,])original.transformationMatrix.Clone();
|
---|
| 60 | this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
|
---|
| 61 | this.targetVariable = original.targetVariable;
|
---|
| 62 | this.nnModel = cloner.Clone(original.nnModel);
|
---|
[8466] | 63 | this.classValues = (double[])original.classValues.Clone();
|
---|
[8412] | 64 | }
|
---|
[12509] | 65 | public NcaModel(int k, double[,] transformationMatrix, IDataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues) {
|
---|
[8454] | 66 | Name = ItemName;
|
---|
| 67 | Description = ItemDescription;
|
---|
[8466] | 68 | this.transformationMatrix = (double[,])transformationMatrix.Clone();
|
---|
[8454] | 69 | this.allowedInputVariables = allowedInputVariables.ToArray();
|
---|
[8412] | 70 | this.targetVariable = targetVariable;
|
---|
[8466] | 71 | this.classValues = (double[])classValues.Clone();
|
---|
[8454] | 72 |
|
---|
[8466] | 73 | var ds = ReduceDataset(dataset, rows);
|
---|
| 74 | nnModel = new NearestNeighbourModel(ds, Enumerable.Range(0, ds.Rows), k, ds.VariableNames.Last(), ds.VariableNames.Take(transformationMatrix.GetLength(1)), classValues);
|
---|
[8412] | 75 | }
|
---|
| 76 |
|
---|
| 77 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
[8466] | 78 | return new NcaModel(this, cloner);
|
---|
[8412] | 79 | }
|
---|
| 80 |
|
---|
[12509] | 81 | public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
|
---|
[8466] | 82 | var ds = ReduceDataset(dataset, rows);
|
---|
| 83 | return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows));
|
---|
[8412] | 84 | }
|
---|
[8454] | 85 |
|
---|
[8466] | 86 | public INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
[8528] | 87 | return new NcaClassificationSolution(new ClassificationProblemData(problemData), this);
|
---|
[8412] | 88 | }
|
---|
[8454] | 89 |
|
---|
[8412] | 90 | IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 91 | return CreateClassificationSolution(problemData);
|
---|
| 92 | }
|
---|
[8437] | 93 |
|
---|
[12509] | 94 | public double[,] Reduce(IDataset dataset, IEnumerable<int> rows) {
|
---|
[9272] | 95 | var data = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows);
|
---|
| 96 |
|
---|
[8466] | 97 | var targets = dataset.GetDoubleValues(targetVariable, rows).ToArray();
|
---|
[9272] | 98 | var result = new double[data.GetLength(0), transformationMatrix.GetLength(1) + 1];
|
---|
| 99 | for (int i = 0; i < data.GetLength(0); i++)
|
---|
| 100 | for (int j = 0; j < data.GetLength(1); j++) {
|
---|
[8454] | 101 | for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
|
---|
[9272] | 102 | result[i, x] += data[i, j] * transformationMatrix[j, x];
|
---|
[8454] | 103 | }
|
---|
[8466] | 104 | result[i, transformationMatrix.GetLength(1)] = targets[i];
|
---|
| 105 | }
|
---|
[8437] | 106 | return result;
|
---|
| 107 | }
|
---|
[8454] | 108 |
|
---|
[12509] | 109 | public Dataset ReduceDataset(IDataset dataset, IEnumerable<int> rows) {
|
---|
[8466] | 110 | return new Dataset(Enumerable
|
---|
| 111 | .Range(0, transformationMatrix.GetLength(1))
|
---|
| 112 | .Select(x => "X" + x.ToString())
|
---|
| 113 | .Concat(targetVariable.ToEnumerable()),
|
---|
| 114 | Reduce(dataset, rows));
|
---|
[8454] | 115 | }
|
---|
[8412] | 116 | }
|
---|
| 117 | }
|
---|