#region License Information /* HeuristicLab * Copyright (C) 2002-2012 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.Collections; using System.Collections.Generic; using System.Linq; using HeuristicLab.Common; using HeuristicLab.Core; using HeuristicLab.Persistence.Default.CompositeSerializers.Storable; namespace HeuristicLab.Problems.DataAnalysis { [StorableClass] [Item("AverageThresholdCalculator", "")] public class AverageThresholdCalculator : DiscriminantClassificationWeightCalculator { public AverageThresholdCalculator() : base() { } [StorableConstructor] protected AverageThresholdCalculator(bool deserializing) : base(deserializing) { } protected AverageThresholdCalculator(AverageThresholdCalculator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new AverageThresholdCalculator(this, cloner); } protected double[] threshold; protected double[] classValues; protected override IEnumerable DiscriminantCalculateWeights(IEnumerable discriminantSolutions) { classValues = discriminantSolutions.First().Model.ClassValues.ToArray(); var modelThresholds = discriminantSolutions.Select(x => x.Model.Thresholds.ToArray()); threshold = new double[modelThresholds.First().GetLength(0)]; for (int i = 0; i < modelThresholds.First().GetLength(0); i++) { threshold[i] = modelThresholds.Select(x => x[i]).Average(); } return Enumerable.Repeat(1, discriminantSolutions.Count()); } protected override double GetDiscriminantConfidence(IEnumerable solutions, int index, double estimatedClassValue) { Dataset dataset = solutions.First().ProblemData.Dataset; IList values = solutions.Select(s => s.Model.GetEstimatedValues(dataset, Enumerable.Repeat(index, 1)).First()).ToList(); if (values.Count <= 0) return double.NaN; double avg = values.Average(); return GetAverageConfidence(avg, estimatedClassValue); } public override IEnumerable GetDiscriminantConfidence(IEnumerable solutions, IEnumerable indices, IEnumerable estimatedClassValue) { Dataset dataset = solutions.First().ProblemData.Dataset; double[][] values = solutions.Select(s => s.Model.GetEstimatedValues(dataset, indices).ToArray()).ToArray(); double[] confidences = new double[indices.Count()]; double[] estimatedClassValueArr = estimatedClassValue.ToArray(); for (int i = 0; i < indices.Count(); i++) { double avg = values.Select(x => x[i]).Average(); confidences[i] = GetAverageConfidence(avg, estimatedClassValueArr[i]); } return confidences; } protected double GetAverageConfidence(double avg, double estimatedClassValue) { for (int i = 0; i < classValues.Length; i++) { if (estimatedClassValue.Equals(classValues[i])) { //special case: avgerage is higher than value of highest class if (i == classValues.Length - 1 && avg > estimatedClassValue) { return 1; } //special case: average is lower than value of lowest class if (i == 0 && avg < estimatedClassValue) { return 1; } //special case: average is not between threshold of estimated class value if ((i < classValues.Length - 1 && avg >= threshold[i + 1]) || avg <= threshold[i]) { return 0; } double thresholdToClassDistance, thresholdToAverageValueDistance; if (avg >= classValues[i]) { thresholdToClassDistance = threshold[i + 1] - classValues[i]; thresholdToAverageValueDistance = threshold[i + 1] - avg; } else { thresholdToClassDistance = classValues[i] - threshold[i]; thresholdToAverageValueDistance = avg - threshold[i]; } return (1 / thresholdToClassDistance) * thresholdToAverageValueDistance; } } return double.NaN; } } }