Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/01/11 17:48:53 (13 years ago)
Author:
mkommend
Message:

#1479: Integrated trunk changes.

Location:
branches/GP.Grammar.Editor/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • branches/GP.Grammar.Editor/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorClassificationSolution.cs

    r5809 r6618  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Drawing;
    25 using System.Linq;
    2622using HeuristicLab.Common;
    2723using HeuristicLab.Core;
     
    4945    public SupportVectorClassificationSolution(SupportVectorMachineModel model, IClassificationProblemData problemData)
    5046      : base(model, problemData) {
     47      RecalculateResults();
    5148    }
    5249
     
    5451      return new SupportVectorClassificationSolution(this, cloner);
    5552    }
     53
     54    protected override void RecalculateResults() {
     55      CalculateResults();
     56    }
    5657  }
    5758}
  • branches/GP.Grammar.Editor/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs

    r5861 r6618  
    9898      this.targetVariable = original.targetVariable;
    9999      this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
     100      foreach (var dataset in original.cachedPredictions.Keys) {
     101        this.cachedPredictions.Add(cloner.Clone(dataset), (double[])original.cachedPredictions[dataset].Clone());
     102      }
    100103      if (original.classValues != null)
    101104        this.classValues = (double[])original.classValues.Clone();
     
    123126      return GetEstimatedValuesHelper(dataset, rows);
    124127    }
    125     #endregion
     128    public SupportVectorRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
     129      return new SupportVectorRegressionSolution(this, problemData);
     130    }
     131    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
     132      return CreateRegressionSolution(problemData);
     133    }
     134    #endregion
     135
    126136    #region IClassificationModel Members
    127137    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
     
    144154      }
    145155    }
    146     #endregion
     156
     157    public SupportVectorClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
     158      return new SupportVectorClassificationSolution(this, problemData);
     159    }
     160    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
     161      return CreateClassificationSolution(problemData);
     162    }
     163    #endregion
     164    // cache for predictions, which is cloned but not persisted, must be cleared when the model is changed
     165    private Dictionary<Dataset, double[]> cachedPredictions = new Dictionary<Dataset, double[]>();
    147166    private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) {
     167      if (!cachedPredictions.ContainsKey(dataset)) {
     168        // create an array of cached predictions which is initially filled with NaNs
     169        double[] predictions = Enumerable.Repeat(double.NaN, dataset.Rows).ToArray();
     170        CalculatePredictions(dataset, rows, predictions);
     171        cachedPredictions.Add(dataset, predictions);
     172      }
     173      // get the array of predictions and select the subset of requested rows
     174      double[] p = cachedPredictions[dataset];
     175      var requestedPredictions = from r in rows
     176                                 select p[r];
     177      // check if the requested predictions contain NaNs
     178      // (this means for the request rows some predictions have not been cached)
     179      if (requestedPredictions.Any(x => double.IsNaN(x))) {
     180        // updated the predictions for currently requested rows
     181        CalculatePredictions(dataset, rows, p);
     182        cachedPredictions[dataset] = p;
     183        // now we can be sure that for the current rows all predictions are available
     184        return from r in rows
     185               select p[r];
     186      } else {
     187        // there were no NaNs => just return the cached predictions
     188        return requestedPredictions;
     189      }
     190    }
     191
     192    private void CalculatePredictions(Dataset dataset, IEnumerable<int> rows, double[] predictions) {
     193      // calculate and cache predictions for the currently requested rows
    148194      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
    149195      SVM.Problem scaledProblem = Scaling.Scale(RangeTransform, problem);
    150196
    151       foreach (var row in Enumerable.Range(0, scaledProblem.Count)) {
    152         yield return SVM.Prediction.Predict(Model, scaledProblem.X[row]);
    153       }
    154     }
     197      // row is the index in the original dataset,
     198      // i is the index in the scaled dataset (containing only the necessary rows)
     199      int i = 0;
     200      foreach (var row in rows) {
     201        predictions[row] = SVM.Prediction.Predict(Model, scaledProblem.X[i]);
     202        i++;
     203      }
     204    }
     205
    155206    #region events
    156207    public event EventHandler Changed;
    157208    private void OnChanged(EventArgs e) {
     209      cachedPredictions.Clear();
    158210      var handlers = Changed;
    159211      if (handlers != null)
  • branches/GP.Grammar.Editor/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorRegressionSolution.cs

    r5809 r6618  
    2020#endregion
    2121
    22 using System;
    23 using System.Collections.Generic;
    24 using System.Drawing;
    25 using System.Linq;
    2622using HeuristicLab.Common;
    2723using HeuristicLab.Core;
     
    4945    public SupportVectorRegressionSolution(SupportVectorMachineModel model, IRegressionProblemData problemData)
    5046      : base(model, problemData) {
     47      RecalculateResults();
    5148    }
    5249
Note: See TracChangeset for help on using the changeset viewer.