Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
09/26/16 18:25:24 (8 years ago)
Author:
gkronber
Message:

#2652: merged r14235 from trunk to stable

Location:
stable
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.DataAnalysis

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/NearestNeighbour/NearestNeighbourRegression.cs

    r14186 r14308  
    3939    private const string KParameterName = "K";
    4040    private const string NearestNeighbourRegressionModelResultName = "Nearest neighbour regression solution";
     41    private const string WeightsParameterName = "Weights";
    4142
    4243    #region parameter properties
    4344    public IFixedValueParameter<IntValue> KParameter {
    4445      get { return (IFixedValueParameter<IntValue>)Parameters[KParameterName]; }
     46    }
     47
     48    public IValueParameter<DoubleArray> WeightsParameter {
     49      get { return (IValueParameter<DoubleArray>)Parameters[WeightsParameterName]; }
    4550    }
    4651    #endregion
     
    5257        else KParameter.Value.Value = value;
    5358      }
     59    }
     60
     61    public DoubleArray Weights {
     62      get { return WeightsParameter.Value; }
     63      set { WeightsParameter.Value = value; }
    5464    }
    5565    #endregion
     
    6373      : base() {
    6474      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)"));
    6576      Problem = new RegressionProblem();
    6677    }
     78
    6779    [StorableHook(HookType.AfterDeserialization)]
    68     private void AfterDeserialization() { }
     80    private void AfterDeserialization() {
     81      // BackwardsCompatibility3.3
     82      #region Backwards compatible code, remove with 3.4
     83      if (!Parameters.ContainsKey(WeightsParameterName)) {
     84        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)"));
     85      }
     86      #endregion
     87    }
    6988
    7089    public override IDeepCloneable Clone(Cloner cloner) {
     
    7493    #region nearest neighbour
    7594    protected override void Run() {
    76       var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K);
     95      double[] weights = null;
     96      if (Weights != null) weights = Weights.CloneAsArray();
     97      var solution = CreateNearestNeighbourRegressionSolution(Problem.ProblemData, K, weights);
    7798      Results.Add(new Result(NearestNeighbourRegressionModelResultName, "The nearest neighbour regression solution.", solution));
    7899    }
    79100
    80     public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k) {
     101    public static IRegressionSolution CreateNearestNeighbourRegressionSolution(IRegressionProblemData problemData, int k, double[] weights = null) {
    81102      var clonedProblemData = (IRegressionProblemData)problemData.Clone();
    82       return new NearestNeighbourRegressionSolution(Train(problemData, k), clonedProblemData);
     103      return new NearestNeighbourRegressionSolution(Train(problemData, k, weights), clonedProblemData);
    83104    }
    84105
    85     public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k) {
     106    public static INearestNeighbourModel Train(IRegressionProblemData problemData, int k, double[] weights = null) {
    86107      return new NearestNeighbourModel(problemData.Dataset,
    87108        problemData.TrainingIndices,
    88109        k,
    89110        problemData.TargetVariable,
    90         problemData.AllowedInputVariables);
     111        problemData.AllowedInputVariables,
     112        weights);
    91113    }
    92114    #endregion
Note: See TracChangeset for help on using the changeset viewer.