Free cookie consent management tool by TermsFeed Policy Generator

Changeset 2347 for trunk


Ignore:
Timestamp:
09/14/09 10:08:40 (15 years ago)
Author:
gkronber
Message:

Worked on SVR algorithm for time-series. #705

Location:
trunk/sources/HeuristicLab.SupportVectorMachines/3.2
Files:
5 edited

Legend:

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

    r2328 r2347  
    4040    private Dictionary<string, int> variableNames = new Dictionary<string, int>();
    4141    private string targetVariable;
     42    private int minTimeOffset;
     43    private int maxTimeOffset;
    4244
    4345    public Predictor() : base() { } // for persistence
    4446
    45     public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames)
     47    public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames) :
     48      this(model, targetVariable, variableNames, 0, 0) {
     49    }
     50
     51    public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames, int minTimeOffset, int maxTimeOffset)
    4652      : base() {
    4753      this.svmModel = model;
    4854      this.targetVariable = targetVariable;
    4955      this.variableNames = variableNames;
     56      this.minTimeOffset = minTimeOffset;
     57      this.maxTimeOffset = maxTimeOffset;
    5058    }
    5159
     
    6169      }
    6270
    63       Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex, start, end);
     71      Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex,
     72        start, end, minTimeOffset, maxTimeOffset);
    6473      Problem scaledProblem = SVM.Scaling.Scale(p, transform);
    6574
     
    8291      clone.targetVariable = targetVariable;
    8392      clone.variableNames = new Dictionary<string, int>(variableNames);
     93      clone.minTimeOffset = minTimeOffset;
     94      clone.maxTimeOffset = maxTimeOffset;
    8495      return clone;
    8596    }
     
    90101      targetVarAttr.Value = targetVariable;
    91102      node.Attributes.Append(targetVarAttr);
     103      XmlAttribute minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset");
     104      XmlAttribute maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset");
     105      minTimeOffsetAttr.Value = XmlConvert.ToString(minTimeOffset);
     106      maxTimeOffsetAttr.Value = XmlConvert.ToString(maxTimeOffset);
     107      node.Attributes.Append(minTimeOffsetAttr);
     108      node.Attributes.Append(maxTimeOffsetAttr);
    92109      node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects));
    93110      XmlNode variablesNode = document.CreateElement("Variables");
     
    111128      svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects);
    112129
     130      if (node.Attributes["MinTimeOffset"] != null) minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value);
     131      if (node.Attributes["MaxTimeOffset"] != null) maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value);
    113132      variableNames = new Dictionary<string, int>();
    114133      XmlNode variablesNode = node.ChildNodes[1];
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SVMHelper.cs

    r2290 r2347  
    1010  public class SVMHelper {
    1111
    12     public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end) {
    13       return CreateSVMProblem(dataset, targetVariable, Enumerable.Range(0, dataset.Columns).ToDictionary<int, int>(x => x), start, end);
     12    public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, int start, int end, int minTimeOffset, int maxTimeOffset) {
     13      return CreateSVMProblem(dataset, targetVariable, Enumerable.Range(0, dataset.Columns).ToDictionary<int, int>(x => x), start, end, minTimeOffset, maxTimeOffset);
    1414    }
    1515
    16     public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end) {
     16    public static SVM.Problem CreateSVMProblem(Dataset dataset, int targetVariable, Dictionary<int, int> columnMapping, int start, int end, int minTimeOffset, int maxTimeOffset) {
    1717      int rowCount = end - start;
    1818      List<int> skippedFeatures = new List<int>();
     
    2424      }
    2525
    26       int maxColumns = dataset.Columns - skippedFeatures.Count();
     26      int maxColumns = (dataset.Columns - skippedFeatures.Count()) * (maxTimeOffset-minTimeOffset);
    2727
    2828      double[] targetVector = new double[rowCount];
     
    3636      List<SVM.Node> tempRow;
    3737      int addedRows = 0;
     38      int timeOffsetBase = columnMapping.Count;
    3839      for (int row = 0; row < rowCount; row++) {
    3940        tempRow = new List<SVM.Node>();
    4041        for (int col = 0; col < dataset.Columns; col++) {
    4142          if (!skippedFeatures.Contains(col) && col != targetVariable && columnMapping.ContainsKey(col)) {
    42             double value = dataset.GetValue(start + row, col);
    43             if (!double.IsNaN(value))
    44               tempRow.Add(new SVM.Node(columnMapping[col], value));
     43            for (int timeOffset = minTimeOffset; timeOffset <= maxTimeOffset; timeOffset++) {
     44              int actualColumn = columnMapping[col] * (maxTimeOffset - minTimeOffset) + (timeOffset - maxTimeOffset) + 1;
     45              double value = dataset.GetValue(start + row + timeOffset, col);
     46              if (!double.IsNaN(value))
     47                tempRow.Add(new SVM.Node(actualColumn, value));
     48            }
    4549          }
    4650        }
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorCreator.cs

    r2165 r2347  
    4242      AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
    4343      AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
     44      AddVariableInfo(new VariableInfo("MaxTimeOffset", "Maximal time offset for time-series prognosis", typeof(IntData), VariableKind.In));
     45      AddVariableInfo(new VariableInfo("MinTimeOffset", "Minimal time offset for time-series prognosis", typeof(IntData), VariableKind.In));
    4446
    4547      //SVM parameters
     
    6971      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
    7072      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
    71 
     73      int maxTimeOffset = GetVariableValue<IntData>("MaxTimeOffset", scope, true).Data;
     74      int minTimeOffset = GetVariableValue<IntData>("MinTimeOffset", scope, true).Data;
    7275      string svmType = GetVariableValue<StringData>("SVMType", scope, true).Data;
    7376      string svmKernelType = GetVariableValue<StringData>("SVMKernelType", scope, true).Data;
     
    8184      parameter.Gamma = GetVariableValue<DoubleData>("SVMGamma", scope, true).Data;
    8285
    83       SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end);
     86      SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset);
    8487      SVM.RangeTransform rangeTransform = SVM.Scaling.DetermineRange(problem);
    8588      SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, rangeTransform);
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorEvaluator.cs

    r2165 r2347  
    3838      AddVariableInfo(new VariableInfo("SamplesStart", "Start index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
    3939      AddVariableInfo(new VariableInfo("SamplesEnd", "End index of samples in dataset to evaluate", typeof(IntData), VariableKind.In));
    40 
     40      AddVariableInfo(new VariableInfo("MaxTimeOffset", "Maximal allowed time offset for input variables", typeof(IntData), VariableKind.In));
     41      AddVariableInfo(new VariableInfo("MinTimeOffset", "Minimal allowed time offset for input variables", typeof(IntData), VariableKind.In));
    4142      AddVariableInfo(new VariableInfo("SVMModel", "Represent the model learned by the SVM", typeof(SVMModel), VariableKind.In));
    4243      AddVariableInfo(new VariableInfo("Values", "Target vs predicted values", typeof(DoubleMatrixData), VariableKind.New | VariableKind.Out));
     
    4950      int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
    5051      int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
    51 
     52      int minTimeOffset = GetVariableValue<IntData>("MinTimeOffset", scope, true).Data;
     53      int maxTimeOffset = GetVariableValue<IntData>("MaxTimeOffset", scope, true).Data;
    5254      SVMModel modelData = GetVariableValue<SVMModel>("SVMModel", scope, true);
    53       SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end);
     55      SVM.Problem problem = SVMHelper.CreateSVMProblem(dataset, targetVariable, start, end, minTimeOffset, maxTimeOffset);
    5456      SVM.Problem scaledProblem = SVM.Scaling.Scale(problem, modelData.RangeTransform);
    5557
    5658      double[,] values = new double[scaledProblem.Count, 2];
    5759      for (int i = 0; i < scaledProblem.Count; i++) {
    58         values[i,0] = SVM.Prediction.Predict(modelData.Model, scaledProblem.X[i]);
    59         values[i,1] = dataset.GetValue(start + i,targetVariable);
     60        values[i, 0] = SVM.Prediction.Predict(modelData.Model, scaledProblem.X[i]);
     61        values[i, 1] = dataset.GetValue(start + i, targetVariable);
    6062      }
    6163
  • trunk/sources/HeuristicLab.SupportVectorMachines/3.2/SupportVectorRegression.cs

    r2344 r2347  
    107107    }
    108108
     109    public int MaxTimeOffset {
     110      get { return GetVariableInjector().GetVariable("MaxTimeOffset").GetValue<IntData>().Data; }
     111      set { GetVariableInjector().GetVariable("MaxTimeOffset").GetValue<IntData>().Data = value; }
     112    }
     113
     114    public int MinTimeOffset {
     115      get { return GetVariableInjector().GetVariable("MinTimeOffset").GetValue<IntData>().Data; }
     116      set { GetVariableInjector().GetVariable("MinTimeOffset").GetValue<IntData>().Data = value; }
     117    }
     118
    109119    public SupportVectorRegression() {
    110120      engine = new SequentialEngine.SequentialEngine();
     
    115125      MaxNuIndex = NuList.Data.Length;
    116126      MaxGammaIndex = GammaList.Data.Length;
     127      MaxTimeOffset = 0;
     128      MinTimeOffset = 0;
    117129    }
    118130
     
    147159      seq.AddSubOperator(new RandomInjector());
    148160
    149       DatasetShuffler shuffler = new DatasetShuffler();
    150       shuffler.GetVariableInfo("ShuffleStart").ActualName = "TrainingSamplesStart";
    151       shuffler.GetVariableInfo("ShuffleEnd").ActualName = "TrainingSamplesEnd";
    152       seq.AddSubOperator(shuffler);
     161      //DatasetShuffler shuffler = new DatasetShuffler();
     162      //shuffler.GetVariableInfo("ShuffleStart").ActualName = "TrainingSamplesStart";
     163      //shuffler.GetVariableInfo("ShuffleEnd").ActualName = "TrainingSamplesEnd";
     164      //seq.AddSubOperator(shuffler);
    153165      return seq;
    154166    }
     
    395407      injector.AddVariable(new HeuristicLab.Core.Variable("Type", new StringData("NU_SVR")));
    396408      injector.AddVariable(new HeuristicLab.Core.Variable("PunishmentFactor", new DoubleData(1000.0)));
    397 
     409      injector.AddVariable(new HeuristicLab.Core.Variable("MaxTimeOffset", new IntData()));
     410      injector.AddVariable(new HeuristicLab.Core.Variable("MinTimeOffset", new IntData()));
    398411      return injector;
    399412    }
Note: See TracChangeset for help on using the changeset viewer.