Free cookie consent management tool by TermsFeed Policy Generator

source: branches/2971_named_intervals/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Classification/ClassificationModel.cs @ 16640

Last change on this file since 16640 was 16640, checked in by gkronber, 5 years ago

#2971: merged r16565:16631 from trunk/HeuristicLab.Problems.DataAnalysis to branch/HeuristicLab.Problems.DataAnalysis (resolving all conflicts)

File size: 4.6 KB
Line 
1#region License Information
2/* HeuristicLab
3 * Copyright (C) 2002-2019 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 HeuristicLab.Common;
25using HeuristicLab.Core;
26using HEAL.Attic;
27using HEAL.Attic;
28
29namespace HeuristicLab.Problems.DataAnalysis {
30  [StorableType("7E6091F9-86FD-4C47-8935-9C35CAB4261B")]
31  [Item("Classification Model", "Base class for all classification models.")]
32  public abstract class ClassificationModel : DataAnalysisModel, IClassificationModel {
33    [Storable]
34    private string targetVariable;
35    public string TargetVariable {
36      get { return targetVariable; }
37      set {
38        if (string.IsNullOrEmpty(value) || targetVariable == value) return;
39        targetVariable = value;
40        OnTargetVariableChanged(this, EventArgs.Empty);
41      }
42    }
43
44    [StorableConstructor]
45    protected ClassificationModel(StorableConstructorFlag _)
46      : base(_) {
47      targetVariable = string.Empty;
48    }
49    protected ClassificationModel(ClassificationModel original, Cloner cloner)
50      : base(original, cloner) {
51      this.targetVariable = original.targetVariable;
52    }
53
54    protected ClassificationModel(string targetVariable)
55      : base("Classification Model") {
56      this.targetVariable = targetVariable;
57    }
58    protected ClassificationModel(string targetVariable, string name)
59      : base(name) {
60      this.targetVariable = targetVariable;
61    }
62    protected ClassificationModel(string targetVariable, string name, string description)
63      : base(name, description) {
64      this.targetVariable = targetVariable;
65    }
66
67    public abstract IEnumerable<double> GetEstimatedClassValues(IDataset dataset, IEnumerable<int> rows);
68    public abstract IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData);
69
70    public virtual bool IsProblemDataCompatible(IClassificationProblemData problemData, out string errorMessage) {
71      return IsProblemDataCompatible(this, problemData, out errorMessage);
72    }
73
74    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
75      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
76      var classificationProblemData = problemData as IClassificationProblemData;
77      if (classificationProblemData == null)
78        throw new ArgumentException("The problem data is not a regression problem data. Instead a " + problemData.GetType().GetPrettyName() + " was provided.", "problemData");
79      return IsProblemDataCompatible(classificationProblemData, out errorMessage);
80    }
81
82    public static bool IsProblemDataCompatible(IClassificationModel model, IClassificationProblemData problemData, out string errorMessage) {
83      if (model == null) throw new ArgumentNullException("model", "The provided model is null.");
84      if (problemData == null) throw new ArgumentNullException("problemData", "The provided problemData is null.");
85      errorMessage = string.Empty;
86
87      if (model.TargetVariable != problemData.TargetVariable)
88        errorMessage = string.Format("The target variable of the model {0} does not match the target variable of the problemData {1}.", model.TargetVariable, problemData.TargetVariable);
89
90      var evaluationErrorMessage = string.Empty;
91      var datasetCompatible = model.IsDatasetCompatible(problemData.Dataset, out evaluationErrorMessage);
92      if (!datasetCompatible)
93        errorMessage += evaluationErrorMessage;
94
95      return string.IsNullOrEmpty(errorMessage);
96    }
97
98    #region events
99    public event EventHandler TargetVariableChanged;
100    private void OnTargetVariableChanged(object sender, EventArgs args) {
101      var changed = TargetVariableChanged;
102      if (changed != null)
103        changed(sender, args);
104    }
105    #endregion
106  }
107}
Note: See TracBrowser for help on using the repository browser.