Free cookie consent management tool by TermsFeed Policy Generator

source: branches/DataAnalysis/HeuristicLab.Problems.DataAnalysis/3.3/SupportVectorMachine/SupportVectorMachineModelEvaluator.cs @ 5275

Last change on this file since 5275 was 5275, checked in by gkronber, 13 years ago

Merged changes from trunk to data analysis exploration branch and added fractional distance metric evaluator. #1142

File size: 5.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2009 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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Operators;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using SVM;
31
32namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine {
33  [StorableClass]
34  [Item("SupportVectorMachineModelEvaluator", "Represents a operator that evaluates a support vector machine model on a data set.")]
35  public class SupportVectorMachineModelEvaluator : SingleSuccessorOperator {
36    private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
37    private const string ModelParameterName = "SupportVectorMachineModel";
38    private const string SamplesStartParameterName = "SamplesStart";
39    private const string SamplesEndParameterName = "SamplesEnd";
40    private const string ValuesParameterName = "Values";
41
42    #region parameter properties
43    public IValueLookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
44      get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
45    }
46    public IValueLookupParameter<IntValue> SamplesStartParameter {
47      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
48    }
49    public IValueLookupParameter<IntValue> SamplesEndParameter {
50      get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
51    }
52    public ILookupParameter<SupportVectorMachineModel> SupportVectorMachineModelParameter {
53      get { return (ILookupParameter<SupportVectorMachineModel>)Parameters[ModelParameterName]; }
54    }
55    public ILookupParameter<DoubleMatrix> ValuesParameter {
56      get { return (ILookupParameter<DoubleMatrix>)Parameters[ValuesParameterName]; }
57    }
58    #endregion
59    #region properties
60    public DataAnalysisProblemData DataAnalysisProblemData {
61      get { return DataAnalysisProblemDataParameter.ActualValue; }
62    }
63    public SupportVectorMachineModel SupportVectorMachineModel {
64      get { return SupportVectorMachineModelParameter.ActualValue; }
65    }
66    public IntValue SamplesStart {
67      get { return SamplesStartParameter.ActualValue; }
68    }
69    public IntValue SamplesEnd {
70      get { return SamplesEndParameter.ActualValue; }
71    }
72    #endregion
73
74    [StorableConstructor]
75    protected SupportVectorMachineModelEvaluator(bool deserializing) : base(deserializing) { }
76    protected SupportVectorMachineModelEvaluator(SupportVectorMachineModelEvaluator original, Cloner cloner) : base(original, cloner) { }
77    public override IDeepCloneable Clone(Cloner cloner) {
78      return new SupportVectorMachineModelEvaluator(this, cloner);
79    }
80    public SupportVectorMachineModelEvaluator()
81      : base() {
82      Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));
83      Parameters.Add(new LookupParameter<SupportVectorMachineModel>(ModelParameterName, "The result model generated by the SVM."));
84      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition on which the SVM model should be evaluated."));
85      Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition on which the SVM model should be evaluated."));
86      Parameters.Add(new LookupParameter<DoubleMatrix>(ValuesParameterName, "A matrix of original values of the target variable and output values of the SVM model."));
87    }
88
89    public override IOperation Apply() {
90      int start = SamplesStart.Value;
91      int end = SamplesEnd.Value;
92      IEnumerable<int> rows =
93        Enumerable.Range(start, end - start)
94        .Where(i => i < DataAnalysisProblemData.TestSamplesStart.Value || DataAnalysisProblemData.TestSamplesEnd.Value <= i);
95
96      ValuesParameter.ActualValue = new DoubleMatrix(Evaluate(SupportVectorMachineModel, DataAnalysisProblemData, rows));
97      return base.Apply();
98    }
99
100    public static double[,] Evaluate(SupportVectorMachineModel model, DataAnalysisProblemData problemData, IEnumerable<int> rowIndices) {
101      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, rowIndices);
102      SVM.Problem scaledProblem = model.RangeTransform.Scale(problem);
103
104      int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value);
105
106      double[,] values = new double[scaledProblem.Count, 2];
107      var rowEnumerator = rowIndices.GetEnumerator();
108      for (int i = 0; i < scaledProblem.Count; i++) {
109        rowEnumerator.MoveNext();
110        values[i, 0] = problemData.Dataset[rowEnumerator.Current, targetVariableIndex];
111        values[i, 1] = SVM.Prediction.Predict(model.Model, scaledProblem.X[i]);
112      }
113
114      return values;
115    }
116  }
117}
Note: See TracBrowser for help on using the repository browser.