Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SingleObjective/SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator.cs @ 16788

Last change on this file since 16788 was 16788, checked in by mkommend, 5 years ago

#2361: Merged all changes from the sensitivity branch to trunk.

File size: 9.7 KB
RevLine 
[12211]1#region License Information
2/* HeuristicLab
[16788]3 * Copyright (C) 2002-2019 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[12211]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;
[12210]23using System.Collections.Generic;
[12302]24using System.Linq;
[16788]25using HEAL.Attic;
[12210]26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Data;
29using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
30using HeuristicLab.Parameters;
[16788]31using HeuristicLab.PluginInfrastructure;
[12210]32
[16788]33namespace HeuristicLab.Problems.DataAnalysis.Symbolic.Classification {
34  [NonDiscoverableType]
[12216]35  [Item("Weighted Performance Measures Evaluator", "Calculates the quality of a symbolic classification solution based on three weighted measures(normalized mean squared error, false negative rate(1-sensitivity) and false positve rate(1-specificity)).")]
[16788]36  [StorableType("0772F316-5E12-4153-857E-8625069B4677")]
[12210]37  public class SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator : SymbolicClassificationSingleObjectiveEvaluator {
38    private const string NormalizedMeanSquaredErrorWeightingFactorParameterName = "NormalizedMeanSquaredErrorWeightingFactor";
[12211]39    private const string FalseNegativeRateWeightingFactorParameterName = "FalseNegativeRateWeightingFactor";
40    private const string FalsePositiveRateWeightingFactorParameterName = "FalsePositiveRateWeightingFactor";
[12210]41    private const string ModelCreatorParameterName = "ModelCreator";
42
43    public override bool Maximization { get { return false; } }
44
45    #region parameter properties
46    public IFixedValueParameter<DoubleValue> NormalizedMeanSquaredErrorWeightingFactorParameter {
47      get { return (IFixedValueParameter<DoubleValue>)Parameters[NormalizedMeanSquaredErrorWeightingFactorParameterName]; }
48    }
[12211]49    public IFixedValueParameter<DoubleValue> FalseNegativeRateWeightingFactorParameter {
50      get { return (IFixedValueParameter<DoubleValue>)Parameters[FalseNegativeRateWeightingFactorParameterName]; }
[12210]51    }
[12211]52    public IFixedValueParameter<DoubleValue> FalsePositiveRateWeightingFactorParameter {
53      get { return (IFixedValueParameter<DoubleValue>)Parameters[FalsePositiveRateWeightingFactorParameterName]; }
[12210]54    }
[12417]55    public IValueLookupParameter<ISymbolicClassificationModelCreator> ModelCreatorParameter {
56      get { return (IValueLookupParameter<ISymbolicClassificationModelCreator>)Parameters[ModelCreatorParameterName]; }
[12210]57    }
58    #endregion
59
[12211]60    public double NormalizedMeanSquaredErrorWeightingFactor {
[16788]61      get { return NormalizedMeanSquaredErrorWeightingFactorParameter.Value.Value; }
[12211]62    }
63    public double FalseNegativeRateWeightingFactor {
[16788]64      get { return FalseNegativeRateWeightingFactorParameter.Value.Value; }
[12211]65    }
66    public double FalsePositiveRateWeightingFactor {
[16788]67      get { return FalsePositiveRateWeightingFactorParameter.Value.Value; }
[12211]68    }
69
[12210]70    [StorableConstructor]
[16788]71    protected SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(StorableConstructorFlag _) : base(_) { }
[12210]72    protected SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator original, Cloner cloner)
73      : base(original, cloner) {
74    }
75    public override IDeepCloneable Clone(Cloner cloner) {
76      return new SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator(this, cloner);
77    }
78
79    public SymbolicClassificationSingleObjectiveWeightedPerformanceMeasuresEvaluator()
80      : base() {
81      Parameters.Add(new FixedValueParameter<DoubleValue>(NormalizedMeanSquaredErrorWeightingFactorParameterName, "The weighting factor of the normalized mean squared error.", new DoubleValue(1)));
[12211]82      Parameters.Add(new FixedValueParameter<DoubleValue>(FalseNegativeRateWeightingFactorParameterName, "The weighting factor of the false negative rate (1-sensitivity).", new DoubleValue(1)));
83      Parameters.Add(new FixedValueParameter<DoubleValue>(FalsePositiveRateWeightingFactorParameterName, "The weighting factor of the false positive rate (1-specificity).", new DoubleValue(1)));
[12417]84      Parameters.Add(new ValueLookupParameter<ISymbolicClassificationModelCreator>(ModelCreatorParameterName, "The model creator which is used during the evaluations."));
[12210]85    }
86
87    public override IOperation InstrumentedApply() {
88      IEnumerable<int> rows = GenerateRowsToEvaluate();
[16788]89      var tree = SymbolicExpressionTreeParameter.ActualValue;
[12210]90      var creator = ModelCreatorParameter.ActualValue;
[16788]91      var interpreter = SymbolicDataAnalysisTreeInterpreterParameter.ActualValue;
92      var estimationLimits = EstimationLimitsParameter.ActualValue;
93      var applyLinearScaling = ApplyLinearScalingParameter.ActualValue.Value;
94
95
96      double quality = Calculate(interpreter, tree, estimationLimits.Lower, estimationLimits.Upper,
97              ProblemDataParameter.ActualValue, rows, applyLinearScaling, creator, NormalizedMeanSquaredErrorWeightingFactor, FalseNegativeRateWeightingFactor, FalsePositiveRateWeightingFactor);
[12210]98      QualityParameter.ActualValue = new DoubleValue(quality);
99      return base.InstrumentedApply();
100    }
101
[16788]102    public static double Calculate(ISymbolicDataAnalysisExpressionTreeInterpreter interpreter, ISymbolicExpressionTree tree, double lowerEstimationLimit, double upperEstimationLimit, IClassificationProblemData problemData,
[12211]103                IEnumerable<int> rows, bool applyLinearScaling, ISymbolicClassificationModelCreator modelCreator, double normalizedMeanSquaredErrorWeightingFactor, double falseNegativeRateWeightingFactor, double falsePositiveRateWeightingFactor) {
[16788]104      var estimatedValues = interpreter.GetSymbolicExpressionTreeValues(tree, problemData.Dataset, rows);
[12311]105      var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
[12302]106      var boundedEstimatedValues = estimatedValues.LimitToRange(lowerEstimationLimit, upperEstimationLimit).ToArray();
[12210]107      OnlineCalculatorError errorState;
108      double nmse;
[12211]109
110      //calculate performance measures
[12210]111      string positiveClassName = problemData.PositiveClass;
[12216]112      double[] classValues, thresholds;
[12487]113      IEnumerable<double> estimatedClassValues = null;
114      ISymbolicDiscriminantFunctionClassificationModel m;
115
[16788]116      var model = modelCreator.CreateSymbolicClassificationModel(problemData.TargetVariable, tree, interpreter, lowerEstimationLimit, upperEstimationLimit);
[12487]117      if ((m = model as ISymbolicDiscriminantFunctionClassificationModel) != null) {
118        m.ThresholdCalculator.Calculate(problemData, boundedEstimatedValues, targetClassValues, out classValues, out thresholds);
119        m.SetThresholdsAndClassValues(thresholds, classValues);
120        estimatedClassValues = m.GetEstimatedClassValues(boundedEstimatedValues);
121      } else {
122        model.RecalculateModelParameters(problemData, rows);
123        estimatedClassValues = model.GetEstimatedClassValues(problemData.Dataset, rows);
124      }
[16788]125
[12210]126      var performanceCalculator = new ClassificationPerformanceMeasuresCalculator(positiveClassName, problemData.GetClassValue(positiveClassName));
[12216]127      performanceCalculator.Calculate(targetClassValues, estimatedClassValues);
[12302]128      if (performanceCalculator.ErrorState != OnlineCalculatorError.None)
[12211]129        return Double.NaN;
130      double falseNegativeRate = 1 - performanceCalculator.TruePositiveRate;
[12210]131      double falsePositiveRate = performanceCalculator.FalsePositiveRate;
132
133      if (applyLinearScaling) {
[12302]134        throw new NotSupportedException("The Weighted Performance Measures Evaluator does not suppport linear scaling!");
[12210]135      }
[12302]136      nmse = OnlineNormalizedMeanSquaredErrorCalculator.Calculate(targetClassValues, boundedEstimatedValues, out errorState);
[12210]137      if (errorState != OnlineCalculatorError.None) return Double.NaN;
[12211]138      return normalizedMeanSquaredErrorWeightingFactor * nmse + falseNegativeRateWeightingFactor * falseNegativeRate + falsePositiveRateWeightingFactor * falsePositiveRate;
[12210]139    }
140
141    public override double Evaluate(IExecutionContext context, ISymbolicExpressionTree tree, IClassificationProblemData problemData, IEnumerable<int> rows) {
142      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = context;
143      EstimationLimitsParameter.ExecutionContext = context;
144      ApplyLinearScalingParameter.ExecutionContext = context;
145      ModelCreatorParameter.ExecutionContext = context;
146
147      double quality = Calculate(SymbolicDataAnalysisTreeInterpreterParameter.ActualValue, tree, EstimationLimitsParameter.ActualValue.Lower, EstimationLimitsParameter.ActualValue.Upper,
[12211]148                                      problemData, rows, ApplyLinearScalingParameter.ActualValue.Value, ModelCreatorParameter.ActualValue, NormalizedMeanSquaredErrorWeightingFactorParameter.Value.Value, FalseNegativeRateWeightingFactor, FalsePositiveRateWeightingFactor);
[12210]149
150      SymbolicDataAnalysisTreeInterpreterParameter.ExecutionContext = null;
151      EstimationLimitsParameter.ExecutionContext = null;
152      ApplyLinearScalingParameter.ExecutionContext = null;
153      ModelCreatorParameter.ExecutionContext = null;
154
155      return quality;
156    }
157  }
158}
Note: See TracBrowser for help on using the repository browser.