1  #region License Information


2  /* HeuristicLab


3  * Copyright (C) 20022012 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 


22  using System.Linq;


23  using HeuristicLab.Common;


24  using HeuristicLab.Core;


25  using HeuristicLab.Optimization;


26  using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;


27  using HeuristicLab.Problems.DataAnalysis;


28 


29  namespace HeuristicLab.Algorithms.DataAnalysis {


30  /// <summary>


31  /// 0R classification algorithm.


32  /// </summary>


33  [Item("ZeroR", "The simplest possible classifier, ZeroR always predicts the majority class.")]


34  [Creatable("Data Analysis")] // TODO


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 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 ConstantRegressionModel(dominantClass);


68  var solution = new ConstantClassificationSolution(model, (IClassificationProblemData)problemData.Clone());


69  return solution;


70  }


71  }


72  }

