Changeset 14239 for branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassification.cs
- Timestamp:
- 08/05/16 17:40:11 (8 years ago)
- Location:
- branches/symbreg-factors-2650
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650
- Property svn:mergeinfo changed
/trunk/sources (added) merged: 14234-14236
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis
- Property svn:mergeinfo changed
/trunk/sources/HeuristicLab.Algorithms.DataAnalysis (added) merged: 14235-14236
- Property svn:mergeinfo changed
-
branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourClassification.cs
r14185 r14239 40 40 private const string KParameterName = "K"; 41 41 private const string NearestNeighbourClassificationModelResultName = "Nearest neighbour classification solution"; 42 private const string WeightsParameterName = "Weights"; 43 42 44 43 45 #region parameter properties 44 46 public IFixedValueParameter<IntValue> KParameter { 45 47 get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; } 48 } 49 public IValueParameter<DoubleArray> WeightsParameter { 50 get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; } 46 51 } 47 52 #endregion … … 53 58 else KParameter.Value.Value = value; 54 59 } 60 } 61 public DoubleArray Weights { 62 get { return WeightsParameter.Value; } 63 set { WeightsParameter.Value = value; } 55 64 } 56 65 #endregion … … 64 73 : base() { 65 74 Parameters.Add(new FixedValueParameter<IntValue>(KParameterName, "The number of nearest neighbours to consider for regression.", new IntValue(3))); 75 Parameters.Add(new OptionalValueParameter<DoubleArray>(WeightsParameterName, "Optional: use weights to specify individual scaling values for all features. If not set the weights are calculated automatically (each feature is scaled to unit variance)")); 66 76 Problem = new ClassificationProblem(); 67 77 } 68 78 [StorableHook(HookType.AfterDeserialization)] 69 private void AfterDeserialization() { } 79 private void AfterDeserialization() { 80 // BackwardsCompatibility3.3 81 #region Backwards compatible code, remove with 3.4 82 if (!Parameters.ContainsKey(WeightsParameterName)) { 83 Parameters.Add(new OptionalValueParameter<DoubleArray>(WeightsParameterName, "Optional: use weights to specify individual scaling values for all features. If not set the weights are calculated automatically (each feature is scaled to unit variance)")); 84 } 85 #endregion 86 } 70 87 71 88 public override IDeepCloneable Clone(Cloner cloner) { … … 75 92 #region nearest neighbour 76 93 protected override void Run() { 77 var solution = CreateNearestNeighbourClassificationSolution(Problem.ProblemData, K); 94 double[] weights = null; 95 if (Weights != null) weights = Weights.CloneAsArray(); 96 var solution = CreateNearestNeighbourClassificationSolution(Problem.ProblemData, K, weights); 78 97 Results.Add(new Result(NearestNeighbourClassificationModelResultName, "The nearest neighbour classification solution.", solution)); 79 98 } 80 99 81 public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k ) {100 public static IClassificationSolution CreateNearestNeighbourClassificationSolution(IClassificationProblemData problemData, int k, double[] weights = null) { 82 101 var problemDataClone = (IClassificationProblemData)problemData.Clone(); 83 return new NearestNeighbourClassificationSolution(Train(problemDataClone, k ), problemDataClone);102 return new NearestNeighbourClassificationSolution(Train(problemDataClone, k, weights), problemDataClone); 84 103 } 85 104 86 public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k ) {105 public static INearestNeighbourModel Train(IClassificationProblemData problemData, int k, double[] weights = null) { 87 106 return new NearestNeighbourModel(problemData.Dataset, 88 107 problemData.TrainingIndices, … … 90 109 problemData.TargetVariable, 91 110 problemData.AllowedInputVariables, 111 weights, 92 112 problemData.ClassValues.ToArray()); 93 113 }
Note: See TracChangeset
for help on using the changeset viewer.