Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.SupportVectorMachines/3.2/Predictor.cs @ 2413

Last change on this file since 2413 was 2413, checked in by gkronber, 14 years ago

Show input variable names, max/min time offset and upper/lower prediction limits in SVMModelView. #772

File size: 6.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
4 *
5 * This file is part of HeuristicLab.
6 *
7 * HeuristicLab is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation, either version 3 of the License, or
10 * (at your option) any later version.
11 *
12 * HeuristicLab is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with HeuristicLab. If not, see <http://www.gnu.org/licenses/>.
19 */
20#endregion
21
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using System.Linq;
27using HeuristicLab.Core;
28using System.Globalization;
29using System.IO;
30using HeuristicLab.Modeling;
31using SVM;
32using HeuristicLab.DataAnalysis;
33
34namespace HeuristicLab.SupportVectorMachines {
35  public class Predictor : PredictorBase {
36    private SVMModel svmModel;
37    public SVMModel Model {
38      get { return svmModel; }
39    }
40
41    private Dictionary<string, int> variableNames = new Dictionary<string, int>();
42    private string targetVariable;
43    private int minTimeOffset;
44    public int MinTimeOffset {
45      get { return minTimeOffset; }
46    }
47    private int maxTimeOffset;
48    public int MaxTimeOffset {
49      get { return maxTimeOffset; }
50    }
51
52    public Predictor() : base() { } // for persistence
53
54    public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames) :
55      this(model, targetVariable, variableNames, 0, 0) {
56    }
57
58    public Predictor(SVMModel model, string targetVariable, Dictionary<string, int> variableNames, int minTimeOffset, int maxTimeOffset)
59      : base() {
60      this.svmModel = model;
61      this.targetVariable = targetVariable;
62      this.variableNames = variableNames;
63      this.minTimeOffset = minTimeOffset;
64      this.maxTimeOffset = maxTimeOffset;
65    }
66
67    public override double[] Predict(Dataset input, int start, int end) {
68      if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end");
69      if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end");
70      RangeTransform transform = svmModel.RangeTransform;
71      Model model = svmModel.Model;
72      // maps columns of the current input dataset to the columns that were originally used in training
73      Dictionary<int, int> newIndex = new Dictionary<int, int>();
74      foreach (var pair in variableNames) {
75        newIndex[input.GetVariableIndex(pair.Key)] = pair.Value;
76      }
77
78
79      Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), newIndex,
80        start, end, minTimeOffset, maxTimeOffset);
81      Problem scaledProblem = SVM.Scaling.Scale(p, transform);
82
83      int targetVariableIndex = input.GetVariableIndex(targetVariable);
84      int rows = end - start;
85      double[] result = new double[rows];
86      int problemRow = 0;
87      for (int resultRow = 0; resultRow < rows; resultRow++) {
88        if (double.IsNaN(input.GetValue(resultRow, targetVariableIndex)))
89          result[resultRow] = UpperPredictionLimit;
90        else
91          result[resultRow] = Math.Max(Math.Min(SVM.Prediction.Predict(model, scaledProblem.X[problemRow++]), UpperPredictionLimit), LowerPredictionLimit);
92      }
93      return result;
94    }
95
96    public override IEnumerable<string> GetInputVariables() {
97      return from pair in variableNames
98             orderby pair.Value
99             select pair.Key;
100    }
101
102    public override IView CreateView() {
103      return new PredictorView(this);
104    }
105
106    public override object Clone(IDictionary<Guid, object> clonedObjects) {
107      Predictor clone = (Predictor)base.Clone(clonedObjects);
108      clone.svmModel = (SVMModel)Auxiliary.Clone(svmModel, clonedObjects);
109      clone.targetVariable = targetVariable;
110      clone.variableNames = new Dictionary<string, int>(variableNames);
111      clone.minTimeOffset = minTimeOffset;
112      clone.maxTimeOffset = maxTimeOffset;
113      return clone;
114    }
115
116    public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
117      XmlNode node = base.GetXmlNode(name, document, persistedObjects);
118      XmlAttribute targetVarAttr = document.CreateAttribute("TargetVariable");
119      targetVarAttr.Value = targetVariable;
120      node.Attributes.Append(targetVarAttr);
121      XmlAttribute minTimeOffsetAttr = document.CreateAttribute("MinTimeOffset");
122      XmlAttribute maxTimeOffsetAttr = document.CreateAttribute("MaxTimeOffset");
123      minTimeOffsetAttr.Value = XmlConvert.ToString(minTimeOffset);
124      maxTimeOffsetAttr.Value = XmlConvert.ToString(maxTimeOffset);
125      node.Attributes.Append(minTimeOffsetAttr);
126      node.Attributes.Append(maxTimeOffsetAttr);
127      node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects));
128      XmlNode variablesNode = document.CreateElement("Variables");
129      foreach (var pair in variableNames) {
130        XmlNode pairNode = document.CreateElement("Variable");
131        XmlAttribute nameAttr = document.CreateAttribute("Name");
132        XmlAttribute indexAttr = document.CreateAttribute("Index");
133        nameAttr.Value = pair.Key;
134        indexAttr.Value = XmlConvert.ToString(pair.Value);
135        pairNode.Attributes.Append(nameAttr);
136        pairNode.Attributes.Append(indexAttr);
137        variablesNode.AppendChild(pairNode);
138      }
139      node.AppendChild(variablesNode);
140      return node;
141    }
142
143    public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
144      base.Populate(node, restoredObjects);
145      targetVariable = node.Attributes["TargetVariable"].Value;
146      svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects);
147
148      if (node.Attributes["MinTimeOffset"] != null) minTimeOffset = XmlConvert.ToInt32(node.Attributes["MinTimeOffset"].Value);
149      if (node.Attributes["MaxTimeOffset"] != null) maxTimeOffset = XmlConvert.ToInt32(node.Attributes["MaxTimeOffset"].Value);
150      variableNames = new Dictionary<string, int>();
151      XmlNode variablesNode = node.ChildNodes[1];
152      foreach (XmlNode pairNode in variablesNode.ChildNodes) {
153        variableNames[pairNode.Attributes["Name"].Value] = XmlConvert.ToInt32(pairNode.Attributes["Index"].Value);
154      }
155    }
156  }
157}
Note: See TracBrowser for help on using the repository browser.