- Timestamp:
- 07/06/17 14:31:05 (8 years ago)
- Location:
- stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression
- Files:
-
- 2 copied
Legend:
- Unmodified
- Added
- Removed
-
stable/HeuristicLab.Problems.DataAnalysis.Views/3.4/Regression/RegressionSolutionResidualAnalysisView.cs
r14890 r15161 20 20 #endregion 21 21 using System; 22 using System.Collections.Generic; 22 23 using System.Drawing; 23 24 using System.Linq; 24 using System.Runtime.Remoting.Contexts;25 using System.Threading.Tasks;26 25 using HeuristicLab.Data; 27 26 using HeuristicLab.MainForm; … … 31 30 [View("Residual Analysis")] 32 31 [Content(typeof(IRegressionSolution))] 33 public partial class RegressionSolutionResidualAnalysisView : DataAnalysisSolutionEvaluationView { 32 public sealed partial class RegressionSolutionResidualAnalysisView : DataAnalysisSolutionEvaluationView { 33 34 // names should be relatively save to prevent collisions with variable names in the dataset 35 private const string TargetLabel = "> Target"; 36 private const string PredictionLabel = "> Prediction"; 37 private const string ResidualLabel = "> Residual"; 38 private const string AbsResidualLabel = "> Residual (abs.)"; 39 private const string RelativeErrorLabel = "> Relative Error"; 40 private const string AbsRelativeErrorLabel = "> Relative Error (abs.)"; 41 private const string PartitionLabel = "> Partition"; 34 42 35 43 public new IRegressionSolution Content { 36 44 get { return (IRegressionSolution)base.Content; } 37 set { 38 base.Content = value; 39 } 45 set { base.Content = value; } 40 46 } 41 47 42 public RegressionSolutionResidualAnalysisView() 43 : base() { 48 public RegressionSolutionResidualAnalysisView() : base() { 44 49 InitializeComponent(); 45 50 } … … 58 63 } 59 64 60 pr otected virtualvoid Content_ProblemDataChanged(object sender, EventArgs e) {65 private void Content_ProblemDataChanged(object sender, EventArgs e) { 61 66 OnContentChanged(); 62 67 } 63 68 64 pr otected virtualvoid Content_ModelChanged(object sender, EventArgs e) {69 private void Content_ModelChanged(object sender, EventArgs e) { 65 70 OnContentChanged(); 66 71 } … … 83 88 var ds = problemData.Dataset; 84 89 var runs = new RunCollection(); 90 // determine relevant variables (at least two different values) 91 var doubleVars = ds.DoubleVariables.Where(vn => ds.GetDoubleValues(vn).Max() > ds.GetDoubleValues(vn).Min()).ToArray(); 92 var stringVars = ds.StringVariables.Where(vn => ds.GetStringValues(vn).Distinct().Skip(1).Any()).ToArray(); 93 var dateTimeVars = ds.DateTimeVariables.Where(vn => ds.GetDateTimeValues(vn).Distinct().Skip(1).Any()).ToArray(); 94 85 95 // produce training and test values separately as they might overlap (e.g. for ensembles) 86 96 var predictedValuesTrain = Content.EstimatedTrainingValues.ToArray(); 87 97 int j = 0; // idx for predictedValues array 88 var partitionId = "Partition";89 while (ds.VariableNames.Contains(partitionId)) partitionId += "_";90 98 foreach (var i in problemData.TrainingIndices) { 91 var run = CreateRunForIdx(i, problemData );99 var run = CreateRunForIdx(i, problemData, doubleVars, stringVars, dateTimeVars); 92 100 var targetValue = ds.GetDoubleValue(problemData.TargetVariable, i); 93 101 AddErrors(run, predictedValuesTrain[j++], targetValue); 94 run.Results.Add( partitionId, new StringValue("Training"));102 run.Results.Add(PartitionLabel, new StringValue("Training")); 95 103 run.Color = Color.Gold; 96 104 runs.Add(run); … … 99 107 j = 0; 100 108 foreach (var i in problemData.TestIndices) { 101 var run = CreateRunForIdx(i, problemData );109 var run = CreateRunForIdx(i, problemData, doubleVars, stringVars, dateTimeVars); 102 110 var targetValue = ds.GetDoubleValue(problemData.TargetVariable, i); 103 111 AddErrors(run, predictedValuesTest[j++], targetValue); 104 run.Results.Add( partitionId, new StringValue("Test"));112 run.Results.Add(PartitionLabel, new StringValue("Test")); 105 113 run.Color = Color.Red; 106 114 runs.Add(run); … … 119 127 var residual = target - pred; 120 128 var relError = residual / target; 121 var predId = "Prediction"; 122 while (run.Results.ContainsKey(predId)) predId += "_"; 123 var resId = "Residual"; 124 while (run.Results.ContainsKey(resId)) resId += "_"; 125 var relErrorId = "Rel. Error"; 126 while (run.Results.ContainsKey(relErrorId)) relErrorId+= "_"; 127 run.Results.Add(predId, new DoubleValue(pred)); 128 run.Results.Add(resId, new DoubleValue(residual)); 129 run.Results.Add(relErrorId, new DoubleValue(relError)); 129 run.Results.Add(TargetLabel, new DoubleValue(target)); 130 run.Results.Add(PredictionLabel, new DoubleValue(pred)); 131 run.Results.Add(ResidualLabel, new DoubleValue(residual)); 132 run.Results.Add(AbsResidualLabel, new DoubleValue(Math.Abs(residual))); 133 run.Results.Add(RelativeErrorLabel, new DoubleValue(relError)); 134 run.Results.Add(AbsRelativeErrorLabel, new DoubleValue(Math.Abs(relError))); 130 135 } 131 136 132 private IRun CreateRunForIdx(int i, IRegressionProblemData problemData ) {137 private IRun CreateRunForIdx(int i, IRegressionProblemData problemData, IEnumerable<string> doubleVars, IEnumerable<string> stringVars, IEnumerable<string> dateTimeVars) { 133 138 var ds = problemData.Dataset; 134 139 var run = new Run(); 135 foreach (var variableName in d s.DoubleVariables) {140 foreach (var variableName in doubleVars) { 136 141 run.Results.Add(variableName, new DoubleValue(ds.GetDoubleValue(variableName, i))); 137 142 } 138 foreach (var variableName in ds.StringVariables) {143 foreach (var variableName in stringVars) { 139 144 run.Results.Add(variableName, new StringValue(ds.GetStringValue(variableName, i))); 140 145 } 141 146 foreach (var variableName in dateTimeVars) { 147 run.Results.Add(variableName, new DateTimeValue(ds.GetDateTimeValue(variableName, i))); 148 } 142 149 return run; 143 150 }
Note: See TracChangeset
for help on using the changeset viewer.