1 | #region License Information
|
---|
2 | /* HeuristicLab
|
---|
3 | * Copyright (C) 2002-2011 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 |
|
---|
22 | using System.Collections.Generic;
|
---|
23 | using System.Linq;
|
---|
24 | using HeuristicLab.Common;
|
---|
25 | using HeuristicLab.Core;
|
---|
26 | using HeuristicLab.Data;
|
---|
27 | using HeuristicLab.Operators;
|
---|
28 | using HeuristicLab.Parameters;
|
---|
29 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
30 | using HeuristicLab.Optimization;
|
---|
31 | using System;
|
---|
32 |
|
---|
33 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
34 | /// <summary>
|
---|
35 | /// Represents discriminant function classification data analysis models.
|
---|
36 | /// </summary>
|
---|
37 | [StorableClass]
|
---|
38 | [Item("DiscriminantFunctionClassificationModel", "Represents a classification model that uses a discriminant function and classification thresholds.")]
|
---|
39 | public class DiscriminantFunctionClassificationModel : NamedItem, IDiscriminantFunctionClassificationModel {
|
---|
40 | [Storable]
|
---|
41 | private IRegressionModel model;
|
---|
42 |
|
---|
43 | [Storable]
|
---|
44 | private double[] classValues;
|
---|
45 | public IEnumerable<double> ClassValues {
|
---|
46 | get { return (double[])classValues.Clone(); }
|
---|
47 | private set { classValues = value.ToArray(); }
|
---|
48 | }
|
---|
49 |
|
---|
50 | [Storable]
|
---|
51 | private double[] thresholds;
|
---|
52 | public IEnumerable<double> Thresholds {
|
---|
53 | get { return (IEnumerable<double>)thresholds.Clone(); }
|
---|
54 | private set { thresholds = value.ToArray(); }
|
---|
55 | }
|
---|
56 |
|
---|
57 |
|
---|
58 | [StorableConstructor]
|
---|
59 | protected DiscriminantFunctionClassificationModel(bool deserializing) : base(deserializing) { }
|
---|
60 | protected DiscriminantFunctionClassificationModel(DiscriminantFunctionClassificationModel original, Cloner cloner)
|
---|
61 | : base(original, cloner) {
|
---|
62 | model = cloner.Clone(original.model);
|
---|
63 | classValues = (double[])original.classValues.Clone();
|
---|
64 | thresholds = (double[])original.thresholds.Clone();
|
---|
65 | }
|
---|
66 |
|
---|
67 | public DiscriminantFunctionClassificationModel(IRegressionModel model)
|
---|
68 | : base() {
|
---|
69 | this.name = ItemName;
|
---|
70 | this.description = ItemDescription;
|
---|
71 | this.model = model;
|
---|
72 | this.classValues = new double[] { 0.0 };
|
---|
73 | this.thresholds = new double[] { double.NegativeInfinity };
|
---|
74 | }
|
---|
75 |
|
---|
76 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
77 | return new DiscriminantFunctionClassificationModel(this, cloner);
|
---|
78 | }
|
---|
79 |
|
---|
80 | public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) {
|
---|
81 | var classValuesArr = classValues.ToArray();
|
---|
82 | var thresholdsArr = thresholds.ToArray();
|
---|
83 | if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException();
|
---|
84 |
|
---|
85 | this.classValues = classValuesArr;
|
---|
86 | this.thresholds = thresholdsArr;
|
---|
87 | OnThresholdsChanged(EventArgs.Empty);
|
---|
88 | }
|
---|
89 |
|
---|
90 | public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
91 | return model.GetEstimatedValues(dataset, rows);
|
---|
92 | }
|
---|
93 |
|
---|
94 | public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
95 | foreach (var x in GetEstimatedValues(dataset, rows)) {
|
---|
96 | int classIndex = 0;
|
---|
97 | // find first threshold value which is larger than x => class index = threshold index + 1
|
---|
98 | for (int i = 0; i < thresholds.Length; i++) {
|
---|
99 | if (x > thresholds[i]) classIndex++;
|
---|
100 | else break;
|
---|
101 | }
|
---|
102 | yield return classValues.ElementAt(classIndex - 1);
|
---|
103 | }
|
---|
104 | }
|
---|
105 | #region events
|
---|
106 | public event EventHandler ThresholdsChanged;
|
---|
107 | protected virtual void OnThresholdsChanged(EventArgs e) {
|
---|
108 | var listener = ThresholdsChanged;
|
---|
109 | if (listener != null) listener(this, e);
|
---|
110 | }
|
---|
111 | #endregion
|
---|
112 | }
|
---|
113 | }
|
---|