source: branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/ZeroR.cs @ 10568

Last change on this file since 10568 was 10568, checked in by mkommend, 8 years ago

#1998: Code cleanup in ZeroR classification algorithm.

File size: 2.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.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", "0R classification algorithm.")]
34  [Creatable("Data Analysis")]
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() {
53      var solution = CreateZeroRSolution(Problem.ProblemData);
54      Results.Add(new Result("ZeroR solution", "The 0R classifier.", solution));
55    }
56
57    public static IClassificationSolution CreateZeroRSolution(IClassificationProblemData problemData) {
58      Dataset dataset = problemData.Dataset;
59      string target = problemData.TargetVariable;
60      var targetValues = dataset.GetDoubleValues(target, problemData.TrainingIndices);
61
62      var dominantClass = targetValues.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count())
63        .MaxItems(kvp => kvp.Value).Select(x => x.Key).First();
64
65      var model = new ConstantClassificationModel(dominantClass);
66      var solution = new ConstantClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
67      return solution;
68    }
69  }
70}
Note: See TracBrowser for help on using the repository browser.