[9074] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[15584] | 3 | * Copyright (C) 2002-2018 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[9074] | 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 |
|
---|
| 22 | using System.Linq;
|
---|
[15061] | 23 | using System.Threading;
|
---|
[9074] | 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Optimization;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 31 | /// <summary>
|
---|
| 32 | /// 0R classification algorithm.
|
---|
| 33 | /// </summary>
|
---|
[13091] | 34 | [Item("ZeroR Classification", "The simplest possible classifier, ZeroR always predicts the majority class.")]
|
---|
[9074] | 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 |
|
---|
[15061] | 52 | protected override void Run(CancellationToken cancellationToken) {
|
---|
[9074] | 53 | var solution = CreateZeroRSolution(Problem.ProblemData);
|
---|
[13089] | 54 | Results.Add(new Result("ZeroR solution", "The simplest possible classifier, ZeroR always predicts the majority class.", solution));
|
---|
[9074] | 55 | }
|
---|
| 56 |
|
---|
| 57 | public static IClassificationSolution CreateZeroRSolution(IClassificationProblemData problemData) {
|
---|
[13086] | 58 | var dataset = problemData.Dataset;
|
---|
[9074] | 59 | string target = problemData.TargetVariable;
|
---|
[10568] | 60 | var targetValues = dataset.GetDoubleValues(target, problemData.TrainingIndices);
|
---|
[9074] | 61 |
|
---|
[13089] | 62 |
|
---|
| 63 | // if multiple classes have the same number of observations then simply take the first one
|
---|
[10568] | 64 | var dominantClass = targetValues.GroupBy(x => x).ToDictionary(g => g.Key, g => g.Count())
|
---|
| 65 | .MaxItems(kvp => kvp.Value).Select(x => x.Key).First();
|
---|
[9074] | 66 |
|
---|
[14027] | 67 | var model = new ConstantModel(dominantClass, target);
|
---|
[13098] | 68 | var solution = model.CreateClassificationSolution(problemData);
|
---|
[9074] | 69 | return solution;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|