Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Modeling/3.2/VariableImpactCalculator.cs @ 2034

Last change on this file since 2034 was 2034, checked in by gkronber, 15 years ago

Implemented a first version of an operator to calculate variable impacts of models (generated by GP or SVM). #644 (Variable impact of CEDMA models should be calculated and stored in the result DB)

File size: 5.1 KB
Line 
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
22using System;
23using System.Collections.Generic;
24using System.Text;
25using System.Xml;
26using HeuristicLab.Core;
27using HeuristicLab.Data;
28using HeuristicLab.DataAnalysis;
29using System.Linq;
30
31namespace HeuristicLab.Modeling {
32  public class VariableImpactCalculator : OperatorBase {
33    public override string Description {
34      get { return @"Calculates the impact of all allowed input variables on the quality of the model using evaluator supplied as suboperator."; }
35    }
36
37    public VariableImpactCalculator()
38      : base() {
39      AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In));
40      AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.In));
41      AddVariableInfo(new VariableInfo("AllowedFeatures", "Indexes of allowed input variables", typeof(ItemList<IntData>), VariableKind.In));
42      AddVariableInfo(new VariableInfo("TrainingSamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.In));
43      AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.In));
44      AddVariableInfo(new VariableInfo("VariableImpacts", "Variable impacts", typeof(ItemList), VariableKind.New));
45    }
46
47    public override IOperation Apply(IScope scope) {
48      ItemList<IntData> allowedFeatures = GetVariableValue<ItemList<IntData>>("AllowedFeatures", scope, true);
49      int targetVariable = GetVariableValue<IntData>("TargetVariable", scope, true).Data;
50      Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
51      Dataset dirtyDataset = (Dataset)dataset.Clone();
52      int start = GetVariableValue<IntData>("TrainingSamplesStart", scope, true).Data;
53      int end = GetVariableValue<IntData>("TrainingSamplesEnd", scope, true).Data;
54
55      if (SubOperators.Count < 1) throw new InvalidOperationException("VariableImpactCalculator needs a suboperator to evaluate the model");
56      IOperator evaluationOperator = this.SubOperators[0];
57
58      ItemList variableImpacts = new ItemList();
59
60      // calculateReferenceQuality
61      double referenceQuality = CalculateQuality(scope, dataset, evaluationOperator);
62
63      for (int i = 0; i < allowedFeatures.Count; i++) {
64        int currentVariable = allowedFeatures[i].Data;
65        var oldValues = ReplaceVariableValues(dirtyDataset, currentVariable , CalculateNewValues(dirtyDataset, currentVariable, start, end), start, end);
66        double newQuality = CalculateQuality(scope, dirtyDataset, evaluationOperator);
67        double ratio = referenceQuality / newQuality;
68        double impact = ratio < 1.0 ? 1.0 - ratio : 1.0 - 1.0 / ratio;
69        ItemList row = new ItemList();
70        row.Add(new StringData(dataset.GetVariableName(currentVariable)));
71        row.Add(new DoubleData(impact));
72        variableImpacts.Add(row);
73        ReplaceVariableValues(dirtyDataset, currentVariable, oldValues, start, end);
74      }
75      scope.AddVariable(new Variable(scope.TranslateName("VariableImpacts"), variableImpacts));
76      return null;
77    }
78
79    private double CalculateQuality(IScope scope, Dataset dataset, IOperator evaluationOperator) {
80      Scope s = new Scope();
81      s.AddVariable(new Variable("Dataset", dataset));
82      scope.AddSubScope(s);
83      evaluationOperator.Execute(s);
84      double quality = s.GetVariableValue<DoubleData>("Quality", false).Data;
85      scope.RemoveSubScope(s);
86      return quality;
87    }
88
89    private IEnumerable<double> ReplaceVariableValues(Dataset ds, int variableIndex, IEnumerable<double> newValues, int start, int end) {
90      double[] oldValues = new double[end - start];
91      for (int i = 0; i < end - start; i++) oldValues[i] = ds.GetValue(i + start, variableIndex);
92      if (newValues.Count() != end - start) throw new ArgumentException("The length of the new values sequence doesn't match the required length (number of replaced values)");
93
94      int index = start;
95      foreach(double v in newValues) {
96        ds.SetValue(index++, variableIndex, v);
97      }
98      return oldValues;
99    }
100
101    private IEnumerable<double> CalculateNewValues(Dataset ds, int variableIndex, int start, int end) {
102      double mean = ds.GetMean(variableIndex, start, end);
103      return Enumerable.Repeat(mean, end - start);
104    }
105  }
106}
Note: See TracBrowser for help on using the repository browser.