[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 System;
|
---|
| 23 | using System.Collections.Generic;
|
---|
| 24 | using System.Linq;
|
---|
| 25 | using System.Text;
|
---|
| 26 | using HeuristicLab.Core;
|
---|
| 27 | using HeuristicLab.Data;
|
---|
| 28 | using HeuristicLab.GP.StructureIdentification;
|
---|
| 29 |
|
---|
[668] | 30 | namespace HeuristicLab.GP.StructureIdentification.Classification {
|
---|
[702] | 31 | public class ConfusionMatrixEvaluator : GPClassificationEvaluatorBase {
|
---|
[645] | 32 | public override string Description {
|
---|
| 33 | get {
|
---|
| 34 | return @"Calculates the classifcation matrix of the model.";
|
---|
| 35 | }
|
---|
| 36 | }
|
---|
| 37 |
|
---|
[658] | 38 | public ConfusionMatrixEvaluator()
|
---|
[645] | 39 | : base() {
|
---|
[658] | 40 | AddVariableInfo(new VariableInfo("ConfusionMatrix", "The confusion matrix of the model", typeof(IntMatrixData), VariableKind.New));
|
---|
[645] | 41 | }
|
---|
| 42 |
|
---|
[1796] | 43 | public override void Evaluate(IScope scope, ITreeEvaluator evaluator, IFunctionTree tree, HeuristicLab.DataAnalysis.Dataset dataset, int targetVariable, double[] classes, double[] thresholds, int start, int end) {
|
---|
[702] | 44 | IntMatrixData matrix = GetVariableValue<IntMatrixData>("ConfusionMatrix", scope, false, false);
|
---|
[712] | 45 | if (matrix == null) {
|
---|
[702] | 46 | matrix = new IntMatrixData(new int[classes.Length, classes.Length]);
|
---|
[658] | 47 | scope.AddVariable(new HeuristicLab.Core.Variable(scope.TranslateName("ConfusionMatrix"), matrix));
|
---|
[645] | 48 | }
|
---|
| 49 |
|
---|
| 50 | int nSamples = end - start;
|
---|
[712] | 51 | for (int sample = start; sample < end; sample++) {
|
---|
[1796] | 52 | double est = evaluator.Evaluate(tree, sample);
|
---|
[712] | 53 | double origClass = dataset.GetValue(sample, targetVariable);
|
---|
[645] | 54 | int estClassIndex = -1;
|
---|
| 55 | // if estimation is lower than the smallest threshold value -> estimated class is the lower class
|
---|
[712] | 56 | if (est < thresholds[0]) estClassIndex = 0;
|
---|
[645] | 57 | // if estimation is larger (or equal) than the largest threshold value -> estimated class is the upper class
|
---|
[712] | 58 | else if (est >= thresholds[thresholds.Length - 1]) estClassIndex = classes.Length - 1;
|
---|
[645] | 59 | else {
|
---|
| 60 | // otherwise the estimated class is the class which upper threshold is larger than the estimated value
|
---|
[712] | 61 | for (int k = 0; k < thresholds.Length; k++) {
|
---|
| 62 | if (thresholds[k] > est) {
|
---|
[645] | 63 | estClassIndex = k;
|
---|
| 64 | break;
|
---|
| 65 | }
|
---|
| 66 | }
|
---|
| 67 | }
|
---|
| 68 |
|
---|
[702] | 69 | // find the first threshold index that is larger to the original value
|
---|
[712] | 70 | int origClassIndex = classes.Length - 1;
|
---|
| 71 | for (int i = 0; i < thresholds.Length; i++) {
|
---|
| 72 | if (origClass < thresholds[i]) {
|
---|
[702] | 73 | origClassIndex = i;
|
---|
| 74 | break;
|
---|
| 75 | }
|
---|
[645] | 76 | }
|
---|
| 77 | matrix.Data[origClassIndex, estClassIndex]++;
|
---|
| 78 | }
|
---|
| 79 | }
|
---|
| 80 | }
|
---|
| 81 | }
|
---|