Free cookie consent management tool by TermsFeed Policy Generator

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

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

#1998:

  • added OneRClassificationModelView
  • added ClassificationSolutionComparisonView
  • added several calculators (ConfusionMatrixCalculator, FOneScoreCalculator, MatthewsCorrelationCoefficientCalculator)
  • fixed bug in OneR
  • added StorableClass and Item attribute to several classes
File size: 3.9 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  [StorableClass]
32  [Item("1R 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    [StorableConstructor]
53    protected OneRClassificationModel(bool deserializing) : base(deserializing) { }
54    protected OneRClassificationModel(OneRClassificationModel original, Cloner cloner)
55      : base(original, cloner) {
56      this.variable = (string)original.variable;
57      this.splits = (double[])original.splits.Clone();
58      this.classes = (double[])original.classes.Clone();
59    }
60    public override IDeepCloneable Clone(Cloner cloner) { return new OneRClassificationModel(this, cloner); }
61
62    public OneRClassificationModel(string variable, double[] splits, double[] classes)
63      : base() {
64      if (splits.Length != classes.Length) {
65        throw new ArgumentException("Number of splits and classes has to be equal.");
66      }
67      if (!Double.IsPositiveInfinity(splits[splits.Length - 1])) {
68        throw new ArgumentException("Last split has to be double.PositiveInfinity, so that all values are covered.");
69      }
70      this.name = ItemName;
71      this.description = ItemDescription;
72      this.variable = variable;
73      this.splits = splits;
74      this.classes = classes;
75    }
76
77    // uses sorting to return the values in the order of rows, instead of using nested for loops
78    // to avoid O(n²) runtime
79    public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
80      var values = dataset.GetDoubleValues(Variable, rows).ToArray();
81      var rowsArray = rows.ToArray();
82      var order = Enumerable.Range(0, rowsArray.Length).ToArray();
83      double[] estimated = new double[rowsArray.Length];
84      Array.Sort(rowsArray, order);
85      Array.Sort(values, rowsArray);
86      int curSplit = 0, curIndex = 0;
87      while (curSplit < Splits.Length) {
88        while (curIndex < values.Length && Splits[curSplit] > values[curIndex]) {
89          estimated[curIndex] = classes[curSplit];
90          curIndex++;
91        }
92        curSplit++;
93      }
94      Array.Sort(rowsArray, estimated);
95      Array.Sort(order, estimated);
96      return estimated;
97    }
98
99    public IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
100      return new OneRClassificationSolution(this, new ClassificationProblemData(problemData));
101    }
102
103  }
104}
Note: See TracBrowser for help on using the repository browser.