Changeset 14762 for branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression
- Timestamp:
- 03/18/17 12:47:30 (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/symbreg-factors-2650/HeuristicLab.Problems.DataAnalysis/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs
r14498 r14762 42 42 Noise 43 43 } 44 44 public enum FactorReplacementMethodEnum { 45 Best, 46 Mode, 47 Shuffle 48 } 45 49 public enum DataPartitionEnum { 46 50 Training, … … 88 92 } 89 93 90 public static IEnumerable<Tuple<string, double>> CalculateImpacts(IRegressionSolution solution, 94 public static IEnumerable<Tuple<string, double>> CalculateImpacts( 95 IRegressionSolution solution, 91 96 DataPartitionEnum data = DataPartitionEnum.Training, 92 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median) { 97 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Median, 98 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) { 93 99 94 100 var problemData = solution.ProblemData; … … 101 107 OnlineCalculatorError error; 102 108 103 switch 109 switch(data) { 104 110 case DataPartitionEnum.All: 105 111 rows = solution.ProblemData.AllIndices; 106 112 targetValues = problemData.TargetVariableValues.ToList(); 107 113 originalR2 = OnlinePearsonsRCalculator.Calculate(problemData.TargetVariableValues, solution.EstimatedValues, out error); 108 if 114 if(error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during R² calculation."); 109 115 originalR2 = originalR2 * originalR2; 110 116 break; … … 129 135 130 136 // calculate impacts for double variables 131 foreach 137 foreach(var inputVariable in allowedInputVariables.Where(problemData.Dataset.VariableHasType<double>)) { 132 138 var newEstimates = EvaluateModelWithReplacedVariable(solution.Model, inputVariable, modifiableDataset, rows, replacementMethod); 133 139 var newR2 = OnlinePearsonsRCalculator.Calculate(targetValues, newEstimates, out error); 134 if 140 if(error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during R² calculation with replaced inputs."); 135 141 136 142 newR2 = newR2 * newR2; … … 138 144 impacts[inputVariable] = impact; 139 145 } 140 // calculate impacts for factor variables 141 foreach (var inputVariable in allowedInputVariables.Where(problemData.Dataset.VariableHasType<string>)) { 142 var smallestImpact = double.PositiveInfinity; 143 foreach (var repl in problemData.Dataset.GetStringValues(inputVariable, rows).Distinct()) { 144 var newEstimates = EvaluateModelWithReplacedVariable(solution.Model, inputVariable, modifiableDataset, rows, Enumerable.Repeat(repl, dataset.Rows)); 146 147 // calculate impacts for string variables 148 foreach(var inputVariable in allowedInputVariables.Where(problemData.Dataset.VariableHasType<string>)) { 149 if(factorReplacementMethod == FactorReplacementMethodEnum.Best) { 150 // try replacing with all possible values and find the best replacement value 151 var smallestImpact = double.PositiveInfinity; 152 foreach(var repl in problemData.Dataset.GetStringValues(inputVariable, rows).Distinct()) { 153 var newEstimates = EvaluateModelWithReplacedVariable(solution.Model, inputVariable, modifiableDataset, rows, 154 Enumerable.Repeat(repl, dataset.Rows)); 155 var newR2 = OnlinePearsonsRCalculator.Calculate(targetValues, newEstimates, out error); 156 if(error != OnlineCalculatorError.None) 157 throw new InvalidOperationException("Error during R² calculation with replaced inputs."); 158 159 newR2 = newR2 * newR2; 160 var impact = originalR2 - newR2; 161 if(impact < smallestImpact) smallestImpact = impact; 162 } 163 impacts[inputVariable] = smallestImpact; 164 } else { 165 // for replacement methods shuffle and mode 166 // calculate impacts for factor variables 167 168 var newEstimates = EvaluateModelWithReplacedVariable(solution.Model, inputVariable, modifiableDataset, rows, 169 factorReplacementMethod); 145 170 var newR2 = OnlinePearsonsRCalculator.Calculate(targetValues, newEstimates, out error); 146 if (error != OnlineCalculatorError.None) throw new InvalidOperationException("Error during R² calculation with replaced inputs."); 171 if(error != OnlineCalculatorError.None) 172 throw new InvalidOperationException("Error during R² calculation with replaced inputs."); 147 173 148 174 newR2 = newR2 * newR2; 149 175 var impact = originalR2 - newR2; 150 i f (impact < smallestImpact) smallestImpact= impact;176 impacts[inputVariable] = impact; 151 177 } 152 impacts[inputVariable] = smallestImpact; 153 } 178 } // foreach 154 179 return impacts.OrderByDescending(i => i.Value).Select(i => Tuple.Create(i.Key, i.Value)); 155 180 } … … 161 186 IRandom rand; 162 187 163 switch 188 switch(replacement) { 164 189 case ReplacementMethodEnum.Median: 165 190 replacementValue = rows.Select(r => originalValues[r]).Median(); … … 179 204 int i = 0; 180 205 // update column values 181 foreach 206 foreach(var r in rows) { 182 207 replacementValues[r] = shuffledValues[i++]; 183 208 } … … 190 215 replacementValues = Enumerable.Repeat(double.NaN, dataset.Rows).ToList(); 191 216 // update column values 192 foreach 217 foreach(var r in rows) { 193 218 replacementValues[r] = NormalDistributedRandom.NextDouble(rand, avg, stdDev); 194 219 } … … 202 227 } 203 228 204 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, 229 private static IEnumerable<double> EvaluateModelWithReplacedVariable( 230 IRegressionModel model, string variable, ModifiableDataset dataset, 231 IEnumerable<int> rows, 232 FactorReplacementMethodEnum replacement = FactorReplacementMethodEnum.Shuffle) { 233 var originalValues = dataset.GetReadOnlyStringValues(variable).ToList(); 234 List<string> replacementValues; 235 IRandom rand; 236 237 switch(replacement) { 238 case FactorReplacementMethodEnum.Mode: 239 var mostCommonValue = rows.Select(r => originalValues[r]) 240 .GroupBy(v => v) 241 .OrderByDescending(g => g.Count()) 242 .First().Key; 243 replacementValues = Enumerable.Repeat(mostCommonValue, dataset.Rows).ToList(); 244 break; 245 case FactorReplacementMethodEnum.Shuffle: 246 // new var has same empirical distribution but the relation to y is broken 247 rand = new FastRandom(31415); 248 // prepare a complete column for the dataset 249 replacementValues = Enumerable.Repeat(string.Empty, dataset.Rows).ToList(); 250 // shuffle only the selected rows 251 var shuffledValues = rows.Select(r => originalValues[r]).Shuffle(rand).ToList(); 252 int i = 0; 253 // update column values 254 foreach(var r in rows) { 255 replacementValues[r] = shuffledValues[i++]; 256 } 257 break; 258 default: 259 throw new ArgumentException(string.Format("FactorReplacementMethod {0} cannot be handled.", replacement)); 260 } 261 262 return EvaluateModelWithReplacedVariable(model, variable, dataset, rows, replacementValues); 263 } 264 265 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, 205 266 ModifiableDataset dataset, IEnumerable<int> rows, IEnumerable<double> replacementValues) { 206 267 var originalValues = dataset.GetReadOnlyDoubleValues(variable).ToList(); … … 212 273 return estimates; 213 274 } 214 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, 275 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, 215 276 ModifiableDataset dataset, IEnumerable<int> rows, IEnumerable<string> replacementValues) { 216 277 var originalValues = dataset.GetReadOnlyStringValues(variable).ToList();
Note: See TracChangeset
for help on using the changeset viewer.