[645] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2008 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 HeuristicLab.Core;
|
---|
| 23 | using HeuristicLab.Data;
|
---|
| 24 |
|
---|
[2351] | 25 | namespace HeuristicLab.Modeling {
|
---|
| 26 | public class SimpleConfusionMatrixEvaluator : OperatorBase {
|
---|
[2357] | 27 | protected const int ORIGINAL_INDEX = 0;
|
---|
| 28 | protected const int ESTIMATION_INDEX = 1;
|
---|
[645] | 29 | public override string Description {
|
---|
| 30 | get {
|
---|
| 31 | return @"Calculates the classifcation matrix of the model.";
|
---|
| 32 | }
|
---|
| 33 | }
|
---|
| 34 |
|
---|
[2351] | 35 | public SimpleConfusionMatrixEvaluator()
|
---|
[645] | 36 | : base() {
|
---|
[2351] | 37 | AddVariableInfo(new VariableInfo("Values", "Original and predicted target values generated by a model", typeof(DoubleMatrixData), VariableKind.In));
|
---|
[658] | 38 | AddVariableInfo(new VariableInfo("ConfusionMatrix", "The confusion matrix of the model", typeof(IntMatrixData), VariableKind.New));
|
---|
[645] | 39 | }
|
---|
| 40 |
|
---|
[2351] | 41 | public override IOperation Apply(IScope scope) {
|
---|
| 42 | double[,] values = GetVariableValue<DoubleMatrixData>("Values", scope, true).Data;
|
---|
| 43 | int[,] confusionMatrix = Calculate(values);
|
---|
[702] | 44 | IntMatrixData matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);
|
---|
[712] | 45 | if (matrix == null) {
|
---|
[2351] | 46 | matrix = new IntMatrixData(confusionMatrix);
|
---|
[658] | 47 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));
|
---|
[645] | 48 | }
|
---|
| 49 |
|
---|
[2351] | 50 | return null;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
| 53 | public static int[,] Calculate(double[,] values) {
|
---|
| 54 | double[] classes = SimpleAccuracyEvaluator.CalculateTargetClasses(values);
|
---|
| 55 | double[] thresholds = SimpleAccuracyEvaluator.CalculateThresholds(classes);
|
---|
| 56 | int nSamples = values.GetLength(0);
|
---|
| 57 | int[,] confusionMatrix = new int[classes.Length, classes.Length];
|
---|
| 58 | for (int sample = 0; sample < nSamples; sample++) {
|
---|
[2357] | 59 | double est = values[sample, ESTIMATION_INDEX];
|
---|
| 60 | double origClass = values[sample, ORIGINAL_INDEX];
|
---|
[645] | 61 | int estClassIndex = -1;
|
---|
| 62 | // if estimation is lower than the smallest threshold value -> estimated class is the lower class
|
---|
[712] | 63 | if (est < thresholds[0]) estClassIndex = 0;
|
---|
[645] | 64 | // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
|
---|
[712] | 65 | else if (est >= thresholds[thresholds.Length - 1]) estClassIndex = classes.Length - 1;
|
---|
[645] | 66 | else {
|
---|
| 67 | // otherwise the estimated class is the class which upper threshold is larger than the estimated value
|
---|
[712] | 68 | for (int k = 0; k < thresholds.Length; k++) {
|
---|
| 69 | if (thresholds[k] > est) {
|
---|
[645] | 70 | estClassIndex = k;
|
---|
| 71 | break;
|
---|
| 72 | }
|
---|
| 73 | }
|
---|
| 74 | }
|
---|
| 75 |
|
---|
[702] | 76 | // find the first threshold index that is larger to the original value
|
---|
[712] | 77 | int origClassIndex = classes.Length - 1;
|
---|
| 78 | for (int i = 0; i < thresholds.Length; i++) {
|
---|
| 79 | if (origClass < thresholds[i]) {
|
---|
[702] | 80 | origClassIndex = i;
|
---|
| 81 | break;
|
---|
| 82 | }
|
---|
[645] | 83 | }
|
---|
[2351] | 84 | confusionMatrix[origClassIndex, estClassIndex]++;
|
---|
[645] | 85 | }
|
---|
[2351] | 86 | return confusionMatrix;
|
---|
[645] | 87 | }
|
---|
| 88 | }
|
---|
| 89 | }
|
---|