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