Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2619 for trunk/sources


Ignore:
Timestamp:
01/10/10 21:01:03 (15 years ago)
Author:
gkronber
Message:

Implemented #834 (IPredictor.Predict() should return an IEnumerable<double> instead of an double[]).

Location:
trunk/sources
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.ArtificialNeuralNetworks/3.2/Predictor.cs

    r2562 r2619  
    3737    }
    3838
    39     public override double[] Predict(Dataset input, int start, int end) {
     39    public override IEnumerable<double> Predict(Dataset input, int start, int end) {
    4040
    4141      if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end");
    4242      if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end");
    43       double[] result = new double[end - start];
    44       for (int i = 0; i < result.Length; i++) {
    45         try {
    46           double[] output = new double[1];
    47           double[] inputRow = new double[input.Columns - 1];
    48           for (int c = 1; c < inputRow.Length; c++) {
    49             inputRow[c - 1] = input.GetValue(i + start, c);
    50           }
    51           alglib.mlpbase.multilayerperceptron p = perceptron.Perceptron;
    52           alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output);
    53           perceptron.Perceptron = p;
    54           result[i] = Math.Max(Math.Min(output[0], UpperPredictionLimit), LowerPredictionLimit);
     43
     44      for (int i = 0; i < end - start; i++) {
     45        double[] output = new double[1];
     46        double[] inputRow = new double[input.Columns - 1];
     47        for (int c = 1; c < inputRow.Length; c++) {
     48          inputRow[c - 1] = input.GetValue(i + start, c);
    5549        }
    56         catch (ArgumentException) {
    57           result[i] = double.NaN;
    58         }
     50        alglib.mlpbase.multilayerperceptron p = perceptron.Perceptron;
     51        alglib.mlpbase.mlpprocess(ref p, ref inputRow, ref output);
     52        perceptron.Perceptron = p;
     53        yield return Math.Max(Math.Min(output[0], UpperPredictionLimit), LowerPredictionLimit);
    5954      }
    60       return result;
    6155    }
    6256
  • trunk/sources/HeuristicLab.GP.StructureIdentification/3.3/Predictor.cs

    r2567 r2619  
    2929using System.Xml;
    3030using HeuristicLab.DataAnalysis;
     31using System.Linq;
    3132
    3233namespace HeuristicLab.GP.StructureIdentification {
     
    5051    }
    5152
    52     public override double[] Predict(Dataset input, int start, int end) {
     53    public override IEnumerable<double> Predict(Dataset input, int start, int end) {
    5354      treeEvaluator.UpperEvaluationLimit = UpperPredictionLimit;
    5455      treeEvaluator.LowerEvaluationLimit = LowerPredictionLimit;
     
    5657      if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end");
    5758      if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end");
    58       treeEvaluator.PrepareForEvaluation(input, functionTree.FunctionTree);
    59       double[] result = new double[end - start];
    60       for (int i = 0; i < result.Length; i++) {
    61         try {
    62           result[i] = treeEvaluator.Evaluate(i + start);
    63         }
    64         catch (ArgumentException) {
    65           result[i] = double.NaN;
    66         }
    67       }
    68       return result;
     59      return treeEvaluator.Evaluate(input, functionTree.FunctionTree, Enumerable.Range(start, end - start));
    6960    }
    7061
  • trunk/sources/HeuristicLab.Modeling/3.2/IPredictor.cs

    r2381 r2619  
    3030    double UpperPredictionLimit { get; set; }
    3131    double LowerPredictionLimit { get; set; }
    32     double[] Predict(Dataset dataset, int start, int end);
     32    IEnumerable<double> Predict(Dataset dataset, int start, int end);
    3333    IEnumerable<string> GetInputVariables();
    3434  }
  • trunk/sources/HeuristicLab.Modeling/3.2/PredictorBase.cs

    r2381 r2619  
    3131    public double UpperPredictionLimit { get; set; }
    3232    public double LowerPredictionLimit { get; set; }
    33     public abstract double[] Predict(Dataset dataset, int start, int end);
     33    public abstract IEnumerable<double> Predict(Dataset dataset, int start, int end);
    3434    public abstract IEnumerable<string> GetInputVariables();
    3535
  • trunk/sources/HeuristicLab.Modeling/3.2/VariableEvaluationImpactCalculator.cs

    r2559 r2619  
    8585      Dataset dirtyDataset = (Dataset)dataset.Clone();
    8686      IPredictor dirtyPredictor = (IPredictor)predictor.Clone();
    87       double[] referenceValues = predictor.Predict(dataset, start, end);
     87      double[] referenceValues = predictor.Predict(dataset, start, end).ToArray();
    8888
    8989      double mean;
     
    101101            mean = dataset.GetMean(variableName, start, end);
    102102            oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
    103             newValues = dirtyPredictor.Predict(dirtyDataset, start, end);
     103            newValues = dirtyPredictor.Predict(dirtyDataset, start, end).ToArray();
    104104            evaluationImpacts[variableName] = 1 - CalculateVAF(referenceValues, newValues);
    105105            dirtyDataset.ReplaceVariableValues(variableName, oldValues, start, end);
  • trunk/sources/HeuristicLab.Modeling/3.2/VariableQualityImpactCalculator.cs

    r2559 r2619  
    8585      IPredictor dirtyPredictor = (IPredictor)predictor.Clone();
    8686
    87       double[] predictedValues = predictor.Predict(dataset, start, end);
     87      double[] predictedValues = predictor.Predict(dataset, start, end).ToArray();
    8888      double[] targetValues = dataset.GetVariableValues(targetVariableName, start, end);
    8989
     
    105105          mean = dataset.GetMean(variableName, start, end);
    106106          oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
    107           predictedValues = dirtyPredictor.Predict(dirtyDataset, start, end);
     107          predictedValues = dirtyPredictor.Predict(dirtyDataset, start, end).ToArray();
    108108          newMSE = CalculateMSE(predictedValues, targetValues);
    109109          evaluationImpacts[variableName] = newMSE / oldMSE;
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs

    r2436 r2619  
    6666    }
    6767
    68     public override double[] Predict(Dataset input, int start, int end) {
     68    public override IEnumerable<double> Predict(Dataset input, int start, int end) {
    6969      if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end");
    7070      if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end");
     
    7878      int targetVariableIndex = input.GetVariableIndex(targetVariable);
    7979      int rows = end - start;
    80       double[] result = new double[rows];
     80      //double[] result = new double[rows];
    8181      int problemRow = 0;
    8282      for (int resultRow = 0; resultRow < rows; resultRow++) {
    8383        if (double.IsNaN(input.GetValue(resultRow, targetVariableIndex)))
    84           result[resultRow] = UpperPredictionLimit;
     84          yield return UpperPredictionLimit;
    8585        else if (resultRow + maxTimeOffset < 0) {
    86           result[resultRow] = UpperPredictionLimit;
    8786          problemRow++;
     87          yield return UpperPredictionLimit;
    8888        } else {
    89           result[resultRow] = Math.Max(Math.Min(SVM.Prediction.Predict(model, scaledProblem.X[problemRow++]), UpperPredictionLimit), LowerPredictionLimit);
     89          yield return Math.Max(Math.Min(SVM.Prediction.Predict(model, scaledProblem.X[problemRow++]), UpperPredictionLimit), LowerPredictionLimit);
    9090        }
    9191      }
    92       return result;
    9392    }
    9493
Note: See TracChangeset for help on using the changeset viewer.