#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("MedianThresholdCalculator", "")] public class MedianThresholdCalculator : DiscriminantClassificationWeightCalculator { public MedianThresholdCalculator() : base() { } [StorableConstructor] protected MedianThresholdCalculator(bool deserializing) : base(deserializing) { } protected MedianThresholdCalculator(MedianThresholdCalculator original, Cloner cloner) : base(original, cloner) { } public override IDeepCloneable Clone(Cloner cloner) { return new MedianThresholdCalculator(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] = GetMedian(modelThresholds.Select(x => x[i]).ToList()); } return Enumerable.Repeat(1, discriminantSolutions.Count()); } protected override double GetDiscriminantConfidence(IEnumerable solutions, int index, double estimatedClassValue) { // only works with binary classification if (!classValues.Count().Equals(2)) return double.NaN; 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 median = GetMedian(values); return GetMedianConfidence(median, estimatedClassValue); } public override IEnumerable GetDiscriminantConfidence(IEnumerable solutions, IEnumerable indices, IEnumerable estimatedClassValue) { if (!classValues.Count().Equals(2)) return Enumerable.Repeat(double.NaN, indices.Count()); 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] = GetMedianConfidence(avg, estimatedClassValueArr[i]); } return confidences; } protected double GetMedianConfidence(double median, double estimatedClassValue) { if (estimatedClassValue.Equals(classValues[0])) { if (median < estimatedClassValue) return 1; else if (median >= threshold[1]) return 0; else { double distance = threshold[1] - classValues[0]; return (1 / distance) * (threshold[1] - median); } } else if (estimatedClassValue.Equals(classValues[1])) { if (median > estimatedClassValue) return 1; else if (median <= threshold[1]) return 0; else { double distance = classValues[1] - threshold[1]; return (1 / distance) * (median - threshold[1]); } } else return double.NaN; } protected double GetMedian(IList estimatedValues) { int count = estimatedValues.Count; if (count % 2 == 0) return 0.5 * (estimatedValues[count / 2 - 1] + estimatedValues[count / 2]); else return estimatedValues[count / 2]; } } }