Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/SupportVectorRegression/SupportVectorRegressionSolution.cs @ 3979

Last change on this file since 3979 was 3979, checked in by mkommend, 14 years ago

corrected DataAnalysis.Views.ResultsView to use an internal DoubleMatrix (ticket #1020)

File size: 4.8 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2010 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 HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Data;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
28using System.Collections.Generic;
29using System.Linq;
30using System.Drawing;
31using HeuristicLab.Problems.DataAnalysis.SupportVectorMachine;
32using SVM;
33
34namespace HeuristicLab.Problems.DataAnalysis.Regression.SupportVectorRegression {
35  /// <summary>
36  /// Represents a support vector solution for a regression problem which can be visualized in the GUI.
37  /// </summary>
38  [Item("SupportVectorRegressionSolution", "Represents a support vector solution for a regression problem which can be visualized in the GUI.")]
39  [StorableClass]
40  public sealed class SupportVectorRegressionSolution : DataAnalysisSolution {
41    public SupportVectorRegressionSolution() : base() { }
42    public SupportVectorRegressionSolution(DataAnalysisProblemData problemData, SupportVectorMachineModel model, IEnumerable<string> inputVariables, double lowerEstimationLimit, double upperEstimationLimit)
43      : base(problemData, lowerEstimationLimit, upperEstimationLimit) {
44      this.Model = model;
45    }
46
47    public override Image ItemImage {
48      get { return HeuristicLab.Common.Resources.VS2008ImageLibrary.Function; }
49    }
50
51    public new SupportVectorMachineModel Model {
52      get { return (SupportVectorMachineModel)base.Model; }
53      set { base.Model = value; }
54    }
55
56    public Dataset SupportVectors {
57      get { return CalculateSupportVectors(); }
58    }
59
60    protected override void OnProblemDataChanged() {
61      Model.Model.SupportVectorIndizes = new int[0];
62      base.OnProblemDataChanged();
63    }
64
65    private Dataset CalculateSupportVectors() {
66      if (Model.Model.SupportVectorIndizes.Length == 0)
67        return new Dataset(new List<string>(),new double[0,0]);
68
69      double[,] data = new double[Model.Model.SupportVectorIndizes.Length, ProblemData.Dataset.Columns];
70      for (int i = 0; i < Model.Model.SupportVectorIndizes.Length; i++) {
71        for (int column = 0; column < ProblemData.Dataset.Columns; column++)
72          data[i, column] = ProblemData.Dataset[Model.Model.SupportVectorIndizes[i], column];
73      }
74      return new Dataset(ProblemData.Dataset.VariableNames, data);
75    }
76
77    protected override void RecalculateEstimatedValues() {
78      SVM.Problem problem = SupportVectorMachineUtil.CreateSvmProblem(ProblemData, 0, ProblemData.Dataset.Rows);
79      SVM.Problem scaledProblem = Scaling.Scale(Model.RangeTransform, problem);
80
81      estimatedValues = (from row in Enumerable.Range(0, scaledProblem.Count)
82                         let prediction = SVM.Prediction.Predict(Model.Model, scaledProblem.X[row])
83                         let boundedX = Math.Min(UpperEstimationLimit, Math.Max(LowerEstimationLimit, prediction))
84                         select double.IsNaN(boundedX) ? UpperEstimationLimit : boundedX).ToList();
85      OnEstimatedValuesChanged();
86    }
87
88    private List<double> estimatedValues;
89    public override IEnumerable<double> EstimatedValues {
90      get {
91        if (estimatedValues == null) RecalculateEstimatedValues();
92        return estimatedValues.AsEnumerable();
93      }
94    }
95
96    public override IEnumerable<double> EstimatedTrainingValues {
97      get {
98        if (estimatedValues == null) RecalculateEstimatedValues();
99        int start = ProblemData.TrainingSamplesStart.Value;
100        int n = ProblemData.TrainingSamplesEnd.Value - start;
101        return estimatedValues.Skip(start).Take(n).ToList();
102      }
103    }
104
105    public override IEnumerable<double> EstimatedTestValues {
106      get {
107        if (estimatedValues == null) RecalculateEstimatedValues();
108        int start = ProblemData.TestSamplesStart.Value;
109        int n = ProblemData.TestSamplesEnd.Value - start;
110        return estimatedValues.Skip(start).Take(n).ToList();
111      }
112    }
113  }
114}
Note: See TracBrowser for help on using the repository browser.