[9074] | 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 |
|
---|
| 22 | using System.Collections.Generic;
|
---|
| 23 | using System.Linq;
|
---|
| 24 | using HeuristicLab.Common;
|
---|
| 25 | using HeuristicLab.Core;
|
---|
| 26 | using HeuristicLab.Encodings.SymbolicExpressionTreeEncoding;
|
---|
| 27 | using HeuristicLab.Optimization;
|
---|
| 28 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 29 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 30 | using HeuristicLab.Problems.DataAnalysis.Symbolic;
|
---|
| 31 | using HeuristicLab.Problems.DataAnalysis.Symbolic.Classification;
|
---|
| 32 |
|
---|
| 33 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
| 34 | /// <summary>
|
---|
| 35 | /// 0R classification algorithm.
|
---|
| 36 | /// </summary>
|
---|
| 37 | [Item("ZeroR", "0R classification algorithm.")]
|
---|
| 38 | [Creatable("Data Analysis")]
|
---|
| 39 | [StorableClass]
|
---|
| 40 | public sealed class ZeroR : FixedDataAnalysisAlgorithm<IClassificationProblem> {
|
---|
| 41 |
|
---|
| 42 | [StorableConstructor]
|
---|
| 43 | private ZeroR(bool deserializing) : base(deserializing) { }
|
---|
| 44 | private ZeroR(ZeroR original, Cloner cloner)
|
---|
| 45 | : base(original, cloner) {
|
---|
| 46 | }
|
---|
| 47 | public ZeroR()
|
---|
| 48 | : base() {
|
---|
| 49 | Problem = new ClassificationProblem();
|
---|
| 50 | }
|
---|
| 51 |
|
---|
| 52 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 53 | return new ZeroR(this, cloner);
|
---|
| 54 | }
|
---|
| 55 |
|
---|
| 56 | protected override void Run() {
|
---|
| 57 | var solution = CreateZeroRSolution(Problem.ProblemData);
|
---|
| 58 | Results.Add(new Result("ZeroR solution", "The 0R classifier.", solution));
|
---|
| 59 | }
|
---|
| 60 |
|
---|
| 61 | public static IClassificationSolution CreateZeroRSolution(IClassificationProblemData problemData) {
|
---|
| 62 | Dataset dataset = problemData.Dataset;
|
---|
| 63 | string target = problemData.TargetVariable;
|
---|
| 64 | var classValuesEnumerator = problemData.ClassValues.GetEnumerator();
|
---|
| 65 | var classValuesInDatasetEnumerator = dataset.GetDoubleValues(target, problemData.TrainingIndices).GetEnumerator();
|
---|
| 66 |
|
---|
| 67 | Dictionary<double, int> classValuesCount = new Dictionary<double, int>(problemData.ClassValues.Count());
|
---|
| 68 |
|
---|
| 69 | //initialize
|
---|
| 70 | while (classValuesEnumerator.MoveNext()) {
|
---|
| 71 | classValuesCount[classValuesEnumerator.Current] = 0;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
| 74 | //count occurence of classes
|
---|
| 75 | while (classValuesInDatasetEnumerator.MoveNext()) {
|
---|
| 76 | classValuesCount[classValuesInDatasetEnumerator.Current] += 1;
|
---|
| 77 | }
|
---|
| 78 |
|
---|
| 79 | classValuesEnumerator.Reset();
|
---|
| 80 | double mostOccurences = -1;
|
---|
| 81 | double bestClass = double.NaN;
|
---|
| 82 | while (classValuesEnumerator.MoveNext()) {
|
---|
| 83 | if (classValuesCount[classValuesEnumerator.Current] > mostOccurences) {
|
---|
| 84 | mostOccurences = classValuesCount[classValuesEnumerator.Current];
|
---|
| 85 | bestClass = classValuesEnumerator.Current;
|
---|
| 86 | }
|
---|
| 87 | }
|
---|
| 88 |
|
---|
| 89 | ConstantClassificationModel model = new ConstantClassificationModel(bestClass);
|
---|
| 90 | ConstantClassificationSolution solution = new ConstantClassificationSolution(model, (IClassificationProblemData)problemData.Clone());
|
---|
| 91 |
|
---|
| 92 | return solution;
|
---|
| 93 | }
|
---|
| 94 |
|
---|
| 95 | private static SymbolicDiscriminantFunctionClassificationModel CreateDiscriminantFunctionModel(ISymbolicExpressionTree tree,
|
---|
| 96 | ISymbolicDataAnalysisExpressionTreeInterpreter interpreter,
|
---|
| 97 | IClassificationProblemData problemData,
|
---|
| 98 | IEnumerable<int> rows,
|
---|
| 99 | IEnumerable<double> classValues) {
|
---|
| 100 | var model = new SymbolicDiscriminantFunctionClassificationModel(tree, interpreter, new AccuracyMaximizationThresholdCalculator());
|
---|
| 101 | IList<double> thresholds = new List<double>();
|
---|
| 102 | double last = 0;
|
---|
| 103 | foreach (double item in classValues) {
|
---|
| 104 | if (thresholds.Count == 0) {
|
---|
| 105 | thresholds.Add(double.NegativeInfinity);
|
---|
| 106 | } else {
|
---|
| 107 | thresholds.Add((last + item) / 2);
|
---|
| 108 | }
|
---|
| 109 | last = item;
|
---|
| 110 | }
|
---|
| 111 | model.SetThresholdsAndClassValues(thresholds, classValues);
|
---|
| 112 | return model;
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 | }
|
---|