Free cookie consent management tool by TermsFeed Policy Generator

source: stable/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneRClassificationModel.cs @ 17511

Last change on this file since 17511 was 17181, checked in by swagner, 5 years ago

#2875: Merged r17180 from trunk to stable

File size: 4.5 KB
RevLine 
[9074]1#region License Information
2/* HeuristicLab
[17181]3 * Copyright (C) Heuristic and Evolutionary Algorithms Laboratory (HEAL)
[9074]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;
[17097]27using HEAL.Attic;
[9074]28using HeuristicLab.Problems.DataAnalysis;
29
30namespace HeuristicLab.Algorithms.DataAnalysis {
[17097]31  [StorableType("C3365B2F-75D6-45F7-9DD9-CD80854F9D75")]
[13098]32  [Item("OneR Classification Model", "A model that uses intervals for one variable to determine the class.")]
[15131]33  public sealed class OneRClassificationModel : ClassificationModel {
[14027]34    public override IEnumerable<string> VariablesUsedForPrediction {
35      get { return new[] { Variable }; }
36    }
37
[9074]38    [Storable]
[15131]39    private string variable;
[9074]40    public string Variable {
41      get { return variable; }
42    }
43
44    [Storable]
[15131]45    private double[] splits;
[9074]46    public double[] Splits {
47      get { return splits; }
48    }
49
50    [Storable]
[15131]51    private double[] classes;
[9074]52    public double[] Classes {
53      get { return classes; }
54    }
55
[9135]56    [Storable]
[15131]57    private double missingValuesClass;
[9135]58    public double MissingValuesClass {
59      get { return missingValuesClass; }
60    }
61
[9074]62    [StorableConstructor]
[17097]63    private OneRClassificationModel(StorableConstructorFlag _) : base(_) { }
[15131]64    private OneRClassificationModel(OneRClassificationModel original, Cloner cloner)
[9074]65      : base(original, cloner) {
[9119]66      this.variable = (string)original.variable;
67      this.splits = (double[])original.splits.Clone();
68      this.classes = (double[])original.classes.Clone();
[15131]69      this.missingValuesClass = original.missingValuesClass;
[9074]70    }
71    public override IDeepCloneable Clone(Cloner cloner) { return new OneRClassificationModel(this, cloner); }
72
[14027]73    public OneRClassificationModel(string targetVariable, string variable, double[] splits, double[] classes, double missingValuesClass = double.NaN)
74      : base(targetVariable) {
[9119]75      if (splits.Length != classes.Length) {
76        throw new ArgumentException("Number of splits and classes has to be equal.");
77      }
[9074]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;
[9135]86      this.missingValuesClass = missingValuesClass;
[9074]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
[14027]91    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
[9074]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;
[9135]99      while (curIndex < values.Length && Double.IsNaN(values[curIndex])) {
100        estimated[curIndex] = MissingValuesClass;
101        curIndex++;
102      }
[9074]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
[14027]115    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
[9074]116      return new OneRClassificationSolution(this, new ClassificationProblemData(problemData));
117    }
118
119  }
120}
Note: See TracBrowser for help on using the repository browser.