Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2922-DataCompletenessChartPerf/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneRClassificationModel.cs @ 15941

Last change on this file since 15941 was 15583, checked in by swagner, 7 years ago

#2640: Updated year of copyrights in license headers

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