#region License Information /* HeuristicLab * Copyright (C) 2002-2008 Heuristic and Evolutionary Algorithms Laboratory (HEAL) * * This file is part of HeuristicLab. * * HeuristicLab is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * HeuristicLab is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with HeuristicLab. If not, see . */ #endregion using System; using System.Collections.Generic; using System.Text; using System.Xml; using HeuristicLab.Core; using HeuristicLab.Data; using HeuristicLab.DataAnalysis; using System.Linq; namespace HeuristicLab.Modeling { public class VariableImpactCalculator : OperatorBase { public override string Description { get { return @"Calculates the impact of all allowed input variables on the quality of the model using evaluator supplied as suboperator."; } } public VariableImpactCalculator() : base() { AddVariableInfo(new VariableInfo("Dataset", "Dataset", typeof(Dataset), VariableKind.In)); AddVariableInfo(new VariableInfo("TargetVariable", "TargetVariable", typeof(IntData), VariableKind.In)); AddVariableInfo(new VariableInfo("AllowedFeatures", "Indexes of allowed input variables", typeof(ItemList), VariableKind.In)); AddVariableInfo(new VariableInfo("TrainingSamplesStart", "TrainingSamplesStart", typeof(IntData), VariableKind.In)); AddVariableInfo(new VariableInfo("TrainingSamplesEnd", "TrainingSamplesEnd", typeof(IntData), VariableKind.In)); AddVariableInfo(new VariableInfo("VariableImpacts", "Variable impacts", typeof(ItemList), VariableKind.New)); } public override IOperation Apply(IScope scope) { ItemList allowedFeatures = GetVariableValue>("AllowedFeatures", scope, true); int targetVariable = GetVariableValue("TargetVariable", scope, true).Data; Dataset dataset = GetVariableValue("Dataset", scope, true); Dataset dirtyDataset = (Dataset)dataset.Clone(); int start = GetVariableValue("TrainingSamplesStart", scope, true).Data; int end = GetVariableValue("TrainingSamplesEnd", scope, true).Data; if (SubOperators.Count < 1) throw new InvalidOperationException("VariableImpactCalculator needs a suboperator to evaluate the model"); IOperator evaluationOperator = this.SubOperators[0]; ItemList variableImpacts = new ItemList(); // calculateReferenceQuality double referenceQuality = CalculateQuality(scope, dataset, evaluationOperator); for (int i = 0; i < allowedFeatures.Count; i++) { int currentVariable = allowedFeatures[i].Data; var oldValues = ReplaceVariableValues(dirtyDataset, currentVariable , CalculateNewValues(dirtyDataset, currentVariable, start, end), start, end); double newQuality = CalculateQuality(scope, dirtyDataset, evaluationOperator); double ratio = referenceQuality / newQuality; double impact = ratio < 1.0 ? 1.0 - ratio : 1.0 - 1.0 / ratio; ItemList row = new ItemList(); row.Add(new StringData(dataset.GetVariableName(currentVariable))); row.Add(new DoubleData(impact)); variableImpacts.Add(row); ReplaceVariableValues(dirtyDataset, currentVariable, oldValues, start, end); } scope.AddVariable(new Variable(scope.TranslateName("VariableImpacts"), variableImpacts)); return null; } private double CalculateQuality(IScope scope, Dataset dataset, IOperator evaluationOperator) { Scope s = new Scope(); s.AddVariable(new Variable("Dataset", dataset)); scope.AddSubScope(s); evaluationOperator.Execute(s); double quality = s.GetVariableValue("Quality", false).Data; scope.RemoveSubScope(s); return quality; } private IEnumerable ReplaceVariableValues(Dataset ds, int variableIndex, IEnumerable newValues, int start, int end) { double[] oldValues = new double[end - start]; for (int i = 0; i < end - start; i++) oldValues[i] = ds.GetValue(i + start, variableIndex); 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)"); int index = start; foreach(double v in newValues) { ds.SetValue(index++, variableIndex, v); } return oldValues; } private IEnumerable CalculateNewValues(Dataset ds, int variableIndex, int start, int end) { double mean = ds.GetMean(variableIndex, start, end); return Enumerable.Repeat(mean, end - start); } } }