Free cookie consent management tool by TermsFeed Policy Generator

source: branches/ClassificationModelComparison/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/OneR/OneRClassificationModel.cs @ 9074

Last change on this file since 9074 was 9074, checked in by sforsten, 11 years ago

#1998:

  • added ZeroR and OneR classifiers
  • added ConstantClassificationModel/-Solution and OneRClassificationModel/-Solution
File size: 3.5 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;
23using System.Collections.Generic;
24using System.Linq;
25using HeuristicLab.Common;
26using HeuristicLab.Core;
27using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
31  public class OneRClassificationModel : NamedItem, IClassificationModel {
32    [Storable]
33    protected string variable;
34    public string Variable {
35      get { return variable; }
36    }
37
38    [Storable]
39    protected double[] splits;
40    public double[] Splits {
41      get { return splits; }
42    }
43
44    [Storable]
45    protected double[] classes;
46    public double[] Classes {
47      get { return classes; }
48    }
49
50    [StorableConstructor]
51    protected OneRClassificationModel(bool deserializing) : base(deserializing) { }
52    protected OneRClassificationModel(OneRClassificationModel original, Cloner cloner)
53      : base(original, cloner) {
54      this.splits = original.splits;
55    }
56    public override IDeepCloneable Clone(Cloner cloner) { return new OneRClassificationModel(this, cloner); }
57
58    public OneRClassificationModel(string variable, double[] splits, double[] classes)
59      : base() {
60      if (!Double.IsPositiveInfinity(splits[splits.Length - 1])) {
61        throw new ArgumentException("Last split has to be double.PositiveInfinity, so that all values are covered.");
62      }
63      this.name = ItemName;
64      this.description = ItemDescription;
65      this.variable = variable;
66      this.splits = splits;
67      this.classes = classes;
68    }
69
70    // uses sorting to return the values in the order of rows, instead of using nested for loops
71    // to avoid O(n²) runtime
72    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
73      var values = dataset.GetDoubleValues(Variable, rows).ToArray();
74      var rowsArray = rows.ToArray();
75      var order = Enumerable.Range(0, rowsArray.Length).ToArray();
76      double[] estimated = new double[rowsArray.Length];
77      Array.Sort(rowsArray, order);
78      Array.Sort(values, rowsArray);
79      int curSplit = 0, curIndex = 0;
80      while (curSplit < Splits.Length) {
81        while (curIndex < values.Length && Splits[curSplit] > values[curIndex]) {
82          estimated[curIndex] = classes[curSplit];
83          curIndex++;
84        }
85        curSplit++;
86      }
87      Array.Sort(rowsArray, estimated);
88      Array.Sort(order, estimated);
89      return estimated;
90    }
91
92    public IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
93      return new OneRClassificationSolution(this, new ClassificationProblemData(problemData));
94    }
95
96  }
97}
Note: See TracBrowser for help on using the repository browser.