Free cookie consent management tool by TermsFeed Policy Generator

source: branches/WebJobManager/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/ZeroR.cs @ 14544

Last change on this file since 14544 was 13098, checked in by gkronber, 9 years ago

#1998:

  • introduced new class ConstantModel (to merge ConstantRegressionModel, ConstantClassificationModel and ConstantTimeSeriesModel)
  • fixed copyright statements
  • tried to unify naming
  • added F1 score and Matthew's correlation to classification results collection
File size: 2.7 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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.Linq;
23using HeuristicLab.Common;
24using HeuristicLab.Core;
25using HeuristicLab.Optimization;
26using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
27using HeuristicLab.Problems.DataAnalysis;
28
29namespace HeuristicLab.Algorithms.DataAnalysis {
30  /// <summary>
31  /// 0R classification algorithm.
32  /// </summary>
33  [Item("ZeroR Classification", "The simplest possible classifier, ZeroR always predicts the majority class.")]
34  [StorableClass]
35  public sealed class ZeroR : FixedDataAnalysisAlgorithm<IClassificationProblem> {
36
37    [StorableConstructor]
38    private ZeroR(bool deserializing) : base(deserializing) { }
39    private ZeroR(ZeroR original, Cloner cloner)
40      : base(original, cloner) {
41    }
42    public ZeroR()
43      : base() {
44      Problem = new ClassificationProblem();
45    }
46
47    public override IDeepCloneable Clone(Cloner cloner) {
48      return new ZeroR(this, cloner);
49    }
50
51    protected override void Run() {
52      var solution = CreateZeroRSolution(Problem.ProblemData);
53      Results.Add(new Result("ZeroR solution", "The simplest possible classifier, ZeroR always predicts the majority class.", solution));
54    }
55
56    public static IClassificationSolution CreateZeroRSolution(IClassificationProblemData problemData) {
57      var dataset = problemData.Dataset;
58      string target = problemData.TargetVariable;
59      var targetValues = dataset.GetDoubleValues(target, problemData.TrainingIndices);
60
61
62      // if multiple classes have the same number of observations then simply take the first one
63      var dominantClass = targetValues.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count())
64        .MaxItems(kvp => kvp.Value).Select(x => x.Key).First();
65
66      var model = new ConstantModel(dominantClass);
67      var solution = model.CreateClassificationSolution(problemData);
68      return solution;
69    }
70  }
71}
Note: See TracBrowser for help on using the repository browser.