Free cookie consent management tool by TermsFeed Policy Generator

Changeset 8466 for branches/NCA


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
Location:
branches/NCA
Files:
1 added
1 deleted
7 edited

Legend:

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

    r8437 r8466  
    3131namespace HeuristicLab.Algorithms.NCA.Views {
    3232  [View("Dimension Reduction")]
    33   [Content(typeof(INCAClassificationSolution), IsDefaultView = false)]
     33  [Content(typeof(INcaClassificationSolution), IsDefaultView = false)]
    3434  public partial class NCADimensionReductionView : DataAnalysisSolutionEvaluationView {
    3535    private ViewHost viewHost = new ViewHost();
    3636    private ScatterPlot scatterPlot = new ScatterPlot();
    3737
    38     public new INCAClassificationSolution Content {
    39       get { return (INCAClassificationSolution)base.Content; }
     38    public new INcaClassificationSolution Content {
     39      get { return (INcaClassificationSolution)base.Content; }
    4040      set { base.Content = value; }
    4141    }
     
    7373
    7474      int idx = 0;
    75       if (reduced.GetLength(1) == 1) {
     75      if (reduced.GetLength(1) == 2) { // last column is the target variable
    7676        foreach (var r in range) {
    7777          var label = Content.ProblemData.Dataset.GetDoubleValue(Content.ProblemData.TargetVariable, r);
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/HeuristicLab.Algorithms.NCA-3.3.csproj

    r8441 r8466  
    102102  </ItemGroup>
    103103  <ItemGroup>
    104     <Compile Include="INCAClassificationSolution.cs" />
    105     <Compile Include="Initialization\INCAInitializer.cs" />
    106     <Compile Include="INCAModel.cs" />
    107     <Compile Include="Initialization\LDAInitializer.cs" />
    108     <Compile Include="Initialization\PCAInitializer.cs" />
     104    <Compile Include="INcaClassificationSolution.cs" />
     105    <Compile Include="Initialization\INcaInitializer.cs" />
     106    <Compile Include="INcaModel.cs" />
     107    <Compile Include="Initialization\LdaInitializer.cs" />
     108    <Compile Include="Initialization\PcaInitializer.cs" />
    109109    <Compile Include="Initialization\RandomInitializer.cs" />
    110110    <Compile Include="Matrix.cs" />
    111     <Compile Include="NeighborhoodComponentsAnalysis.cs" />
    112     <Compile Include="NCAClassificationSolution.cs" />
    113     <Compile Include="NCAModel.cs" />
     111    <Compile Include="NcaAlgorithm.cs" />
     112    <Compile Include="NcaClassificationSolution.cs" />
     113    <Compile Include="NcaModel.cs" />
    114114    <Compile Include="Plugin.cs" />
    115115    <Compile Include="Properties\AssemblyInfo.cs" />
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/INCAClassificationSolution.cs

    r8437 r8466  
    2323
    2424namespace HeuristicLab.Algorithms.NCA {
    25   public interface INCAClassificationSolution : IClassificationSolution {
    26     new INCAModel Model { get; }
     25  public interface INcaClassificationSolution : IClassificationSolution {
     26    new INcaModel Model { get; }
    2727  }
    2828}
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/INCAModel.cs

    r8437 r8466  
    2424
    2525namespace HeuristicLab.Algorithms.NCA {
    26   public interface INCAModel : IClassificationModel {
     26  public interface INcaModel : IClassificationModel {
     27    new INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData);
     28
    2729    double[,] Reduce(Dataset dataset, IEnumerable<int> rows);
    2830  }
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/Matrix.cs

    r8437 r8466  
    3030  [NonDiscoverableType]
    3131  internal class Matrix : IEnumerable<double>, IDeepCloneable {
    32 
    33     private IEnumerable<double> values;
    34     public int Rows { get; private set; }
    35     public int Columns { get; private set; }
     32    // this type is immutable
     33    private readonly IEnumerable<double> values;
     34    public readonly int Rows;
     35    public readonly int Columns;
    3636
    3737    protected Matrix(Matrix original, Cloner cloner) {
  • branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCAClassificationSolution.cs

    r8437 r8466  
    2626
    2727namespace HeuristicLab.Algorithms.NCA {
    28   [Item("NCAClassificationSolution", "")]
     28  [Item("NCA Classification Solution", "")]
    2929  [StorableClass]
    30   public class NCAClassificationSolution : ClassificationSolution, INCAClassificationSolution {
     30  public class NcaClassificationSolution : ClassificationSolution, INcaClassificationSolution {
    3131
    32     public new INCAModel Model {
    33       get { return (INCAModel)base.Model; }
     32    public new INcaModel Model {
     33      get { return (INcaModel)base.Model; }
    3434      set { base.Model = value; }
    3535    }
    3636
    3737    [StorableConstructor]
    38     private NCAClassificationSolution(bool deserializing) : base(deserializing) { }
    39     private NCAClassificationSolution(NCAClassificationSolution original, Cloner cloner)
     38    private NcaClassificationSolution(bool deserializing) : base(deserializing) { }
     39    private NcaClassificationSolution(NcaClassificationSolution original, Cloner cloner)
    4040      : base(original, cloner) {
    4141    }
    42     public NCAClassificationSolution(IClassificationProblemData problemData, INCAModel ncaModel)
     42    public NcaClassificationSolution(IClassificationProblemData problemData, INcaModel ncaModel)
    4343      : base(ncaModel, problemData) {
    4444      RecalculateResults();
     
    4646
    4747    public override IDeepCloneable Clone(Cloner cloner) {
    48       return new NCAClassificationSolution(this, cloner);
     48      return new NcaClassificationSolution(this, cloner);
    4949    }
    5050
  • 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.