Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/SupportVectorMachine/SupportVectorMachineModel.cs @ 10216

Last change on this file since 10216 was 9456, checked in by swagner, 12 years ago

Updated copyright year and added some missing license headers (#1889)

File size: 8.0 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2013 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.IO;
25using System.Linq;
26using System.Text;
27using HeuristicLab.Common;
28using HeuristicLab.Core;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31using LibSVM;
32
33namespace HeuristicLab.Algorithms.DataAnalysis {
34  /// <summary>
35  /// Represents a support vector machine model.
36  /// </summary>
37  [StorableClass]
38  [Item("SupportVectorMachineModel", "Represents a support vector machine model.")]
39  public sealed class SupportVectorMachineModel : NamedItem, ISupportVectorMachineModel {
40
41    private svm_model model;
42    /// <summary>
43    /// Gets or sets the SVM model.
44    /// </summary>
45    public svm_model Model {
46      get { return model; }
47      set {
48        if (value != model) {
49          if (value == null) throw new ArgumentNullException();
50          model = value;
51          OnChanged(EventArgs.Empty);
52        }
53      }
54    }
55
56    /// <summary>
57    /// Gets or sets the range transformation for the model.
58    /// </summary>
59    private RangeTransform rangeTransform;
60    public RangeTransform RangeTransform {
61      get { return rangeTransform; }
62      set {
63        if (value != rangeTransform) {
64          if (value == null) throw new ArgumentNullException();
65          rangeTransform = value;
66          OnChanged(EventArgs.Empty);
67        }
68      }
69    }
70
71    public Dataset SupportVectors {
72      get {
73        var data = new double[Model.sv_coef.Length, allowedInputVariables.Count()];
74        for (int i = 0; i < Model.sv_coef.Length; i++) {
75          var sv = Model.SV[i];
76          for (int j = 0; j < sv.Length; j++) {
77            data[i, j] = sv[j].value;
78          }
79        }
80        return new Dataset(allowedInputVariables, data);
81      }
82    }
83
84    [Storable]
85    private string targetVariable;
86    [Storable]
87    private string[] allowedInputVariables;
88    [Storable]
89    private double[] classValues; // only for SVM classification models
90
91    [StorableConstructor]
92    private SupportVectorMachineModel(bool deserializing) : base(deserializing) { }
93    private SupportVectorMachineModel(SupportVectorMachineModel original, Cloner cloner)
94      : base(original, cloner) {
95      // only using a shallow copy here! (gkronber)
96      this.model = original.model;
97      this.rangeTransform = original.rangeTransform;
98      this.targetVariable = original.targetVariable;
99      this.allowedInputVariables = (string[])original.allowedInputVariables.Clone();
100      if (original.classValues != null)
101        this.classValues = (double[])original.classValues.Clone();
102    }
103    public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables, IEnumerable<double> classValues)
104      : this(model, rangeTransform, targetVariable, allowedInputVariables) {
105      this.classValues = classValues.ToArray();
106    }
107    public SupportVectorMachineModel(svm_model model, RangeTransform rangeTransform, string targetVariable, IEnumerable<string> allowedInputVariables)
108      : base() {
109      this.name = ItemName;
110      this.description = ItemDescription;
111      this.model = model;
112      this.rangeTransform = rangeTransform;
113      this.targetVariable = targetVariable;
114      this.allowedInputVariables = allowedInputVariables.ToArray();
115    }
116
117    public override IDeepCloneable Clone(Cloner cloner) {
118      return new SupportVectorMachineModel(this, cloner);
119    }
120
121    #region IRegressionModel Members
122    public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
123      return GetEstimatedValuesHelper(dataset, rows);
124    }
125    public SupportVectorRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
126      return new SupportVectorRegressionSolution(this, new RegressionProblemData(problemData));
127    }
128    IRegressionSolution IRegressionModel.CreateRegressionSolution(IRegressionProblemData problemData) {
129      return CreateRegressionSolution(problemData);
130    }
131    #endregion
132
133    #region IClassificationModel Members
134    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
135      if (classValues == null) throw new NotSupportedException();
136      // return the original class value instead of the predicted value of the model
137      // svm classification only works for integer classes
138      foreach (var estimated in GetEstimatedValuesHelper(dataset, rows)) {
139        // find closest class
140        double bestDist = double.MaxValue;
141        double bestClass = -1;
142        for (int i = 0; i < classValues.Length; i++) {
143          double d = Math.Abs(estimated - classValues[i]);
144          if (d < bestDist) {
145            bestDist = d;
146            bestClass = classValues[i];
147            if (d.IsAlmost(0.0)) break; // exact match no need to look further
148          }
149        }
150        yield return bestClass;
151      }
152    }
153
154    public SupportVectorClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
155      return new SupportVectorClassificationSolution(this, new ClassificationProblemData(problemData));
156    }
157    IClassificationSolution IClassificationModel.CreateClassificationSolution(IClassificationProblemData problemData) {
158      return CreateClassificationSolution(problemData);
159    }
160    #endregion
161    private IEnumerable<double> GetEstimatedValuesHelper(Dataset dataset, IEnumerable<int> rows) {
162      // calculate predictions for the currently requested rows
163      svm_problem problem = SupportVectorMachineUtil.CreateSvmProblem(dataset, targetVariable, allowedInputVariables, rows);
164      svm_problem scaledProblem = rangeTransform.Scale(problem);
165
166      for (int i = 0; i < problem.l; i++) {
167        yield return svm.svm_predict(Model, scaledProblem.x[i]);
168      }
169    }
170
171    #region events
172    public event EventHandler Changed;
173    private void OnChanged(EventArgs e) {
174      var handlers = Changed;
175      if (handlers != null)
176        handlers(this, e);
177    }
178    #endregion
179
180    #region persistence
181    [Storable]
182    private string ModelAsString {
183      get {
184        using (MemoryStream stream = new MemoryStream()) {
185          svm.svm_save_model(new StreamWriter(stream), Model);
186          stream.Seek(0, System.IO.SeekOrigin.Begin);
187          StreamReader reader = new StreamReader(stream);
188          return reader.ReadToEnd();
189        }
190      }
191      set {
192        using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
193          model = svm.svm_load_model(new StreamReader(stream));
194        }
195      }
196    }
197    [Storable]
198    private string RangeTransformAsString {
199      get {
200        using (MemoryStream stream = new MemoryStream()) {
201          RangeTransform.Write(stream, RangeTransform);
202          stream.Seek(0, System.IO.SeekOrigin.Begin);
203          StreamReader reader = new StreamReader(stream);
204          return reader.ReadToEnd();
205        }
206      }
207      set {
208        using (MemoryStream stream = new MemoryStream(Encoding.ASCII.GetBytes(value))) {
209          RangeTransform = RangeTransform.Read(stream);
210        }
211      }
212    }
213    #endregion
214  }
215}
Note: See TracBrowser for help on using the repository browser.