Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/ZeroR.cs @ 15298

Last change on this file since 15298 was 15061, checked in by gkronber, 7 years ago

#2524: merged r14517, r14523, r14527 from trunk to branch

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