Changeset 9272 for trunk/sources/HeuristicLab.Algorithms.DataAnalysis
- Timestamp:
- 03/04/13 15:16:49 (12 years ago)
- Location:
- trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca
- Files:
-
- 8 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/INcaInitializer.cs
r9270 r9272 31 31 /// <param name="dimensions">The amount of columns in the matrix</param> 32 32 /// <returns>The matrix that projects the input variables into a lower dimensional space.</returns> 33 double[,] Initialize(IClassificationProblemData data, Scaling scaling,int dimensions);33 double[,] Initialize(IClassificationProblemData data, int dimensions); 34 34 } 35 35 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/LdaInitializer.cs
r9270 r9272 40 40 } 41 41 42 public override double[,] Initialize(IClassificationProblemData data, Scaling scaling,int dimensions) {42 public override double[,] Initialize(IClassificationProblemData data, int dimensions) { 43 43 var instances = data.TrainingIndices.Count(); 44 44 var attributes = data.AllowedInputVariables.Count(); 45 45 46 var ldaDs = new double[instances, attributes + 1]; 47 int j = 0; 48 foreach (var a in data.AllowedInputVariables) { 49 int i = 0; 50 var sv = scaling.GetScaledValues(data.Dataset, a, data.TrainingIndices); 51 foreach (var v in sv) { 52 ldaDs[i++, j] = v; 53 } 54 j++; 55 } 56 j = 0; 57 foreach (var tv in data.Dataset.GetDoubleValues(data.TargetVariable, data.TrainingIndices)) 58 ldaDs[j++, attributes] = tv; 46 var ldaDs = AlglibUtil.PrepareInputMatrix(data.Dataset, 47 data.AllowedInputVariables.Concat(data.TargetVariable.ToEnumerable()), 48 data.TrainingIndices); 59 49 60 50 var uniqueClasses = data.Dataset.GetDoubleValues(data.TargetVariable, data.TrainingIndices).Distinct().Count(); -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/NcaInitializer.cs
r9270 r9272 43 43 get { return (ILookupParameter<RealVector>)Parameters["NcaMatrix"]; } 44 44 } 45 public ILookupParameter<Scaling> ScalingParameter {46 get { return (ILookupParameter<Scaling>)Parameters["Scaling"]; }47 }48 45 49 46 [StorableConstructor] … … 54 51 Parameters.Add(new LookupParameter<IntValue>("Dimensions", "The number of dimensions to which the features should be pruned.")); 55 52 Parameters.Add(new LookupParameter<RealVector>("NcaMatrix", "The coefficients of the matrix that need to be optimized. Note that the matrix is flattened.")); 56 Parameters.Add(new LookupParameter<Scaling>("Scaling", "Each dataset is scaled and the information is stored in a scaling object."));57 53 } 58 54 59 55 public override IOperation Apply() { 60 56 var problemData = ProblemDataParameter.ActualValue; 61 var scaling = new Scaling(problemData.Dataset, problemData.AllowedInputVariables, problemData.TrainingIndices);62 57 63 58 var dimensions = DimensionsParameter.ActualValue.Value; 64 var matrix = Initialize(problemData, scaling,dimensions);59 var matrix = Initialize(problemData, dimensions); 65 60 var attributes = matrix.GetLength(0); 66 61 … … 70 65 result[i * dimensions + j] = matrix[i, j]; 71 66 72 ScalingParameter.ActualValue = scaling;73 67 NcaMatrixParameter.ActualValue = new RealVector(result); 74 68 return base.Apply(); 75 69 } 76 70 77 public abstract double[,] Initialize(IClassificationProblemData data, Scaling scaling,int dimensions);71 public abstract double[,] Initialize(IClassificationProblemData data, int dimensions); 78 72 } 79 73 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/PcaInitializer.cs
r9270 r9272 40 40 } 41 41 42 public override double[,] Initialize(IClassificationProblemData data, Scaling scaling,int dimensions) {42 public override double[,] Initialize(IClassificationProblemData data, int dimensions) { 43 43 var instances = data.TrainingIndices.Count(); 44 44 var attributes = data.AllowedInputVariables.Count(); 45 45 46 var pcaDs = AlglibUtil.Prepare AndScaleInputMatrix(data.Dataset, data.AllowedInputVariables, data.TrainingIndices, scaling);46 var pcaDs = AlglibUtil.PrepareInputMatrix(data.Dataset, data.AllowedInputVariables, data.TrainingIndices); 47 47 48 48 int info; -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/Initialization/RandomInitializer.cs
r9270 r9272 48 48 } 49 49 50 public override double[,] Initialize(IClassificationProblemData data, Scaling scaling,int dimensions) {50 public override double[,] Initialize(IClassificationProblemData data, int dimensions) { 51 51 var attributes = data.AllowedInputVariables.Count(); 52 52 -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/ModelCreation/NcaModelCreator.cs
r9270 r9272 55 55 } 56 56 57 public ILookupParameter<Scaling> ScalingParameter {58 get { return (ILookupParameter<Scaling>)Parameters["Scaling"]; }59 }60 61 57 public ILookupParameter<INcaModel> NcaModelParameter { 62 58 get { return (ILookupParameter<INcaModel>)Parameters["NcaModel"]; } … … 72 68 Parameters.Add(new LookupParameter<RealVector>("NcaMatrixGradients", "The gradients from the matrix that is being optimized.")); 73 69 Parameters.Add(new LookupParameter<IClassificationProblemData>("ProblemData", "The classification problem data.")); 74 Parameters.Add(new LookupParameter<Scaling>("Scaling", "The scaling of the dataset."));75 70 Parameters.Add(new LookupParameter<INcaModel>("NcaModel", "The NCA model that should be created.")); 76 71 } … … 91 86 } 92 87 93 var scaling = ScalingParameter.ActualValue;94 88 var problemData = ProblemDataParameter.ActualValue; 95 NcaModelParameter.ActualValue = new NcaModel(k, matrix, problemData.Dataset, problemData.TrainingIndices, problemData.TargetVariable, problemData.AllowedInputVariables, scaling,problemData.ClassValues.ToArray());89 NcaModelParameter.ActualValue = new NcaModel(k, matrix, problemData.Dataset, problemData.TrainingIndices, problemData.TargetVariable, problemData.AllowedInputVariables, problemData.ClassValues.ToArray()); 96 90 return base.Apply(); 97 91 } -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaGradientCalculator.cs
r9270 r9272 65 65 get { return (ILookupParameter<IClassificationProblemData>)Parameters["ProblemData"]; } 66 66 } 67 68 public ILookupParameter<Scaling> ScalingParameter {69 get { return (ILookupParameter<Scaling>)Parameters["Scaling"]; }70 }71 67 #endregion 72 68 … … 83 79 Parameters.Add(new LookupParameter<DoubleValue>("Quality", "The quality of the current matrix.")); 84 80 Parameters.Add(new LookupParameter<IClassificationProblemData>("ProblemData", "The classification problem data.")); 85 Parameters.Add(new LookupParameter<Scaling>("Scaling", "The scaling of the dataset."));86 81 } 87 82 … … 92 87 public override IOperation Apply() { 93 88 var problemData = ProblemDataParameter.ActualValue; 94 var scaling = ScalingParameter.ActualValue;95 89 var dimensions = DimensionsParameter.ActualValue.Value; 96 90 var neighborSamples = NeighborSamplesParameter.ActualValue.Value; … … 104 98 } 105 99 106 var data = AlglibUtil.Prepare AndScaleInputMatrix(problemData.Dataset, problemData.AllowedInputVariables,107 problemData.TrainingIndices, scaling);100 var data = AlglibUtil.PrepareInputMatrix(problemData.Dataset, problemData.AllowedInputVariables, 101 problemData.TrainingIndices); 108 102 var classes = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices).ToArray(); 109 103 -
trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Nca/NcaModel.cs
r8528 r9272 33 33 34 34 [Storable] 35 private Scaling scaling;36 [Storable]37 35 private double[,] transformationMatrix; 38 36 public double[,] TransformationMatrix { … … 52 50 protected NcaModel(NcaModel original, Cloner cloner) 53 51 : base(original, cloner) { 54 this.scaling = cloner.Clone(original.scaling);55 52 this.transformationMatrix = (double[,])original.transformationMatrix.Clone(); 56 53 this.allowedInputVariables = (string[])original.allowedInputVariables.Clone(); … … 59 56 this.classValues = (double[])original.classValues.Clone(); 60 57 } 61 public NcaModel(int k, double[,] transformationMatrix, Dataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, Scaling scaling,double[] classValues) {58 public NcaModel(int k, double[,] transformationMatrix, Dataset dataset, IEnumerable<int> rows, string targetVariable, IEnumerable<string> allowedInputVariables, double[] classValues) { 62 59 Name = ItemName; 63 60 Description = ItemDescription; 64 this.scaling = scaling;65 61 this.transformationMatrix = (double[,])transformationMatrix.Clone(); 66 62 this.allowedInputVariables = allowedInputVariables.ToArray(); … … 90 86 91 87 public double[,] Reduce(Dataset dataset, IEnumerable<int> rows) { 92 var scaledData = AlglibUtil.PrepareAndScaleInputMatrix(dataset, allowedInputVariables, rows, scaling); 88 var data = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables, rows); 89 93 90 var targets = dataset.GetDoubleValues(targetVariable, rows).ToArray(); 94 var result = new double[ scaledData.GetLength(0), transformationMatrix.GetLength(1) + 1];95 for (int i = 0; i < scaledData.GetLength(0); i++)96 for (int j = 0; j < scaledData.GetLength(1); j++) {91 var result = new double[data.GetLength(0), transformationMatrix.GetLength(1) + 1]; 92 for (int i = 0; i < data.GetLength(0); i++) 93 for (int j = 0; j < data.GetLength(1); j++) { 97 94 for (int x = 0; x < transformationMatrix.GetLength(1); x++) { 98 result[i, x] += scaledData[i, j] * transformationMatrix[j, x];95 result[i, x] += data[i, j] * transformationMatrix[j, x]; 99 96 } 100 97 result[i, transformationMatrix.GetLength(1)] = targets[i];
Note: See TracChangeset
for help on using the changeset viewer.