Changeset 8425
- Timestamp:
- 08/07/12 15:14:34 (12 years ago)
- Location:
- branches/NCA
- Files:
-
- 6 added
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/NCA
- Property svn:ignore
-
old new 2 2 *.user 3 3 TestResults 4 _ReSharper.NCA
-
- Property svn:ignore
-
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/HeuristicLab.Algorithms.NCA-3.3.csproj
r8420 r8425 89 89 <Private>False</Private> 90 90 </Reference> 91 <Reference Include="HeuristicLab.Random-3.3, Version=3.3.0.0, Culture=neutral, PublicKeyToken=ba48961d6f65dcec, processorArchitecture=MSIL" /> 91 92 <Reference Include="System" /> 92 93 <Reference Include="System.Core" /> … … 98 99 </ItemGroup> 99 100 <ItemGroup> 101 <Compile Include="Initialization\INCAInitializer.cs" /> 100 102 <Compile Include="INCAModel.cs" /> 103 <Compile Include="Initialization\LDAInitializer.cs" /> 104 <Compile Include="Initialization\PCAInitializer.cs" /> 105 <Compile Include="Initialization\RandomInitializer.cs" /> 101 106 <Compile Include="Matrix.cs" /> 102 107 <Compile Include="NCA.cs" /> -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NCA.cs
r8412 r8425 20 20 #endregion 21 21 22 using System.Linq; 22 23 using HeuristicLab.Algorithms.DataAnalysis; 23 24 using HeuristicLab.Common; … … 27 28 using HeuristicLab.Parameters; 28 29 using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; 30 using HeuristicLab.PluginInfrastructure; 29 31 using HeuristicLab.Problems.DataAnalysis; 30 32 … … 34 36 /// </summary> 35 37 [Item("NCA", "Neihborhood Components Analysis is described in J. Goldberger, S. Roweis, G. Hinton, R. Salakhutdinov. 2005. Neighbourhood Component Analysis. Advances in Neural Information Processing Systems, 17. pp. 513-520.")] 36 [Creatable(" Algorithms")]38 [Creatable("Data Analysis")] 37 39 [StorableClass] 38 40 public sealed class NCA : FixedDataAnalysisAlgorithm<IClassificationProblem> { … … 43 45 public IValueLookupParameter<IntValue> ReduceDimensionsParameter { 44 46 get { return (IValueLookupParameter<IntValue>)Parameters["ReduceDimensions"]; } 47 } 48 private IConstrainedValueParameter<INCAInitializer> InitializationParameter { 49 get { return (IConstrainedValueParameter<INCAInitializer>)Parameters["Initialization"]; } 45 50 } 46 51 #endregion … … 62 67 Parameters.Add(new ValueLookupParameter<IntValue>("k", "The k for the nearest neighbor.", new IntValue(1))); 63 68 Parameters.Add(new ValueLookupParameter<IntValue>("ReduceDimensions", "The number of dimensions that NCA should reduce the data to.", new IntValue(2))); 69 Parameters.Add(new ConstrainedValueParameter<INCAInitializer>("Initialization", "Which method should be used to initialize the matrix. Typically LDA (linear discriminant analysis) should provide a good estimate.")); 70 71 INCAInitializer defaultInitializer = null; 72 foreach (var initializer in ApplicationManager.Manager.GetInstances<INCAInitializer>().OrderBy(x => x.ItemName)) { 73 if (initializer is LDAInitializer) defaultInitializer = initializer; 74 InitializationParameter.ValidValues.Add(initializer); 75 } 76 if (defaultInitializer != null) InitializationParameter.Value = defaultInitializer; 64 77 65 78 Problem = new ClassificationProblem(); … … 75 88 76 89 protected override void Run() { 77 var classification = NeighborhoodComponentsAnalysis.CreateNCASolution(Problem.ProblemData, K.Value, ReduceDimensions.Value );90 var classification = NeighborhoodComponentsAnalysis.CreateNCASolution(Problem.ProblemData, K.Value, ReduceDimensions.Value, InitializationParameter.Value); 78 91 Results.Add(new Result("ClassificationSolution", "The classification solution.", classification)); 79 92 } -
branches/NCA/HeuristicLab.Algorithms.NCA/3.3/NeighborhoodComponentsAnalysis.cs
r8422 r8425 29 29 public class NeighborhoodComponentsAnalysis { 30 30 31 public static INCAModel Train(IClassificationProblemData data, int k, int reduceDimensions ) {31 public static INCAModel Train(IClassificationProblemData data, int k, int reduceDimensions, INCAInitializer initializer) { 32 32 var instances = data.TrainingIndices.Count(); 33 33 var attributes = data.AllowedInputVariables.Count(); 34 34 35 /*alglib.pcabuildbasis( 36 double[,] x, 37 int npoints, 38 int nvars, 39 out int info, 40 out double[] s2, 41 out double[,] v)*/ 42 43 var matrix = new double[attributes * reduceDimensions]; 44 45 // TODO: make some more clever initialization of matrix 46 var rand = new Random(); 47 var counter = 0; 48 for (var i = 0; i < attributes; i++) 49 for (var j = 0; j < reduceDimensions; j++) 50 matrix[counter++] = rand.NextDouble(); 35 double[] matrix = initializer.Initialize(data, reduceDimensions); 51 36 52 37 alglib.mincgstate state; … … 60 45 61 46 var transformationMatrix = new double[attributes, reduceDimensions]; 62 counter = 0;47 var counter = 0; 63 48 for (var i = 0; i < attributes; i++) 64 49 for (var j = 0; j < reduceDimensions; j++) … … 143 128 } 144 129 145 public static NCAClassificationSolution CreateNCASolution(IClassificationProblemData problemData, int k, int reduceDimensions ) {146 return new NCAClassificationSolution(problemData, Train(problemData, k, reduceDimensions ));130 public static NCAClassificationSolution CreateNCASolution(IClassificationProblemData problemData, int k, int reduceDimensions, INCAInitializer initializer) { 131 return new NCAClassificationSolution(problemData, Train(problemData, k, reduceDimensions, initializer)); 147 132 } 148 133
Note: See TracChangeset
for help on using the changeset viewer.