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