Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneRClassificationModel.cs @ 13783

Last change on this file since 13783 was 13098, checked in by gkronber, 9 years ago

#1998:

  • introduced new class ConstantModel (to merge ConstantRegressionModel, ConstantClassificationModel and ConstantTimeSeriesModel)
  • fixed copyright statements
  • tried to unify naming
  • added F1 score and Matthew's correlation to classification results collection
File size: 4.3 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2015 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  [StorableClass]
32  [Item("OneR Classification Model", "A model that uses intervals for one variable to determine the class.")]
33  public class OneRClassificationModel : NamedItem, IClassificationModel {
34    [Storable]
35    protected string variable;
36    public string Variable {
37      get { return variable; }
38    }
39
40    [Storable]
41    protected double[] splits;
42    public double[] Splits {
43      get { return splits; }
44    }
45
46    [Storable]
47    protected double[] classes;
48    public double[] Classes {
49      get { return classes; }
50    }
51
52    [Storable]
53    protected double missingValuesClass;
54    public double MissingValuesClass {
55      get { return missingValuesClass; }
56    }
57
58    [StorableConstructor]
59    protected OneRClassificationModel(bool deserializing) : base(deserializing) { }
60    protected OneRClassificationModel(OneRClassificationModel original, Cloner cloner)
61      : base(original, cloner) {
62      this.variable = (string)original.variable;
63      this.splits = (double[])original.splits.Clone();
64      this.classes = (double[])original.classes.Clone();
65    }
66    public override IDeepCloneable Clone(Cloner cloner) { return new OneRClassificationModel(this, cloner); }
67
68    public OneRClassificationModel(string variable, double[] splits, double[] classes, double missingValuesClass = double.NaN)
69      : base() {
70      if (splits.Length != classes.Length) {
71        throw new ArgumentException("Number of splits and classes has to be equal.");
72      }
73      if (!Double.IsPositiveInfinity(splits[splits.Length - 1])) {
74        throw new ArgumentException("Last split has to be double.PositiveInfinity, so that all values are covered.");
75      }
76      this.name = ItemName;
77      this.description = ItemDescription;
78      this.variable = variable;
79      this.splits = splits;
80      this.classes = classes;
81      this.missingValuesClass = missingValuesClass;
82    }
83
84    // uses sorting to return the values in the order of rows, instead of using nested for loops
85    // to avoid O(n²) runtime
86    public IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
87      var values = dataset.GetDoubleValues(Variable, rows).ToArray();
88      var rowsArray = rows.ToArray();
89      var order = Enumerable.Range(0, rowsArray.Length).ToArray();
90      double[] estimated = new double[rowsArray.Length];
91      Array.Sort(rowsArray, order);
92      Array.Sort(values, rowsArray);
93      int curSplit = 0, curIndex = 0;
94      while (curIndex < values.Length && Double.IsNaN(values[curIndex])) {
95        estimated[curIndex] = MissingValuesClass;
96        curIndex++;
97      }
98      while (curSplit < Splits.Length) {
99        while (curIndex < values.Length && Splits[curSplit] > values[curIndex]) {
100          estimated[curIndex] = classes[curSplit];
101          curIndex++;
102        }
103        curSplit++;
104      }
105      Array.Sort(rowsArray, estimated);
106      Array.Sort(order, estimated);
107      return estimated;
108    }
109
110    public IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
111      return new OneRClassificationSolution(this, new ClassificationProblemData(problemData));
112    }
113
114  }
115}
Note: See TracBrowser for help on using the repository browser.