[9074] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[16565] | 3 | * Copyright (C) 2002-2019 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 |
|
---|
| 22 | using System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
[16565] | 27 | using HEAL.Attic;
|
---|
[9074] | 28 | using HeuristicLab.Problems.DataAnalysis;
|
---|
| 29 |
|
---|
| 30 | namespace HeuristicLab.Algorithms.DataAnalysis {
|
---|
[16565] | 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.")]
|
---|
[14826] | 33 | public sealed class OneRClassificationModel : ClassificationModel {
|
---|
[13941] | 34 | public override IEnumerable<string> VariablesUsedForPrediction {
|
---|
| 35 | get { return new[] { Variable }; }
|
---|
[13921] | 36 | }
|
---|
| 37 |
|
---|
[9074] | 38 | [Storable]
|
---|
[14826] | 39 | private string variable;
|
---|
[9074] | 40 | public string Variable {
|
---|
| 41 | get { return variable; }
|
---|
| 42 | }
|
---|
| 43 |
|
---|
| 44 | [Storable]
|
---|
[14826] | 45 | private double[] splits;
|
---|
[9074] | 46 | public double[] Splits {
|
---|
| 47 | get { return splits; }
|
---|
| 48 | }
|
---|
| 49 |
|
---|
| 50 | [Storable]
|
---|
[14826] | 51 | private double[] classes;
|
---|
[9074] | 52 | public double[] Classes {
|
---|
| 53 | get { return classes; }
|
---|
| 54 | }
|
---|
| 55 |
|
---|
[9135] | 56 | [Storable]
|
---|
[14826] | 57 | private double missingValuesClass;
|
---|
[9135] | 58 | public double MissingValuesClass {
|
---|
| 59 | get { return missingValuesClass; }
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[9074] | 62 | [StorableConstructor]
|
---|
[16565] | 63 | private OneRClassificationModel(StorableConstructorFlag _) : base(_) { }
|
---|
[14826] | 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();
|
---|
[14826] | 69 | this.missingValuesClass = original.missingValuesClass;
|
---|
[9074] | 70 | }
|
---|
| 71 | public override IDeepCloneable Clone(Cloner cloner) { return new OneRClassificationModel(this, cloner); }
|
---|
| 72 |
|
---|
[13941] | 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
|
---|
[13941] | 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 |
|
---|
[13941] | 115 | public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
[9074] | 116 | return new OneRClassificationSolution(this, new ClassificationProblemData(problemData));
|
---|
| 117 | }
|
---|
| 118 |
|
---|
| 119 | }
|
---|
| 120 | }
|
---|