Changeset 17779 for branches/3022-FastFunctionExtraction/FFX/BFUtils.cs
- Timestamp:
- 10/29/20 15:24:40 (3 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3022-FastFunctionExtraction/FFX/BFUtils.cs
r17740 r17779 14 14 public static IEnumerable<IBasisFunction> CreateBasisFunctions(IRegressionProblemData data, Approach approach) { 15 15 var exponents = approach.AllowExp ? approach.Exponents : new HashSet<double> { 1 }; 16 var funcs = approach.AllowNon linFuncs ? approach.NonlinFuncs : new HashSet<NonlinearOperator> { NonlinearOperator.None };16 var funcs = approach.AllowNonLinearFunctions ? approach.NonLinearFunctions : new HashSet<NonlinearOperator> { NonlinearOperator.None }; 17 17 var simpleBasisFuncs = CreateSimpleBases(data, exponents, funcs); 18 18 … … 20 20 // only allow hinge functions for features with exponent = 1 (deemed too complex otherwise) 21 21 var linearSimpleBasisFuncs = simpleBasisFuncs.Where(simpleBf => simpleBf.Exponent == 1 && simpleBf.Operator.Equals(NonlinearOperator.None)); 22 simpleBasisFuncs = simpleBasisFuncs.Concat(CreateHingeBases(data, linearSimpleBasisFuncs, approach.MinHingeThr , approach.MaxHingeThr, approach.NumHingeThrs));22 simpleBasisFuncs = simpleBasisFuncs.Concat(CreateHingeBases(data, linearSimpleBasisFuncs, approach.MinHingeThreshold, approach.MaxHingeThreshold, approach.NumHingeThresholds)); 23 23 } 24 24 25 25 IEnumerable<IBasisFunction> functions = simpleBasisFuncs; 26 26 27 if (approach.AllowInter ) {27 if (approach.AllowInteractions) { 28 28 var multivariateBases = CreateMultivariateBases(data, simpleBasisFuncs.ToArray()); 29 29 functions = functions.Concat(multivariateBases); 30 30 } 31 31 32 if (approach.AllowDenom ) {32 if (approach.AllowDenominators) { 33 33 var denominatorBases = CreateDenominatorBases(functions); 34 34 functions = functions.Concat(denominatorBases); … … 37 37 } 38 38 39 public static IEnumerable<ISimpleBasisFunction> CreateSimpleBases(IRegressionProblemData problemData, HashSet<double> exponents, HashSet<NonlinearOperator> non linFuncs) {39 public static IEnumerable<ISimpleBasisFunction> CreateSimpleBases(IRegressionProblemData problemData, HashSet<double> exponents, HashSet<NonlinearOperator> nonLinearFunctions) { 40 40 var simpleBasisFunctions = new List<ISimpleBasisFunction>(); 41 41 foreach (var variableName in problemData.AllowedInputVariables) { … … 45 45 var simpleBase = new SimpleBasisFunction(variableName, exp, NonlinearOperator.None); 46 46 // if the basis function is not valid without any operator, then it won't be valid in combination with any nonlinear operator -> skip 47 if (!Ok(simpleBase. Simulate(problemData))) continue;48 49 foreach (NonlinearOperator op in non linFuncs) {47 if (!Ok(simpleBase.Evaluate(problemData))) continue; 48 49 foreach (NonlinearOperator op in nonLinearFunctions) { 50 50 // ignore cases where op has no effect 51 if (op.Equals(NonlinearOperator.Abs) && new[] { -2.0, 2.0 }.Contains(exp) && non linFuncs.Contains(NonlinearOperator.None)) continue;51 if (op.Equals(NonlinearOperator.Abs) && new[] { -2.0, 2.0 }.Contains(exp) && nonLinearFunctions.Contains(NonlinearOperator.None)) continue; 52 52 if (op.Equals(NonlinearOperator.Abs) && min >= 0) continue; 53 53 var nonsimpleBase = (SimpleBasisFunction)simpleBase.DeepCopy(); 54 54 nonsimpleBase.Operator = op; 55 if (!Ok(nonsimpleBase. Simulate(problemData))) continue;55 if (!Ok(nonsimpleBase.Evaluate(problemData))) continue; 56 56 simpleBasisFunctions.Add(nonsimpleBase); 57 57 } … … 77 77 if (b_j.Operator != NonlinearOperator.None) continue; // disallow op() * op(); deemed to complex 78 78 var b_inter = new ProductBaseFunction(b_i, b_j, true); 79 if (!Ok(b_inter. Simulate(data))) continue;79 if (!Ok(b_inter.Evaluate(data))) continue; 80 80 multivariateBases.Add(b_inter); 81 81 if (multivariateBases.Count() >= maxSize) … … 98 98 } 99 99 100 public static IList<ISimpleBasisFunction> CreateHingeBases(IRegressionProblemData data, IEnumerable<ISimpleBasisFunction> simple_bfs, double relative_start_thr = 0.0, double relative_end_thr = 1.0, int num_thrs = 3, IntRange trainingPartition = null) { 100 public static IList<ISimpleBasisFunction> CreateHingeBases(IRegressionProblemData data, IEnumerable<ISimpleBasisFunction> simple_bfs, 101 double relative_start_thr = 0.0, double relative_end_thr = 1.0, int num_thrs = 3, IntRange trainingPartition = null) { 101 102 var hingeBases = new List<ISimpleBasisFunction>(); 102 103 … … 107 108 } 108 109 109 private static IEnumerable<ISimpleBasisFunction> CreateHingeBases(IRegressionProblemData data, ISimpleBasisFunction simple_bf, double relative_start_thr, double relative_end_thr, int num_thrs, IntRange trainingPartition) { 110 private static IEnumerable<ISimpleBasisFunction> CreateHingeBases(IRegressionProblemData data, ISimpleBasisFunction simple_bf, 111 double relative_start_thr, double relative_end_thr, int num_thrs, IntRange trainingPartition) { 110 112 if (relative_start_thr >= relative_end_thr) throw new ArgumentException($"{nameof(relative_start_thr)} must be smaller than {nameof(relative_end_thr)}."); 111 113 var ans = new List<ISimpleBasisFunction>(); 112 114 113 var vals = simple_bf. Simulate(data);115 var vals = simple_bf.Evaluate(data); 114 116 var temp = trainingPartition ?? data.TrainingPartition; 115 117 double min = Double.MaxValue; … … 126 128 127 129 foreach (var thr in thresholds) { 128 ans.Add(new SimpleBasisFunction(simple_bf.Feature, 1, NonlinearOperator.G th, true, thr));129 ans.Add(new SimpleBasisFunction(simple_bf.Feature, 1, NonlinearOperator.L th, true, thr));130 ans.Add(new SimpleBasisFunction(simple_bf.Feature, 1, NonlinearOperator.GT_Hinge, true, thr)); 131 ans.Add(new SimpleBasisFunction(simple_bf.Feature, 1, NonlinearOperator.LT_Hinge, true, thr)); 130 132 } 131 133 return ans; … … 135 137 List<IBasisFunction> ans = new List<IBasisFunction>(); 136 138 foreach (var bf in basisFunctions) { 137 if (!bf.Is Nominator) continue;139 if (!bf.IsDenominator) continue; 138 140 var denomFunc = bf.DeepCopy(); 139 denomFunc.Is Nominator = false;141 denomFunc.IsDenominator = false; 140 142 ans.Add(denomFunc); 141 143 } … … 151 153 int col = 0; 152 154 foreach (var basisFunc in basisFunctions) { 153 allowedInputVars.Add(basisFunc.ToString() + (!basisFunc.Is Nominator ? " * " + problemData.TargetVariable : ""));154 var vals = basisFunc. Simulate(problemData);155 allowedInputVars.Add(basisFunc.ToString() + (!basisFunc.IsDenominator ? " * " + problemData.TargetVariable : "")); 156 var vals = basisFunc.Evaluate(problemData); 155 157 for (int i = 0; i < numRows; i++) { 156 158 variableValues[i, col] = vals[i];
Note: See TracChangeset
for help on using the changeset viewer.