[9119] | 1 | #region License Information
|
---|
| 2 | /* HeuristicLab
|
---|
| 3 | * Copyright (C) 2002-2012 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 |
|
---|
| 26 | namespace HeuristicLab.Problems.DataAnalysis.OnlineCalculators {
|
---|
| 27 | public class ConfusionMatrixCalculator {
|
---|
| 28 | public static double[,] Calculate(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) {
|
---|
| 29 | if (originalValues.Count() != estimatedValues.Count()) {
|
---|
| 30 | throw new ArgumentException("Number of elements in originalValues and estimatedValues enumerations doesn't match.");
|
---|
| 31 | }
|
---|
| 32 |
|
---|
| 33 | IEnumerable<double> classValues = originalValues.Distinct();
|
---|
| 34 | IEnumerable<double> estimatedClassValues = estimatedValues.Distinct();
|
---|
| 35 |
|
---|
| 36 | if (!estimatedClassValues.All(x => originalValues.Contains(x))) {
|
---|
| 37 | errorState = OnlineCalculatorError.InvalidValueAdded;
|
---|
| 38 | return null;
|
---|
| 39 | }
|
---|
| 40 |
|
---|
| 41 | int classes = classValues.Count();
|
---|
| 42 | double[,] confusionMatrix = new double[classes, classes];
|
---|
| 43 |
|
---|
| 44 | Dictionary<double, int> classValueIndexMapping = new Dictionary<double, int>();
|
---|
| 45 | int index = 0;
|
---|
| 46 | foreach (double classValue in classValues.OrderBy(x => x)) {
|
---|
| 47 | classValueIndexMapping.Add(classValue, index);
|
---|
| 48 | index++;
|
---|
| 49 | }
|
---|
| 50 |
|
---|
| 51 | IEnumerator<double> originalEnumerator = originalValues.GetEnumerator();
|
---|
| 52 | IEnumerator<double> estimatedEnumerator = estimatedValues.GetEnumerator();
|
---|
| 53 | int originalIndex;
|
---|
| 54 | int estimatedIndex;
|
---|
| 55 | while (originalEnumerator.MoveNext() & estimatedEnumerator.MoveNext()) {
|
---|
| 56 | if (!classValueIndexMapping.TryGetValue(originalEnumerator.Current, out originalIndex)) {
|
---|
| 57 | errorState = OnlineCalculatorError.InvalidValueAdded;
|
---|
| 58 | return null;
|
---|
| 59 | }
|
---|
| 60 | if (!classValueIndexMapping.TryGetValue(estimatedEnumerator.Current, out estimatedIndex)) {
|
---|
| 61 | errorState = OnlineCalculatorError.InvalidValueAdded;
|
---|
| 62 | return null;
|
---|
| 63 | }
|
---|
| 64 |
|
---|
| 65 | confusionMatrix[estimatedIndex, originalIndex] += 1;
|
---|
| 66 | }
|
---|
| 67 |
|
---|
| 68 | errorState = OnlineCalculatorError.None;
|
---|
| 69 | return confusionMatrix;
|
---|
| 70 | }
|
---|
| 71 | }
|
---|
| 72 | }
|
---|