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

Last change on this file since 14185 was 14185, checked in by swagner, 3 years ago

#2526: Updated year of copyrights in license headers

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