Changeset 8466 for branches/NCA
- Timestamp:
- 08/10/12 15:21:19 (12 years ago)
- 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 31 31 namespace HeuristicLab.Algorithms.NCA.Views { 32 32 [View("Dimension Reduction")] 33 [Content(typeof(IN CAClassificationSolution), IsDefaultView = false)]33 [Content(typeof(INcaClassificationSolution), IsDefaultView = false)] 34 34 public partial class NCADimensionReductionView : DataAnalysisSolutionEvaluationView { 35 35 private ViewHost viewHost = new ViewHost(); 36 36 private ScatterPlot scatterPlot = new ScatterPlot(); 37 37 38 public new IN CAClassificationSolution Content {39 get { return (IN CAClassificationSolution)base.Content; }38 public new INcaClassificationSolution Content { 39 get { return (INcaClassificationSolution)base.Content; } 40 40 set { base.Content = value; } 41 41 } … … 73 73 74 74 int idx = 0; 75 if (reduced.GetLength(1) == 1) {75 if (reduced.GetLength(1) == 2) { // last column is the target variable 76 76 foreach (var r in range) { 77 77 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 102 102 </ItemGroup> 103 103 <ItemGroup> 104 <Compile Include="IN CAClassificationSolution.cs" />105 <Compile Include="Initialization\IN CAInitializer.cs" />106 <Compile Include="IN CAModel.cs" />107 <Compile Include="Initialization\L DAInitializer.cs" />108 <Compile Include="Initialization\P CAInitializer.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" /> 109 109 <Compile Include="Initialization\RandomInitializer.cs" /> 110 110 <Compile Include="Matrix.cs" /> 111 <Compile Include="N eighborhoodComponentsAnalysis.cs" />112 <Compile Include="N CAClassificationSolution.cs" />113 <Compile Include="N CAModel.cs" />111 <Compile Include="NcaAlgorithm.cs" /> 112 <Compile Include="NcaClassificationSolution.cs" /> 113 <Compile Include="NcaModel.cs" /> 114 114 <Compile Include="Plugin.cs" /> 115 115 <Compile Include="Properties\AssemblyInfo.cs" /> -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/INCAClassificationSolution.cs
r8437 r8466 23 23 24 24 namespace HeuristicLab.Algorithms.NCA { 25 public interface IN CAClassificationSolution : IClassificationSolution {26 new IN CAModel Model { get; }25 public interface INcaClassificationSolution : IClassificationSolution { 26 new INcaModel Model { get; } 27 27 } 28 28 } -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/INCAModel.cs
r8437 r8466 24 24 25 25 namespace HeuristicLab.Algorithms.NCA { 26 public interface INCAModel : IClassificationModel { 26 public interface INcaModel : IClassificationModel { 27 new INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData); 28 27 29 double[,] Reduce(Dataset dataset, IEnumerable<int> rows); 28 30 } -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/Matrix.cs
r8437 r8466 30 30 [NonDiscoverableType] 31 31 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; 36 36 37 37 protected Matrix(Matrix original, Cloner cloner) { -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCAClassificationSolution.cs
r8437 r8466 26 26 27 27 namespace HeuristicLab.Algorithms.NCA { 28 [Item("NCA ClassificationSolution", "")]28 [Item("NCA Classification Solution", "")] 29 29 [StorableClass] 30 public class N CAClassificationSolution : ClassificationSolution, INCAClassificationSolution {30 public class NcaClassificationSolution : ClassificationSolution, INcaClassificationSolution { 31 31 32 public new IN CAModel Model {33 get { return (IN CAModel)base.Model; }32 public new INcaModel Model { 33 get { return (INcaModel)base.Model; } 34 34 set { base.Model = value; } 35 35 } 36 36 37 37 [StorableConstructor] 38 private N CAClassificationSolution(bool deserializing) : base(deserializing) { }39 private N CAClassificationSolution(NCAClassificationSolution original, Cloner cloner)38 private NcaClassificationSolution(bool deserializing) : base(deserializing) { } 39 private NcaClassificationSolution(NcaClassificationSolution original, Cloner cloner) 40 40 : base(original, cloner) { 41 41 } 42 public N CAClassificationSolution(IClassificationProblemData problemData, INCAModel ncaModel)42 public NcaClassificationSolution(IClassificationProblemData problemData, INcaModel ncaModel) 43 43 : base(ncaModel, problemData) { 44 44 RecalculateResults(); … … 46 46 47 47 public override IDeepCloneable Clone(Cloner cloner) { 48 return new N CAClassificationSolution(this, cloner);48 return new NcaClassificationSolution(this, cloner); 49 49 } 50 50 -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCAModel.cs
r8454 r8466 29 29 30 30 namespace HeuristicLab.Algorithms.NCA { 31 [Item("NCA Model", "")]31 [Item("NCA Model", "")] 32 32 [StorableClass] 33 public class N CAModel : NamedItem, INCAModel {33 public class NcaModel : NamedItem, INcaModel { 34 34 35 35 [Storable] … … 47 47 private INearestNeighbourModel nnModel; 48 48 [Storable] 49 private Dictionary<double, double> nn2ncaClassMapping; 50 [Storable] 51 private Dictionary<double, double> nca2nnClassMapping; 49 private double[] classValues; 52 50 53 51 [StorableConstructor] 54 protected N CAModel(bool deserializing) : base(deserializing) { }55 protected N CAModel(NCAModel original, Cloner cloner)52 protected NcaModel(bool deserializing) : base(deserializing) { } 53 protected NcaModel(NcaModel original, Cloner cloner) 56 54 : base(original, cloner) { 57 55 this.scaling = cloner.Clone(original.scaling); … … 60 58 this.targetVariable = original.targetVariable; 61 59 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(); 64 61 } 65 public N CAModel(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) { 66 63 Name = ItemName; 67 64 Description = ItemDescription; 68 65 this.scaling = scaling; 69 this.transformationMatrix = transformationMatrix;66 this.transformationMatrix = (double[,])transformationMatrix.Clone(); 70 67 this.allowedInputVariables = allowedInputVariables.ToArray(); 71 68 this.targetVariable = targetVariable; 69 this.classValues = (double[])classValues.Clone(); 72 70 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); 84 73 } 85 74 86 75 public override IDeepCloneable Clone(Cloner cloner) { 87 return new N CAModel(this, cloner);76 return new NcaModel(this, cloner); 88 77 } 89 78 90 79 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)); 100 82 } 101 83 102 public NCAClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {103 return new N CAClassificationSolution(problemData, this);84 public INcaClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) { 85 return new NcaClassificationSolution(problemData, this); 104 86 } 105 87 … … 110 92 public double[,] Reduce(Dataset dataset, IEnumerable<int> rows) { 111 93 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]; 117 96 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++) { 119 98 for (int x = 0; x < transformationMatrix.GetLength(1); x++) { 120 99 result[i, x] += scaledData[i, j] * transformationMatrix[j, x]; 121 100 } 101 result[i, transformationMatrix.GetLength(1)] = targets[i]; 102 } 122 103 return result; 123 104 } 124 105 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)); 142 112 } 143 113 }
Note: See TracChangeset
for help on using the changeset viewer.