Changeset 8466 for branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCAModel.cs
- Timestamp:
- 08/10/12 15:21:19 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
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.