- Timestamp:
- 07/26/18 13:00:41 (6 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/2904_CalculateImpacts/3.4/Implementation/Regression/RegressionSolutionVariableImpactsCalculator.cs
r16018 r16020 37 37 [Item("RegressionSolution Impacts Calculator", "Calculation of the impacts of input variables for any regression solution")] 38 38 public sealed class RegressionSolutionVariableImpactsCalculator : ParameterizedNamedItem { 39 private static IOnlineCalculator calculator = new OnlinePearsonsRSquaredCalculator();40 41 39 public enum ReplacementMethodEnum { 42 40 Median, … … 112 110 public static IEnumerable<Tuple<string, double>> CalculateImpacts( 113 111 IRegressionSolution solution, 114 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum. Median,112 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle, 115 113 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, 116 114 DataPartitionEnum data = DataPartitionEnum.Training, … … 123 121 IRegressionProblemData problemData, 124 122 IEnumerable<double> estimatedValues, 125 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum. Median,123 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle, 126 124 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, 127 125 DataPartitionEnum data = DataPartitionEnum.Training, … … 151 149 IEnumerable<double> estimatedValues, 152 150 IEnumerable<int> rows, 153 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum. Median,151 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle, 154 152 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, 155 153 Func<double, string, bool> progressCallback = null) { 156 154 157 155 IEnumerable<double> targetValues; 158 double original Value = -1;159 160 PrepareData(rows, problemData, estimatedValues, out targetValues, out original Value);156 double originalCalculatorValue = -1; 157 158 PrepareData(rows, problemData, estimatedValues, out targetValues, out originalCalculatorValue); 161 159 162 160 var impacts = new Dictionary<string, double>(); … … 165 163 166 164 int curIdx = 0; 167 int count = allowedInputVariables.Count( );165 int count = allowedInputVariables.Count(v => problemData.Dataset.VariableHasType<double>(v) || problemData.Dataset.VariableHasType<string>(v)); 168 166 169 167 foreach (var inputVariable in allowedInputVariables) { … … 173 171 if (progressCallback((double)curIdx / count, string.Format("Calculating impact for variable {0} ({1} of {2})", inputVariable, curIdx, count))) { return null; } 174 172 } 175 impacts[inputVariable] = CalculateImpact(inputVariable, model, problemData.Dataset, rows, targetValues, original Value, replacementMethod, factorReplacementMethod);173 impacts[inputVariable] = CalculateImpact(inputVariable, model, problemData.Dataset, rows, targetValues, originalCalculatorValue, replacementMethod, factorReplacementMethod); 176 174 } 177 175 … … 184 182 IEnumerable<double> targetValues, 185 183 double originalValue, 186 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum. Median,184 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle, 187 185 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best, 188 186 DataPartitionEnum data = DataPartitionEnum.Training) { … … 196 194 IEnumerable<double> targetValues, 197 195 double originalValue, 198 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum. Median,196 ReplacementMethodEnum replacementMethod = ReplacementMethodEnum.Shuffle, 199 197 FactorReplacementMethodEnum factorReplacementMethod = FactorReplacementMethodEnum.Best) { 200 198 … … 276 274 } 277 275 278 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, ModifiableDataset dataset, IEnumerable<int> rows, ReplacementMethodEnum replacement = ReplacementMethodEnum. Median) {276 private static IEnumerable<double> EvaluateModelWithReplacedVariable(IRegressionModel model, string variable, ModifiableDataset dataset, IEnumerable<int> rows, ReplacementMethodEnum replacement = ReplacementMethodEnum.Shuffle) { 279 277 var originalValues = dataset.GetReadOnlyDoubleValues(variable).ToList(); 280 278 double replacementValue; … … 369 367 } 370 368 371 private static double CalculateValue(IEnumerable<double> targets, IEnumerable<double> estimates, out OnlineCalculatorError error) { 372 calculator.Reset(); 373 374 var targetsEnumerator = targets.GetEnumerator(); 375 var estimatesEnumerator = estimates.GetEnumerator(); 376 377 bool targetsHasNextValue = targetsEnumerator.MoveNext(); 378 bool estimatesHasNextValue = estimatesEnumerator.MoveNext(); 379 380 while (targetsHasNextValue && estimatesHasNextValue) { 381 calculator.Add(targetsEnumerator.Current, estimatesEnumerator.Current); 382 targetsHasNextValue = targetsEnumerator.MoveNext(); 383 estimatesHasNextValue = estimatesEnumerator.MoveNext(); 384 } 385 386 //Check if there is an equal quantity of targets and estimates 387 if (targetsHasNextValue != estimatesHasNextValue) { 388 throw new ArgumentException(string.Format("Targets and Estimates must be of equal length ({0},{1})", targets.Count(), estimates.Count())); 389 390 } 391 392 error = calculator.ErrorState; 393 return calculator.Value; 369 private static double CalculateValue(IEnumerable<double> originalValues, IEnumerable<double> estimatedValues, out OnlineCalculatorError errorState) { 370 IEnumerator<double> firstEnumerator = originalValues.GetEnumerator(); 371 IEnumerator<double> secondEnumerator = estimatedValues.GetEnumerator(); 372 var calculator = new OnlinePearsonsRSquaredCalculator(); 373 374 // always move forward both enumerators (do not use short-circuit evaluation!) 375 while (firstEnumerator.MoveNext() & secondEnumerator.MoveNext()) { 376 double original = firstEnumerator.Current; 377 double estimated = secondEnumerator.Current; 378 calculator.Add(original, estimated); 379 if (calculator.ErrorState != OnlineCalculatorError.None) break; 380 } 381 382 // check if both enumerators are at the end to make sure both enumerations have the same length 383 if (calculator.ErrorState == OnlineCalculatorError.None && 384 (secondEnumerator.MoveNext() || firstEnumerator.MoveNext())) { 385 throw new ArgumentException("Number of elements in first and second enumeration doesn't match."); 386 } else { 387 errorState = calculator.ErrorState; 388 return calculator.Value; 389 } 394 390 } 395 391 }
Note: See TracChangeset
for help on using the changeset viewer.