Last change
on this file since 2498 was
2136,
checked in by gkronber, 15 years ago
|
Improved handling of exceptional cases in data-based modeling evaluators. #688 (SimpleEvaluators should handle exceptional cases more gracefully)
|
File size:
1.3 KB
|
Line | |
---|
1 | using System;
|
---|
2 | using System.Collections.Generic;
|
---|
3 | using System.Linq;
|
---|
4 | using System.Text;
|
---|
5 | using HeuristicLab.Core;
|
---|
6 | using HeuristicLab.Data;
|
---|
7 | using HeuristicLab.DataAnalysis;
|
---|
8 |
|
---|
9 | namespace HeuristicLab.Modeling {
|
---|
10 | public class SimpleMSEEvaluator : SimpleEvaluatorBase {
|
---|
11 |
|
---|
12 | public override string OutputVariableName {
|
---|
13 | get {
|
---|
14 | return "MSE";
|
---|
15 | }
|
---|
16 | }
|
---|
17 | public override double Evaluate(double[,] values) {
|
---|
18 | try {
|
---|
19 | return Calculate(values);
|
---|
20 | }
|
---|
21 | catch (ArgumentException) {
|
---|
22 | return double.PositiveInfinity;
|
---|
23 | }
|
---|
24 | }
|
---|
25 |
|
---|
26 | public static double Calculate(double[,] values) {
|
---|
27 | double sse = 0;
|
---|
28 | double cnt = 0;
|
---|
29 | for (int i = 0; i < values.GetLength(0); i++) {
|
---|
30 | double estimated = values[i, 0];
|
---|
31 | double target = values[i, 1];
|
---|
32 | if (!double.IsNaN(estimated) && !double.IsInfinity(estimated) &&
|
---|
33 | !double.IsNaN(target) && !double.IsInfinity(target)) {
|
---|
34 | double error = estimated - target;
|
---|
35 | sse += error * error;
|
---|
36 | cnt++;
|
---|
37 | }
|
---|
38 | }
|
---|
39 | if (cnt > 0) {
|
---|
40 | double mse = sse / cnt;
|
---|
41 | return mse;
|
---|
42 | } else {
|
---|
43 | throw new ArgumentException("Mean squared errors is not defined for input vectors of NaN or Inf");
|
---|
44 | }
|
---|
45 | }
|
---|
46 | }
|
---|
47 | }
|
---|
Note: See
TracBrowser
for help on using the repository browser.