Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/10/12 15:21:19 (12 years ago)
Author:
abeham
Message:

#1913:

  • Refactored classes
  • Added parameter for number of iterations
  • Added parameter for neighborhood sampling (allows to speed up gradient calculation)
  • Adapted to changed k-NN algorithm
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCAModel.cs

    r8454 r8466  
    2929
    3030namespace HeuristicLab.Algorithms.NCA {
    31   [Item("NCAModel", "")]
     31  [Item("NCA Model", "")]
    3232  [StorableClass]
    33   public class NCAModel : NamedItem, INCAModel {
     33  public class NcaModel : NamedItem, INcaModel {
    3434
    3535    [Storable]
     
    4747    private INearestNeighbourModel nnModel;
    4848    [Storable]
    49     private Dictionary<double, double> nn2ncaClassMapping;
    50     [Storable]
    51     private Dictionary<double, double> nca2nnClassMapping;
     49    private double[] classValues;
    5250
    5351    [StorableConstructor]
    54     protected NCAModel(bool deserializing) : base(deserializing) { }
    55     protected NCAModel(NCAModel original, Cloner cloner)
     52    protected NcaModel(bool deserializing) : base(deserializing) { }
     53    protected NcaModel(NcaModel original, Cloner cloner)
    5654      : base(original, cloner) {
    5755      this.scaling = cloner.Clone(original.scaling);
     
    6058      this.targetVariable = original.targetVariable;
    6159      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);
     60      this.classValues = (double[])original.classValues.Clone();
    6461    }
    65     public NCAModel(int k, double[,] scaledData, Scaling scaling, double[,] transformationMatrix, string targetVariable, IEnumerable<double> targetVector, IEnumerable<string> allowedInputVariables) {
     62    public NcaModel(int k, double[,] transformationMatrix, Dataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, Scaling scaling, double[] classValues) {
    6663      Name = ItemName;
    6764      Description = ItemDescription;
    6865      this.scaling = scaling;
    69       this.transformationMatrix = transformationMatrix;
     66      this.transformationMatrix = (double[,])transformationMatrix.Clone();
    7067      this.allowedInputVariables = allowedInputVariables.ToArray();
    7168      this.targetVariable = targetVariable;
     69      this.classValues = (double[])classValues.Clone();
    7270
    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());
     71      var ds = ReduceDataset(dataset, rows);
     72      nnModel = new NearestNeighbourModel(ds, Enumerable.Range(0, ds.Rows), k, ds.VariableNames.Last(), ds.VariableNames.Take(transformationMatrix.GetLength(1)), classValues);
    8473    }
    8574
    8675    public override IDeepCloneable Clone(Cloner cloner) {
    87       return new NCAModel(this, cloner);
     76      return new NcaModel(this, cloner);
    8877    }
    8978
    9079    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
    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;
    96         }
    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]);
     80      var ds = ReduceDataset(dataset, rows);
     81      return nnModel.GetEstimatedClassValues(ds, Enumerable.Range(0, ds.Rows));
    10082    }
    10183
    102     public NCAClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
    103       return new NCAClassificationSolution(problemData, this);
     84    public INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
     85      return new NcaClassificationSolution(problemData, this);
    10486    }
    10587
     
    11092    public double[,] Reduce(Dataset dataset, IEnumerable<int> rows) {
    11193      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)];
     94      var targets = dataset.GetDoubleValues(targetVariable, rows).ToArray();
     95      var result = new double[scaledData.GetLength(0), transformationMatrix.GetLength(1) + 1];
    11796      for (int i = 0; i < scaledData.GetLength(0); i++)
    118         for (int j = 0; j < scaledData.GetLength(1); j++)
     97        for (int j = 0; j < scaledData.GetLength(1); j++) {
    11998          for (int x = 0; x < transformationMatrix.GetLength(1); x++) {
    12099            result[i, x] += scaledData[i, j] * transformationMatrix[j, x];
    121100          }
     101          result[i, transformationMatrix.GetLength(1)] = targets[i];
     102        }
    122103      return result;
    123104    }
    124105
    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;
     106    public Dataset ReduceDataset(Dataset dataset, IEnumerable<int> rows) {
     107      return new Dataset(Enumerable
     108          .Range(0, transformationMatrix.GetLength(1))
     109          .Select(x => "X" + x.ToString())
     110          .Concat(targetVariable.ToEnumerable()),
     111        Reduce(dataset, rows));
    142112    }
    143113  }
Note: See TracChangeset for help on using the changeset viewer.