Changeset 3892 for trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/SupportVectorRegression
- Timestamp:
- 06/02/10 19:10:35 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/sources/HeuristicLab.Problems.DataAnalysis.Regression/3.3/SupportVectorRegression/BestSupportVectorRegressionSolutionAnalyzer.cs
r3884 r3892 36 36 using HeuristicLab.Problems.DataAnalysis.Evaluators; 37 37 using HeuristicLab.Problems.DataAnalysis.SupportVectorMachine; 38 using HeuristicLab.Problems.DataAnalysis.Regression.Symbolic.Analyzers; 38 39 39 40 namespace HeuristicLab.Problems.DataAnalysis.Regression.SupportVectorRegression { 40 [Item("BestSupportVectorRegressionSolutionAnalyzer", "An operator for analyzing the best s upport vector solution ofregression problems.")]41 [Item("BestSupportVectorRegressionSolutionAnalyzer", "An operator for analyzing the best solution of support vector regression problems.")] 41 42 [StorableClass] 42 public sealed class BestSupportVectorRegressionSolutionAnalyzer : SingleSuccessorOperator, IAnalyzer {43 public sealed class BestSupportVectorRegressionSolutionAnalyzer : RegressionSolutionAnalyzer { 43 44 private const string SupportVectorRegressionModelParameterName = "SupportVectorRegressionModel"; 44 private const string ProblemDataParameterName = "ProblemData"; 45 private const string QualityParameterName = "Quality"; 46 private const string UpperEstimationLimitParameterName = "UpperEstimationLimit"; 47 private const string LowerEstimationLimitParameterName = "LowerEstimationLimit"; 45 private const string BestSolutionInputvariableCountResultName = "Variables used by best solution"; 48 46 private const string BestSolutionParameterName = "BestSolution"; 49 private const string BestSolutionQualityParameterName = "BestSolutionQuality";50 private const string ResultsParameterName = "Results";51 private const string BestSolutionResultName = "Best solution (on validiation set)";52 private const string BestSolutionInputvariableCountResultName = "Variables used by best solution";53 private const string BestSolutionTrainingRSquared = "Best solution R² (training)";54 private const string BestSolutionTestRSquared = "Best solution R² (test)";55 private const string BestSolutionTrainingMse = "Best solution mean squared error (training)";56 private const string BestSolutionTestMse = "Best solution mean squared error (test)";57 private const string BestSolutionTrainingRelativeError = "Best solution average relative error (training)";58 private const string BestSolutionTestRelativeError = "Best solution average relative error (test)";59 47 48 #region parameter properties 60 49 public ScopeTreeLookupParameter<SupportVectorMachineModel> SupportVectorRegressionModelParameter { 61 50 get { return (ScopeTreeLookupParameter<SupportVectorMachineModel>)Parameters[SupportVectorRegressionModelParameterName]; } 62 }63 public IValueLookupParameter<DataAnalysisProblemData> ProblemDataParameter {64 get { return (IValueLookupParameter<DataAnalysisProblemData>)Parameters[ProblemDataParameterName]; }65 }66 public ScopeTreeLookupParameter<DoubleValue> QualityParameter {67 get { return (ScopeTreeLookupParameter<DoubleValue>)Parameters[QualityParameterName]; }68 }69 public IValueLookupParameter<DoubleValue> UpperEstimationLimitParameter {70 get { return (IValueLookupParameter<DoubleValue>)Parameters[UpperEstimationLimitParameterName]; }71 }72 public IValueLookupParameter<DoubleValue> LowerEstimationLimitParameter {73 get { return (IValueLookupParameter<DoubleValue>)Parameters[LowerEstimationLimitParameterName]; }74 51 } 75 52 public ILookupParameter<SupportVectorRegressionSolution> BestSolutionParameter { 76 53 get { return (ILookupParameter<SupportVectorRegressionSolution>)Parameters[BestSolutionParameterName]; } 77 54 } 78 public ILookupParameter<DoubleValue> BestSolutionQualityParameter { 79 get { return (ILookupParameter<DoubleValue>)Parameters[BestSolutionQualityParameterName]; } 55 #endregion 56 #region properties 57 public ItemArray<SupportVectorMachineModel> SupportVectorMachineModel { 58 get { return SupportVectorRegressionModelParameter.ActualValue; } 80 59 } 81 public ILookupParameter<ResultCollection> ResultsParameter { 82 get { return (ILookupParameter<ResultCollection>)Parameters[ResultsParameterName]; } 83 } 60 #endregion 84 61 85 62 public BestSupportVectorRegressionSolutionAnalyzer() 86 63 : base() { 87 64 Parameters.Add(new ScopeTreeLookupParameter<SupportVectorMachineModel>(SupportVectorRegressionModelParameterName, "The support vector regression models to analyze.")); 88 Parameters.Add(new ValueLookupParameter<DataAnalysisProblemData>(ProblemDataParameterName, "The problem data for which the support vector model is a solution.")); 89 Parameters.Add(new ValueLookupParameter<DoubleValue>(UpperEstimationLimitParameterName, "The upper estimation limit that was set for the evaluation of the support vector model.")); 90 Parameters.Add(new ValueLookupParameter<DoubleValue>(LowerEstimationLimitParameterName, "The lower estimation limit that was set for the evaluation of the support vector model.")); 91 Parameters.Add(new ScopeTreeLookupParameter<DoubleValue>(QualityParameterName, "The qualities of the support vector models which should be analyzed.")); 92 Parameters.Add(new LookupParameter<SupportVectorRegressionSolution>(BestSolutionParameterName, "The best support vector solution.")); 93 Parameters.Add(new LookupParameter<DoubleValue>(BestSolutionQualityParameterName, "The quality of the best support vector solution.")); 94 Parameters.Add(new LookupParameter<ResultCollection>(ResultsParameterName, "The result collection where the best support vector solution should be stored.")); 65 Parameters.Add(new LookupParameter<SupportVectorRegressionSolution>(BestSolutionParameterName, "The best support vector regression solution.")); 95 66 } 96 67 97 public override IOperation Apply() { 98 ItemArray<DoubleValue> qualities = QualityParameter.ActualValue; 99 ResultCollection results = ResultsParameter.ActualValue; 100 ItemArray<SupportVectorMachineModel> models = SupportVectorRegressionModelParameter.ActualValue; 101 DataAnalysisProblemData problemData = ProblemDataParameter.ActualValue; 102 DoubleValue upperEstimationLimit = UpperEstimationLimitParameter.ActualValue; 103 DoubleValue lowerEstimationLimit = LowerEstimationLimitParameter.ActualValue; 104 var inputVariables = ProblemDataParameter.ActualValue.InputVariables.Select(x => x.Value); 68 protected override DataAnalysisSolution UpdateBestSolution() { 69 double upperEstimationLimit = UpperEstimationLimit != null ? UpperEstimationLimit.Value : double.PositiveInfinity; 70 double lowerEstimationLimit = LowerEstimationLimit != null ? LowerEstimationLimit.Value : double.NegativeInfinity; 105 71 106 int i = qualities.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index;72 int i = Quality.Select((x, index) => new { index, x.Value }).OrderBy(x => x.Value).First().index; 107 73 108 SupportVectorRegressionSolution solution = BestSolutionParameter.ActualValue; 109 if (solution == null) { 110 solution = new SupportVectorRegressionSolution(problemData, models[i], inputVariables, lowerEstimationLimit.Value, upperEstimationLimit.Value); 74 if (BestSolutionQualityParameter.ActualValue == null || BestSolutionQualityParameter.ActualValue.Value > Quality[i].Value) { 75 IEnumerable<string> inputVariables = from var in ProblemData.InputVariables 76 where ProblemData.InputVariables.ItemChecked(var) 77 select var.Value; 78 var solution = new SupportVectorRegressionSolution(ProblemData, SupportVectorMachineModel[i], inputVariables, lowerEstimationLimit, upperEstimationLimit); 79 111 80 BestSolutionParameter.ActualValue = solution; 112 BestSolutionQualityParameter.ActualValue = qualities[i]; 113 results.Add(new Result(BestSolutionResultName, solution)); 114 results.Add(new Result(BestSolutionInputvariableCountResultName, new IntValue(inputVariables.Count()))); 115 #region calculate R2,MSE,Rel Error 116 double[] trainingValues = problemData.Dataset.GetVariableValues( 117 problemData.TargetVariable.Value, 118 problemData.TrainingSamplesStart.Value, 119 problemData.TrainingSamplesEnd.Value); 120 double[] testValues = problemData.Dataset.GetVariableValues( 121 problemData.TargetVariable.Value, 122 problemData.TestSamplesStart.Value, 123 problemData.TestSamplesEnd.Value); 124 double trainingR2 = SimpleRSquaredEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues); 125 double testR2 = SimpleRSquaredEvaluator.Calculate(testValues, solution.EstimatedTestValues); 126 double trainingMse = SimpleMSEEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues); 127 double testMse = SimpleMSEEvaluator.Calculate(testValues, solution.EstimatedTestValues); 128 double trainingRelError = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues); 129 double testRelError = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(testValues, solution.EstimatedTestValues); 130 results.Add(new Result(BestSolutionTrainingRSquared, new DoubleValue(trainingR2))); 131 results.Add(new Result(BestSolutionTestRSquared, new DoubleValue(testR2))); 132 results.Add(new Result(BestSolutionTrainingMse, new DoubleValue(trainingMse))); 133 results.Add(new Result(BestSolutionTestMse, new DoubleValue(testMse))); 134 results.Add(new Result(BestSolutionTrainingRelativeError, new DoubleValue(trainingRelError))); 135 results.Add(new Result(BestSolutionTestRelativeError, new DoubleValue(testRelError))); 136 #endregion 137 } else { 138 if (BestSolutionQualityParameter.ActualValue.Value > qualities[i].Value) { 139 solution = new SupportVectorRegressionSolution(problemData, models[i], inputVariables, lowerEstimationLimit.Value, upperEstimationLimit.Value); 140 BestSolutionParameter.ActualValue = solution; 141 BestSolutionQualityParameter.ActualValue = qualities[i]; 142 results[BestSolutionResultName].Value = solution; 143 results[BestSolutionInputvariableCountResultName].Value = new IntValue(inputVariables.Count()); 144 #region update R2,MSE, Rel Error 145 double[] trainingValues = problemData.Dataset.GetVariableValues( 146 problemData.TargetVariable.Value, 147 problemData.TrainingSamplesStart.Value, 148 problemData.TrainingSamplesEnd.Value); 149 double[] testValues = problemData.Dataset.GetVariableValues( 150 problemData.TargetVariable.Value, 151 problemData.TestSamplesStart.Value, 152 problemData.TestSamplesEnd.Value); 153 double trainingR2 = SimpleRSquaredEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues); 154 double testR2 = SimpleRSquaredEvaluator.Calculate(testValues, solution.EstimatedTestValues); 155 double trainingMse = SimpleMSEEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues); 156 double testMse = SimpleMSEEvaluator.Calculate(testValues, solution.EstimatedTestValues); 157 double trainingRelError = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(trainingValues, solution.EstimatedTrainingValues); 158 double testRelError = SimpleMeanAbsolutePercentageErrorEvaluator.Calculate(testValues, solution.EstimatedTestValues); 159 results[BestSolutionTrainingRSquared].Value = new DoubleValue(trainingR2); 160 results[BestSolutionTestRSquared].Value = new DoubleValue(testR2); 161 results[BestSolutionTrainingMse].Value = new DoubleValue(trainingMse); 162 results[BestSolutionTestMse].Value = new DoubleValue(testMse); 163 results[BestSolutionTrainingRelativeError].Value = new DoubleValue(trainingRelError); 164 results[BestSolutionTestRelativeError].Value = new DoubleValue(testRelError); 165 #endregion 81 BestSolutionQualityParameter.ActualValue = Quality[i]; 82 83 if (Results.ContainsKey(BestSolutionInputvariableCountResultName)) { 84 Results[BestSolutionInputvariableCountResultName].Value = new IntValue(inputVariables.Count()); 85 } else { 86 Results.Add(new Result(BestSolutionInputvariableCountResultName, new IntValue(inputVariables.Count()))); 166 87 } 167 88 } 168 169 return base.Apply(); 89 return BestSolutionParameter.ActualValue; 170 90 } 171 91 }
Note: See TracChangeset
for help on using the changeset viewer.