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 |
|
---|
22 | using System;
|
---|
23 | using System.Collections.Generic;
|
---|
24 | using System.Text;
|
---|
25 | using System.Xml;
|
---|
26 | using HeuristicLab.Core;
|
---|
27 | using HeuristicLab.Data;
|
---|
28 | using HeuristicLab.DataAnalysis;
|
---|
29 | using System.Linq;
|
---|
30 |
|
---|
31 | namespace HeuristicLab.Modeling {
|
---|
32 | public class VariableQualityImpactCalculator : OperatorBase {
|
---|
33 |
|
---|
34 | public VariableQualityImpactCalculator()
|
---|
35 | : base() {
|
---|
36 | AddVariableInfo(new VariableInfo("Predictor", "The predictor used to evaluate the model", typeof(IPredictor), VariableKind.In));
|
---|
37 | AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In));
|
---|
38 | AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(StringData), VariableKind.In));
|
---|
39 | AddVariableInfo(new VariableInfo("InputVariableNames", "Names of used variables in the model (optional)", typeof(ItemList<StringData>), VariableKind.In));
|
---|
40 | AddVariableInfo(new VariableInfo("SamplesStart", "SamplesStart", typeof(IntData), VariableKind.In));
|
---|
41 | AddVariableInfo(new VariableInfo("SamplesEnd", "SamplesEnd", typeof(IntData), VariableKind.In));
|
---|
42 | AddVariableInfo(new VariableInfo(ModelingResult.VariableQualityImpact.ToString(), "VariableQualityImpacts", typeof(ItemList), VariableKind.New));
|
---|
43 | }
|
---|
44 |
|
---|
45 | public override string Description {
|
---|
46 | get { return @"Calculates the impact of all allowed input variables on the quality of the model using evaluator supplied as suboperator."; }
|
---|
47 | }
|
---|
48 |
|
---|
49 | public override IOperation Apply(IScope scope) {
|
---|
50 | IPredictor predictor = GetVariableValue<IPredictor>("Predictor", scope, true);
|
---|
51 | Dataset dataset = GetVariableValue<Dataset>("Dataset", scope, true);
|
---|
52 | string targetVariableName = GetVariableValue<StringData>("TargetVariable", scope, true).Data;
|
---|
53 | int targetVariable = dataset.GetVariableIndex(targetVariableName);
|
---|
54 | ItemList<StringData> inputVariableNames = GetVariableValue<ItemList<StringData>>("InputVariableNames", scope, true, false);
|
---|
55 | int start = GetVariableValue<IntData>("SamplesStart", scope, true).Data;
|
---|
56 | int end = GetVariableValue<IntData>("SamplesEnd", scope, true).Data;
|
---|
57 |
|
---|
58 | Dictionary<string, double> qualityImpacts;
|
---|
59 | if (inputVariableNames == null)
|
---|
60 | qualityImpacts = Calculate(dataset, predictor, targetVariableName, start, end);
|
---|
61 | else
|
---|
62 | qualityImpacts = Calculate(dataset, predictor, targetVariableName, inputVariableNames.Select(iv => iv.Data), start, end);
|
---|
63 |
|
---|
64 | ItemList variableImpacts = new ItemList();
|
---|
65 | foreach (KeyValuePair<string, double> p in qualityImpacts) {
|
---|
66 | if (p.Key != targetVariableName) {
|
---|
67 | ItemList row = new ItemList();
|
---|
68 | row.Add(new StringData(p.Key));
|
---|
69 | row.Add(new DoubleData(p.Value));
|
---|
70 | variableImpacts.Add(row);
|
---|
71 | }
|
---|
72 | }
|
---|
73 |
|
---|
74 | scope.AddVariable(new Variable(scope.TranslateName(ModelingResult.VariableQualityImpact.ToString()), variableImpacts));
|
---|
75 | return null;
|
---|
76 | }
|
---|
77 |
|
---|
78 | public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, int start, int end) {
|
---|
79 | return Calculate(dataset, predictor, targetVariableName, null, start, end);
|
---|
80 | }
|
---|
81 |
|
---|
82 | public static Dictionary<string, double> Calculate(Dataset dataset, IPredictor predictor, string targetVariableName, IEnumerable<string> inputVariableNames, int start, int end) {
|
---|
83 | Dictionary<string, double> evaluationImpacts = new Dictionary<string, double>();
|
---|
84 | Dataset dirtyDataset = (Dataset)dataset.Clone();
|
---|
85 |
|
---|
86 | double[] predictedValues = predictor.Predict(dataset, start, end);
|
---|
87 | double[] targetValues = dataset.GetVariableValues(targetVariableName, start, end);
|
---|
88 |
|
---|
89 | double oldMSE = CalculateMSE(targetValues, predictedValues);
|
---|
90 | double newMSE;
|
---|
91 |
|
---|
92 | double mean;
|
---|
93 | IEnumerable<double> oldValues;
|
---|
94 | IEnumerable<string> variables;
|
---|
95 | if (inputVariableNames != null)
|
---|
96 | variables = inputVariableNames;
|
---|
97 | else
|
---|
98 | variables = dataset.VariableNames;
|
---|
99 |
|
---|
100 | foreach (string variableName in variables) {
|
---|
101 | if (dataset.CountMissingValues(variableName, start, end) < (end - start) &&
|
---|
102 | dataset.GetRange(variableName, start, end) > 0.0 &&
|
---|
103 | variableName != targetVariableName) {
|
---|
104 | mean = dataset.GetMean(variableName, start, end);
|
---|
105 | oldValues = dirtyDataset.ReplaceVariableValues(variableName, Enumerable.Repeat(mean, end - start), start, end);
|
---|
106 | predictedValues = predictor.Predict(dirtyDataset, start, end);
|
---|
107 | newMSE = CalculateMSE(predictedValues, targetValues);
|
---|
108 | evaluationImpacts[variableName] = newMSE / oldMSE;
|
---|
109 | dirtyDataset.ReplaceVariableValues(variableName, oldValues, start, end);
|
---|
110 | } else {
|
---|
111 | evaluationImpacts[variableName] = 1.0;
|
---|
112 | }
|
---|
113 | }
|
---|
114 |
|
---|
115 | return evaluationImpacts;
|
---|
116 | }
|
---|
117 |
|
---|
118 | private static double CalculateImpact(double referenceValue, double newValue) {
|
---|
119 | return newValue / referenceValue;
|
---|
120 | }
|
---|
121 |
|
---|
122 | private static double CalculateMSE(double[] referenceValues, double[] newValues) {
|
---|
123 | try {
|
---|
124 | return SimpleMSEEvaluator.Calculate(Matrix<double>.Create(referenceValues, newValues));
|
---|
125 | }
|
---|
126 | catch (ArgumentException) {
|
---|
127 | return double.PositiveInfinity;
|
---|
128 | }
|
---|
129 | }
|
---|
130 | }
|
---|
131 | }
|
---|