#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; /// /// /// /// /// median instead of weights, because it doesn't use any weights protected override IEnumerable DiscriminantCalculateWeights(IEnumerable discriminantSolutions) { List> estimatedValues = new List>(); List> estimatedClassValues = new List>(); List solutionProblemData = discriminantSolutions.Select(sol => sol.ProblemData).ToList(); Dataset dataSet = solutionProblemData[0].Dataset; IEnumerable rows = Enumerable.Range(0, dataSet.Rows); foreach (var solution in discriminantSolutions) { estimatedValues.Add(solution.Model.GetEstimatedValues(dataSet, rows).ToList()); estimatedClassValues.Add(solution.Model.GetEstimatedValues(dataSet, rows).ToList()); } List median = new List(); List targetValues = dataSet.GetDoubleValues(solutionProblemData[0].TargetVariable).ToList(); IList curTrainingpoints = new List(); int removed = 0; int count = targetValues.Count; for (int point = 0; point < count; point++) { curTrainingpoints.Clear(); for (int solutionPos = 0; solutionPos < solutionProblemData.Count; solutionPos++) { if (PointInTraining(solutionProblemData[solutionPos], point)) { curTrainingpoints.Add(estimatedValues[solutionPos][point]); } } if (curTrainingpoints.Count > 0) median.Add(GetMedian(curTrainingpoints.OrderBy(p => p).ToList())); else { //remove not used points targetValues.RemoveAt(point - removed); removed++; } } AccuracyMaximizationThresholdCalculator.CalculateThresholds(solutionProblemData[0], median, targetValues, out classValues, out threshold); return Enumerable.Repeat(1, discriminantSolutions.Count()); } protected override double DiscriminantAggregateEstimatedClassValues(IDictionary estimatedClassValues, IDictionary estimatedValues) { IList values = estimatedValues.Select(x => x.Value).ToList(); if (values.Count <= 0) return double.NaN; double median = GetMedian(values); return GetClassValueToMedian(median); } private double GetClassValueToMedian(double median) { double classValue = classValues.First(); for (int i = 0; i < classValues.Count(); i++) { if (median > threshold[i]) classValue = classValues[i]; else break; } return classValue; } 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); 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) * (median - classValues[0]); } } 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) * (classValues[1] - median); } } else return double.NaN; } private 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]; } } }