Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2213_irace/HeuristicLab.Algorithms.DataAnalysis/3.4/BaselineClassifiers/OneFactorClassificationModel.cs @ 15923

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

#2640: Updated year of copyrights in license headers

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