Free cookie consent management tool by TermsFeed Policy Generator

source: branches/symbreg-factors-2650/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneFactorClassificationModel.cs @ 14242

Last change on this file since 14242 was 14242, checked in by gkronber, 8 years ago

#2650: added support for factor variables to OneR algorithm

File size: 3.9 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.Diagnostics.Eventing.Reader;
25using System.Linq;
26using HeuristicLab.Common;
27using HeuristicLab.Core;
28using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
29using HeuristicLab.Problems.DataAnalysis;
30
31namespace HeuristicLab.Algorithms.DataAnalysis {
32  [StorableClass]
33  [Item("OneFactor Classification Model", "A model that uses only one categorical feature (factor) to determine the class.")]
34  public class OneFactorClassificationModel : ClassificationModel {
35    public override IEnumerable<string> VariablesUsedForPrediction {
36      get { return new[] { Variable }; }
37    }
38
39    [Storable]
40    protected string variable;
41    public string Variable {
42      get { return variable; }
43    }
44
45    [Storable]
46    protected string[] variableValues;
47    public string[] VariableValues {
48      get { return variableValues; }
49    }
50
51    [Storable]
52    protected double[] classes;
53    public double[] Classes {
54      get { return classes; }
55    }
56
57    [Storable]
58    protected double defaultClass;
59    public double DefaultClass {
60      get { return defaultClass; }
61    }
62
63    [StorableConstructor]
64    protected OneFactorClassificationModel(bool deserializing) : base(deserializing) { }
65    protected OneFactorClassificationModel(OneFactorClassificationModel original, Cloner cloner)
66      : base(original, cloner) {
67      this.variable = (string)original.variable;
68      this.variableValues = (string[])original.variableValues.Clone();
69      this.classes = (double[])original.classes.Clone();
70      this.defaultClass = original.defaultClass;
71    }
72    public override IDeepCloneable Clone(Cloner cloner) { return new OneFactorClassificationModel(this, cloner); }
73
74    public OneFactorClassificationModel(string targetVariable, string variable, string[] variableValues, double[] classes, double defaultClass = double.NaN)
75      : base(targetVariable) {
76      if (variableValues.Length != classes.Length) {
77        throw new ArgumentException("Number of variable values and classes has to be equal.");
78      }
79      this.name = ItemName;
80      this.description = ItemDescription;
81      this.variable = variable;
82      this.variableValues = variableValues;
83      this.classes = classes;
84      this.defaultClass = double.IsNaN(defaultClass) ? classes.First() : defaultClass;
85      Array.Sort(variableValues, classes);
86    }
87
88    public override IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows) {
89      return dataset.GetStringValues(Variable, rows)
90        .Select(GetPredictedValueForInput);
91    }
92
93    private double GetPredictedValueForInput(string val) {
94      var matchingIdx = Array.BinarySearch(variableValues, val);
95      if (matchingIdx >= 0) return classes[matchingIdx];
96      else return DefaultClass;
97    }
98
99    public override IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
100      return new OneFactorClassificationSolution(this, new ClassificationProblemData(problemData));
101    }
102
103  }
104}
Note: See TracBrowser for help on using the repository browser.