Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/OneRTest.cs @ 10569

Last change on this file since 10569 was 10569, checked in by mkommend, 9 years ago

#1998: Reimplemented OneR classification algorithm.

File size: 6.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2012 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
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.Collections.Generic;
23using System.Linq;
24using HeuristicLab.Common;
25using HeuristicLab.Core;
26using HeuristicLab.Data;
27using HeuristicLab.Optimization;
28using HeuristicLab.Parameters;
29using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
30using HeuristicLab.Problems.DataAnalysis;
31
32namespace HeuristicLab.Algorithms.DataAnalysis {
33  /// <summary>
34  /// 1R classification algorithm.
35  /// </summary>
36  [Item("OneR Classification", "1R classification algorithm.")]
37  [StorableClass]
38  public sealed class OneRTest : FixedDataAnalysisAlgorithm<IClassificationProblem> {
39
40    public IValueParameter<IntValue> MinBucketSizeParameter {
41      get { return (IValueParameter<IntValue>)Parameters["MinBucketSize"]; }
42    }
43
44    [StorableConstructor]
45    private OneRTest(bool deserializing) : base(deserializing) { }
46
47    private OneRTest(OneRTest original, Cloner cloner)
48      : base(original, cloner) { }
49
50    public OneRTest()
51      : base() {
52      Parameters.Add(new ValueParameter<IntValue>("MinBucketSize", "Minimum size of a bucket for numerical values. (Except for the rightmost bucket)", new IntValue(6)));
53      Problem = new ClassificationProblem();
54    }
55
56    public override IDeepCloneable Clone(Cloner cloner) {
57      return new OneRTest(this, cloner);
58    }
59
60    protected override void Run() {
61      var solution = CreateOneRSolution(Problem.ProblemData, MinBucketSizeParameter.Value.Value);
62      Results.Add(new Result("OneR solution", "The 1R classifier.", solution));
63    }
64
65    public static IClassificationSolution CreateOneRSolution(IClassificationProblemData problemData, int minBucketSize) {
66      var bestClassified = 0;
67      List<Split> bestSplits = null;
68      string bestVariable = string.Empty;
69
70      var classValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, problemData.TrainingIndices);
71      foreach (var variable in problemData.AllowedInputVariables) {
72        var inputValues = problemData.Dataset.GetDoubleValues(variable, problemData.TrainingIndices);
73        var samples = inputValues.Zip(classValues, (i, v) => new Sample(i, v)).OrderBy(s => s.inputValue);
74
75        //calculate class distributions for all distinct inputValues
76        List<Dictionary<double, int>> classDistributions = new List<Dictionary<double, int>>();
77        List<double> thresholds = new List<double>();
78        double lastValue = double.NaN;
79        foreach (var sample in samples) {
80          if (sample.inputValue > lastValue || double.IsNaN(lastValue)) {
81            if (!double.IsNaN(lastValue)) thresholds.Add((lastValue + sample.inputValue) / 2);
82            lastValue = sample.inputValue;
83            classDistributions.Add(new Dictionary<double, int>());
84            foreach (var classValue in problemData.ClassValues)
85              classDistributions[classDistributions.Count - 1][classValue] = 0;
86
87          }
88          classDistributions[classDistributions.Count - 1][sample.classValue]++;
89        }
90        thresholds.Add(double.PositiveInfinity);
91
92        var distribution = classDistributions[0];
93        var threshold = thresholds[0];
94        var splits = new List<Split>();
95
96        for (int i = 1; i < classDistributions.Count; i++) {
97          var samplesInSplit = distribution.Max(d => d.Value);
98          //join splits if too few sample in split or the distributions has the same maximum class value
99          if (samplesInSplit < minBucketSize ||
100            classDistributions[i].MaxItems(d => d.Value).Select(d => d.Key).Contains(
101              distribution.MaxItems(d => d.Value).Select(d => d.Key).First())) {
102            foreach (var classValue in classDistributions[i])
103              distribution[classValue.Key] += classValue.Value;
104            threshold = thresholds[i];
105          } else {
106            splits.Add(new Split(threshold, distribution.MaxItems(d => d.Value).Select(d => d.Key).First()));
107            distribution = classDistributions[i];
108            threshold = thresholds[i];
109          }
110        }
111        splits.Add(new Split(double.PositiveInfinity, distribution.MaxItems(d => d.Value).Select(d => d.Key).First()));
112
113        int correctClassified = 0;
114        int splitIndex = 0;
115        foreach (var sample in samples) {
116          while (sample.inputValue >= splits[splitIndex].thresholdValue)
117            splitIndex++;
118          correctClassified += sample.classValue == splits[splitIndex].classValue ? 1 : 0;
119        }
120
121        if (correctClassified > bestClassified) {
122          bestClassified = correctClassified;
123          bestSplits = splits;
124          bestVariable = variable;
125        }
126      }
127
128      //remove neighboring splits with the same class value
129      for (int i = 0; i < bestSplits.Count - 1; i++) {
130        if (bestSplits[i].classValue == bestSplits[i + 1].classValue) {
131          bestSplits.Remove(bestSplits[i]);
132          i--;
133        }
134      }
135
136      var model = new OneRClassificationModel(bestVariable, bestSplits.Select(s => s.thresholdValue).ToArray(), bestSplits.Select(s => s.classValue).ToArray(), 0);
137      var solution = new OneRClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
138
139      return solution;
140    }
141
142    #region helper classes
143    private class Split {
144      public double thresholdValue;
145      public double classValue;
146
147      public Split(double thresholdValue, double classValue) {
148        this.thresholdValue = thresholdValue;
149        this.classValue = classValue;
150      }
151    }
152
153    private class Sample {
154      public double inputValue;
155      public double classValue;
156
157      public Sample(double inputValue, double classValue) {
158        this.inputValue = inputValue;
159        this.classValue = classValue;
160      }
161    }
162    #endregion
163  }
164}
Note: See TracBrowser for help on using the repository browser.