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 |
|
---|
22 | using HeuristicLab.Core;
|
---|
23 | using HeuristicLab.Data;
|
---|
24 | using HeuristicLab.Operators;
|
---|
25 | using HeuristicLab.Parameters;
|
---|
26 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
27 | using SVM;
|
---|
28 |
|
---|
29 | namespace HeuristicLab.Problems.DataAnalysis.SupportVectorMachine {
|
---|
30 | [StorableClass]
|
---|
31 | [Item("SupportVectorMachineModelEvaluator", "Represents a operator that evaluates a support vector machine model on a data set.")]
|
---|
32 | public class SupportVectorMachineModelEvaluator : SingleSuccessorOperator {
|
---|
33 | private const string DataAnalysisProblemDataParameterName = "DataAnalysisProblemData";
|
---|
34 | private const string ModelParameterName = "SupportVectorMachineModel";
|
---|
35 | private const string SamplesStartParameterName = "SamplesStart";
|
---|
36 | private const string SamplesEndParameterName = "SamplesEnd";
|
---|
37 | private const string ValuesParameterName = "Values";
|
---|
38 |
|
---|
39 | #region parameter properties
|
---|
40 | public IValueLookupParameter<DataAnalysisProblemData> DataAnalysisProblemDataParameter {
|
---|
41 | get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[DataAnalysisProblemDataParameterName]; }
|
---|
42 | }
|
---|
43 | public IValueLookupParameter<IntValue> SamplesStartParameter {
|
---|
44 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesStartParameterName]; }
|
---|
45 | }
|
---|
46 | public IValueLookupParameter<IntValue> SamplesEndParameter {
|
---|
47 | get { return (IValueLookupParameter<IntValue>)Parameters[SamplesEndParameterName]; }
|
---|
48 | }
|
---|
49 | public ILookupParameter<SupportVectorMachineModel> SupportVectorMachineModelParameter {
|
---|
50 | get { return (ILookupParameter<SupportVectorMachineModel>)Parameters[ModelParameterName]; }
|
---|
51 | }
|
---|
52 | public ILookupParameter<DoubleMatrix> ValuesParameter {
|
---|
53 | get { return (ILookupParameter<DoubleMatrix>)Parameters[ValuesParameterName]; }
|
---|
54 | }
|
---|
55 | #endregion
|
---|
56 | #region properties
|
---|
57 | public DataAnalysisProblemData DataAnalysisProblemData {
|
---|
58 | get { return DataAnalysisProblemDataParameter.ActualValue; }
|
---|
59 | }
|
---|
60 | public SupportVectorMachineModel SupportVectorMachineModel {
|
---|
61 | get { return SupportVectorMachineModelParameter.ActualValue; }
|
---|
62 | }
|
---|
63 | public IntValue SamplesStart {
|
---|
64 | get { return SamplesStartParameter.ActualValue; }
|
---|
65 | }
|
---|
66 | public IntValue SamplesEnd {
|
---|
67 | get { return SamplesEndParameter.ActualValue; }
|
---|
68 | }
|
---|
69 | #endregion
|
---|
70 | public SupportVectorMachineModelEvaluator()
|
---|
71 | : base() {
|
---|
72 | Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(DataAnalysisProblemDataParameterName, "The data analysis problem data to use for training."));
|
---|
73 | Parameters.Add(new LookupParameter<SupportVectorMachineModel>(ModelParameterName, "The result model generated by the SVM."));
|
---|
74 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesStartParameterName, "The first index of the data set partition on which the SVM model should be evaluated."));
|
---|
75 | Parameters.Add(new ValueLookupParameter<IntValue>(SamplesEndParameterName, "The last index of the data set partition on which the SVM model should be evaluated."));
|
---|
76 | Parameters.Add(new LookupParameter<DoubleMatrix>(ValuesParameterName, "A matrix of original values of the target variable and output values of the SVM model."));
|
---|
77 | }
|
---|
78 |
|
---|
79 | public override IOperation Apply() {
|
---|
80 | int start = SamplesStart.Value;
|
---|
81 | int end = SamplesEnd.Value;
|
---|
82 |
|
---|
83 | ValuesParameter.ActualValue = new DoubleMatrix(Evaluate(SupportVectorMachineModel, DataAnalysisProblemData, start, end));
|
---|
84 | return base.Apply();
|
---|
85 | }
|
---|
86 |
|
---|
87 | public static double[,] Evaluate(SupportVectorMachineModel model, DataAnalysisProblemData problemData, int start, int end) {
|
---|
88 | SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(problemData, start, end);
|
---|
89 | SVM.Problem scaledProblem = model.RangeTransform.Scale(problem);
|
---|
90 |
|
---|
91 | int targetVariableIndex = problemData.Dataset.GetVariableIndex(problemData.TargetVariable.Value);
|
---|
92 |
|
---|
93 | double[,] values = new double[scaledProblem.Count, 2];
|
---|
94 | for (int i = 0; i < scaledProblem.Count; i++) {
|
---|
95 | values[i, 0] = problemData.Dataset[start + i, targetVariableIndex];
|
---|
96 | values[i, 1] = SVM.Prediction.Predict(model.Model, scaledProblem.X[i]);
|
---|
97 | }
|
---|
98 |
|
---|
99 | return values;
|
---|
100 | }
|
---|
101 | }
|
---|
102 | }
|
---|