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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using System.Globalization;
|
---|
28 | using System.IO;
|
---|
29 | using HeuristicLab.Modeling;
|
---|
30 | using SVM;
|
---|
31 | using HeuristicLab.DataAnalysis;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.SupportVectorMachines {
|
---|
34 | public class Predictor : ItemBase, IPredictor {
|
---|
35 | private SVMModel svmModel;
|
---|
36 | private string targetVariable;
|
---|
37 |
|
---|
38 | public Predictor() : base() { } // for persistence
|
---|
39 |
|
---|
40 | public Predictor(SVMModel model, string targetVariable)
|
---|
41 | : base() {
|
---|
42 | this.svmModel = model;
|
---|
43 | this.targetVariable = targetVariable;
|
---|
44 | }
|
---|
45 |
|
---|
46 | public double[] Predict(Dataset input, int start, int end) {
|
---|
47 | if (start < 0 || end <= start) throw new ArgumentException("start must be larger than zero and strictly smaller than end");
|
---|
48 | if (end > input.Rows) throw new ArgumentOutOfRangeException("number of rows in input is smaller then end");
|
---|
49 | RangeTransform transform = svmModel.RangeTransform;
|
---|
50 | Model model = svmModel.Model;
|
---|
51 |
|
---|
52 | Problem p = SVMHelper.CreateSVMProblem(input, input.GetVariableIndex(targetVariable), start, end);
|
---|
53 | Problem scaledProblem = SVM.Scaling.Scale(p, transform);
|
---|
54 |
|
---|
55 | int rows = end - start;
|
---|
56 | int columns = input.Columns;
|
---|
57 | double[] result = new double[rows];
|
---|
58 | for (int row = 0; row < rows; row++) {
|
---|
59 | result[row] = SVM.Prediction.Predict(model, scaledProblem.X[row]);
|
---|
60 | }
|
---|
61 | return result;
|
---|
62 | }
|
---|
63 |
|
---|
64 | public override IView CreateView() {
|
---|
65 | return svmModel.CreateView();
|
---|
66 | }
|
---|
67 |
|
---|
68 | public override object Clone(IDictionary<Guid, object> clonedObjects) {
|
---|
69 | Predictor clone = (Predictor)base.Clone(clonedObjects);
|
---|
70 | clone.svmModel = (SVMModel)Auxiliary.Clone(svmModel, clonedObjects);
|
---|
71 | clone.targetVariable = targetVariable;
|
---|
72 | return clone;
|
---|
73 | }
|
---|
74 |
|
---|
75 | public override XmlNode GetXmlNode(string name, XmlDocument document, IDictionary<Guid, IStorable> persistedObjects) {
|
---|
76 | XmlNode node = base.GetXmlNode(name, document, persistedObjects);
|
---|
77 | XmlAttribute targetVarAttr = document.CreateAttribute("TargetVariable");
|
---|
78 | targetVarAttr.Value = targetVariable;
|
---|
79 | node.Attributes.Append(targetVarAttr);
|
---|
80 | node.AppendChild(PersistenceManager.Persist(svmModel, document, persistedObjects));
|
---|
81 | return node;
|
---|
82 | }
|
---|
83 |
|
---|
84 | public override void Populate(XmlNode node, IDictionary<Guid, IStorable> restoredObjects) {
|
---|
85 | base.Populate(node, restoredObjects);
|
---|
86 | targetVariable = node.Attributes["TargetVariable"].Value;
|
---|
87 | svmModel = (SVMModel)PersistenceManager.Restore(node.ChildNodes[0], restoredObjects);
|
---|
88 | }
|
---|
89 | }
|
---|
90 | }
|
---|