#region License Information
/* HeuristicLab
* Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
*
* This file is part of HeuristicLab.
*
* HeuristicLab is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* HeuristicLab is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with HeuristicLab. If not, see .
*/
#endregion
using System.Collections.Generic;
using System.Linq;
using HeuristicLab.Algorithms.DataAnalysis;
using HeuristicLab.Common;
using HeuristicLab.Core;
using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
using HeuristicLab.Problems.DataAnalysis;
namespace HeuristicLab.Algorithms.NCA {
[Item("NCAModel", "")]
[StorableClass]
public class NCAModel : NamedItem, INCAModel {
[Storable]
private Scaling scaling;
[Storable]
private double[,] transformationMatrix;
public double[,] TransformationMatrix {
get { return (double[,])transformationMatrix.Clone(); }
}
[Storable]
private string[] allowedInputVariables;
[Storable]
private string targetVariable;
[Storable]
private INearestNeighbourModel nnModel;
[Storable]
private Dictionary nn2ncaClassMapping;
[Storable]
private Dictionary nca2nnClassMapping;
[StorableConstructor]
protected NCAModel(bool deserializing) : base(deserializing) { }
protected NCAModel(NCAModel original, Cloner cloner)
: base(original, cloner) {
this.scaling = cloner.Clone(original.scaling);
this.transformationMatrix = (double[,])original.transformationMatrix.Clone();
this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
this.targetVariable = original.targetVariable;
this.nnModel = cloner.Clone(original.nnModel);
this.nn2ncaClassMapping = original.nn2ncaClassMapping.ToDictionary(x => x.Key, y => y.Value);
this.nca2nnClassMapping = original.nca2nnClassMapping.ToDictionary(x => x.Key, y => y.Value);
}
public NCAModel(int k, double[,] scaledData, Scaling scaling, double[,] transformationMatrix, string targetVariable, IEnumerable targetVector, IEnumerable allowedInputVariables) {
Name = ItemName;
Description = ItemDescription;
this.scaling = scaling;
this.transformationMatrix = transformationMatrix;
this.allowedInputVariables = allowedInputVariables.ToArray();
this.targetVariable = targetVariable;
nca2nnClassMapping = targetVector.Distinct().OrderBy(x => x).Select((v, i) => new { Index = (double)i, Class = v }).ToDictionary(x => x.Class, y => y.Index);
nn2ncaClassMapping = nca2nnClassMapping.ToDictionary(x => x.Value, y => y.Key);
var transformedData = ReduceWithTarget(scaledData, targetVector.Select(x => nca2nnClassMapping[x]));
var kdtree = new alglib.nearestneighbor.kdtree();
alglib.nearestneighbor.kdtreebuild(transformedData, transformedData.GetLength(0), transformedData.GetLength(1) - 1, 1, 2, kdtree);
nnModel = new NearestNeighbourModel(kdtree, k, targetVariable,
Enumerable.Range(0, transformationMatrix.GetLength(1)).Select(x => x.ToString()),
nn2ncaClassMapping.Keys.ToArray());
}
public override IDeepCloneable Clone(Cloner cloner) {
return new NCAModel(this, cloner);
}
public IEnumerable GetEstimatedClassValues(Dataset dataset, IEnumerable rows) {
var unknownClasses = dataset.GetDoubleValues(targetVariable, rows).Where(x => !nca2nnClassMapping.ContainsKey(x));
if (unknownClasses.Any())
foreach (var uc in unknownClasses) {
nca2nnClassMapping[uc] = nca2nnClassMapping.Count;
nn2ncaClassMapping[nca2nnClassMapping[uc]] = uc;
}
var transformedData = ReduceWithTarget(dataset, rows, dataset.GetDoubleValues(targetVariable, rows).Select(x => nca2nnClassMapping[x]));
var ds = new Dataset(Enumerable.Range(0, transformationMatrix.GetLength(1)).Select(x => x.ToString()).Concat(targetVariable.ToEnumerable()), transformedData);
return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows)).Select(x => nn2ncaClassMapping[x]);
}
public NCAClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
return new NCAClassificationSolution(problemData, this);
}
IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
return CreateClassificationSolution(problemData);
}
public double[,] Reduce(Dataset dataset, IEnumerable rows) {
var scaledData = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, scaling);
return Reduce(scaledData);
}
private double[,] Reduce(double[,] scaledData) {
var result = new double[scaledData.GetLength(0), transformationMatrix.GetLength(1)];
for (int i = 0; i < scaledData.GetLength(0); i++)
for (int j = 0; j < scaledData.GetLength(1); j++)
for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
result[i, x] += scaledData[i, j] * transformationMatrix[j, x];
}
return result;
}
private double[,] ReduceWithTarget(Dataset dataset, IEnumerable rows, IEnumerable targetValues) {
var scaledData = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, scaling);
return ReduceWithTarget(scaledData, targetValues);
}
private double[,] ReduceWithTarget(double[,] scaledData, IEnumerable targetValues) {
var result = new double[scaledData.GetLength(0), transformationMatrix.GetLength(1) + 1];
for (int i = 0; i < scaledData.GetLength(0); i++)
for (int j = 0; j < scaledData.GetLength(1); j++)
for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
result[i, x] += scaledData[i, j] * transformationMatrix[j, x];
}
int r = 0;
foreach (var d in targetValues) result[r++, transformationMatrix.GetLength(1)] = d;
return result;
}
}
}