[5649] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
[12009] | 3 | * Copyright (C) 2002-2015 Heuristic and Evolutionary Algorithms Laboratory (HEAL)
|
---|
[5649] | 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 |
|
---|
[5777] | 22 | using System;
|
---|
[5649] | 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using HeuristicLab.Common;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Persistence.Default.CompositeSerializers.Storable;
|
---|
| 28 |
|
---|
| 29 | namespace HeuristicLab.Problems.DataAnalysis {
|
---|
| 30 | /// <summary>
|
---|
| 31 | /// Represents discriminant function classification data analysis models.
|
---|
| 32 | /// </summary>
|
---|
| 33 | [StorableClass]
|
---|
| 34 | [Item("DiscriminantFunctionClassificationModel", "Represents a classification model that uses a discriminant function and classification thresholds.")]
|
---|
[8679] | 35 | public class DiscriminantFunctionClassificationModel : NamedItem, IDiscriminantFunctionClassificationModel {
|
---|
[5649] | 36 | [Storable]
|
---|
| 37 | private IRegressionModel model;
|
---|
[8679] | 38 | public IRegressionModel Model {
|
---|
| 39 | get { return model; }
|
---|
| 40 | private set { model = value; }
|
---|
| 41 | }
|
---|
[5736] | 42 |
|
---|
[5649] | 43 | [Storable]
|
---|
| 44 | private double[] classValues;
|
---|
[5678] | 45 | public IEnumerable<double> ClassValues {
|
---|
| 46 | get { return (double[])classValues.Clone(); }
|
---|
[5736] | 47 | private set { classValues = value.ToArray(); }
|
---|
[5678] | 48 | }
|
---|
[5736] | 49 |
|
---|
[5678] | 50 | [Storable]
|
---|
| 51 | private double[] thresholds;
|
---|
| 52 | public IEnumerable<double> Thresholds {
|
---|
| 53 | get { return (IEnumerable<double>)thresholds.Clone(); }
|
---|
[5736] | 54 | private set { thresholds = value.ToArray(); }
|
---|
[5678] | 55 | }
|
---|
[5649] | 56 |
|
---|
[8623] | 57 | private IDiscriminantFunctionThresholdCalculator thresholdCalculator;
|
---|
| 58 | [Storable]
|
---|
| 59 | public IDiscriminantFunctionThresholdCalculator ThresholdCalculator {
|
---|
| 60 | get { return thresholdCalculator; }
|
---|
| 61 | private set { thresholdCalculator = value; }
|
---|
| 62 | }
|
---|
[5678] | 63 |
|
---|
[8623] | 64 |
|
---|
[5649] | 65 | [StorableConstructor]
|
---|
[5681] | 66 | protected DiscriminantFunctionClassificationModel(bool deserializing) : base(deserializing) { }
|
---|
[5649] | 67 | protected DiscriminantFunctionClassificationModel(DiscriminantFunctionClassificationModel original, Cloner cloner)
|
---|
| 68 | : base(original, cloner) {
|
---|
| 69 | model = cloner.Clone(original.model);
|
---|
| 70 | classValues = (double[])original.classValues.Clone();
|
---|
[5678] | 71 | thresholds = (double[])original.thresholds.Clone();
|
---|
[5649] | 72 | }
|
---|
[5736] | 73 |
|
---|
[8623] | 74 | public DiscriminantFunctionClassificationModel(IRegressionModel model, IDiscriminantFunctionThresholdCalculator thresholdCalculator)
|
---|
[5649] | 75 | : base() {
|
---|
| 76 | this.name = ItemName;
|
---|
| 77 | this.description = ItemDescription;
|
---|
| 78 | this.model = model;
|
---|
[8623] | 79 | this.classValues = new double[0];
|
---|
| 80 | this.thresholds = new double[0];
|
---|
| 81 | this.thresholdCalculator = thresholdCalculator;
|
---|
[5649] | 82 | }
|
---|
| 83 |
|
---|
[8623] | 84 | [StorableHook(HookType.AfterDeserialization)]
|
---|
| 85 | private void AfterDeserialization() {
|
---|
| 86 | if (ThresholdCalculator == null) ThresholdCalculator = new AccuracyMaximizationThresholdCalculator();
|
---|
| 87 | }
|
---|
| 88 |
|
---|
[8679] | 89 | public override IDeepCloneable Clone(Cloner cloner) {
|
---|
| 90 | return new DiscriminantFunctionClassificationModel(this, cloner);
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[5736] | 93 | public void SetThresholdsAndClassValues(IEnumerable<double> thresholds, IEnumerable<double> classValues) {
|
---|
| 94 | var classValuesArr = classValues.ToArray();
|
---|
| 95 | var thresholdsArr = thresholds.ToArray();
|
---|
| 96 | if (thresholdsArr.Length != classValuesArr.Length) throw new ArgumentException();
|
---|
| 97 |
|
---|
| 98 | this.classValues = classValuesArr;
|
---|
| 99 | this.thresholds = thresholdsArr;
|
---|
| 100 | OnThresholdsChanged(EventArgs.Empty);
|
---|
| 101 | }
|
---|
| 102 |
|
---|
[8623] | 103 | public virtual void RecalculateModelParameters(IClassificationProblemData problemData, IEnumerable<int> rows) {
|
---|
| 104 | double[] classValues;
|
---|
| 105 | double[] thresholds;
|
---|
| 106 | var targetClassValues = problemData.Dataset.GetDoubleValues(problemData.TargetVariable, rows);
|
---|
| 107 | var estimatedTrainingValues = GetEstimatedValues(problemData.Dataset, rows);
|
---|
| 108 | thresholdCalculator.Calculate(problemData, estimatedTrainingValues, targetClassValues, out classValues, out thresholds);
|
---|
| 109 | SetThresholdsAndClassValues(thresholds, classValues);
|
---|
| 110 | }
|
---|
| 111 |
|
---|
| 112 |
|
---|
[5649] | 113 | public IEnumerable<double> GetEstimatedValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
| 114 | return model.GetEstimatedValues(dataset, rows);
|
---|
| 115 | }
|
---|
| 116 |
|
---|
| 117 | public IEnumerable<double> GetEstimatedClassValues(Dataset dataset, IEnumerable<int> rows) {
|
---|
[8623] | 118 | if (!Thresholds.Any() && !ClassValues.Any()) throw new ArgumentException("No thresholds and class values were set for the current classification model.");
|
---|
[5649] | 119 | foreach (var x in GetEstimatedValues(dataset, rows)) {
|
---|
| 120 | int classIndex = 0;
|
---|
[5678] | 121 | // find first threshold value which is larger than x => class index = threshold index + 1
|
---|
[5649] | 122 | for (int i = 0; i < thresholds.Length; i++) {
|
---|
| 123 | if (x > thresholds[i]) classIndex++;
|
---|
| 124 | else break;
|
---|
| 125 | }
|
---|
[5736] | 126 | yield return classValues.ElementAt(classIndex - 1);
|
---|
[5649] | 127 | }
|
---|
| 128 | }
|
---|
[5678] | 129 | #region events
|
---|
| 130 | public event EventHandler ThresholdsChanged;
|
---|
| 131 | protected virtual void OnThresholdsChanged(EventArgs e) {
|
---|
| 132 | var listener = ThresholdsChanged;
|
---|
| 133 | if (listener != null) listener(this, e);
|
---|
| 134 | }
|
---|
[5649] | 135 | #endregion
|
---|
[6604] | 136 |
|
---|
[8679] | 137 | public virtual IDiscriminantFunctionClassificationSolution CreateDiscriminantFunctionClassificationSolution(IClassificationProblemData problemData) {
|
---|
[8857] | 138 | return new DiscriminantFunctionClassificationSolution(this, new ClassificationProblemData(problemData));
|
---|
[8679] | 139 | }
|
---|
| 140 |
|
---|
| 141 | public virtual IClassificationSolution CreateClassificationSolution(IClassificationProblemData problemData) {
|
---|
| 142 | return CreateDiscriminantFunctionClassificationSolution(problemData);
|
---|
| 143 | }
|
---|
[5649] | 144 | }
|
---|
| 145 | }
|
---|