Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
10/29/20 15:24:40 (3 years ago)
Author:
gkronber
Message:

#3022: made a few changes while reviewing the code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/3022-FastFunctionExtraction/FFX/BFUtils.cs

    r17740 r17779  
    1414        public static IEnumerable<IBasisFunction> CreateBasisFunctions(IRegressionProblemData data, Approach approach) {
    1515            var exponents = approach.AllowExp ? approach.Exponents : new HashSet<double> { 1 };
    16             var funcs = approach.AllowNonlinFuncs ? approach.NonlinFuncs : new HashSet<NonlinearOperator> { NonlinearOperator.None };
     16            var funcs = approach.AllowNonLinearFunctions ? approach.NonLinearFunctions : new HashSet<NonlinearOperator> { NonlinearOperator.None };
    1717            var simpleBasisFuncs = CreateSimpleBases(data, exponents, funcs);
    1818
     
    2020                // only allow hinge functions for features with exponent = 1 (deemed too complex otherwise)
    2121                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));
    2323            }
    2424
    2525            IEnumerable<IBasisFunction> functions = simpleBasisFuncs;
    2626
    27             if (approach.AllowInter) {
     27            if (approach.AllowInteractions) {
    2828                var multivariateBases = CreateMultivariateBases(data, simpleBasisFuncs.ToArray());
    2929                functions = functions.Concat(multivariateBases);
    3030            }
    3131
    32             if (approach.AllowDenom) {
     32            if (approach.AllowDenominators) {
    3333                var denominatorBases = CreateDenominatorBases(functions);
    3434                functions = functions.Concat(denominatorBases);
     
    3737        }
    3838
    39         public static IEnumerable<ISimpleBasisFunction> CreateSimpleBases(IRegressionProblemData problemData, HashSet<double> exponents, HashSet<NonlinearOperator> nonlinFuncs) {
     39        public static IEnumerable<ISimpleBasisFunction> CreateSimpleBases(IRegressionProblemData problemData, HashSet<double> exponents, HashSet<NonlinearOperator> nonLinearFunctions) {
    4040            var simpleBasisFunctions = new List<ISimpleBasisFunction>();
    4141            foreach (var variableName in problemData.AllowedInputVariables) {
     
    4545                    var simpleBase = new SimpleBasisFunction(variableName, exp, NonlinearOperator.None);
    4646                    // 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 nonlinFuncs) {
     47                    if (!Ok(simpleBase.Evaluate(problemData))) continue;
     48
     49                    foreach (NonlinearOperator op in nonLinearFunctions) {
    5050                        // ignore cases where op has no effect
    51                         if (op.Equals(NonlinearOperator.Abs) && new[] { -2.0, 2.0 }.Contains(exp) && nonlinFuncs.Contains(NonlinearOperator.None)) continue;
     51                        if (op.Equals(NonlinearOperator.Abs) && new[] { -2.0, 2.0 }.Contains(exp) && nonLinearFunctions.Contains(NonlinearOperator.None)) continue;
    5252                        if (op.Equals(NonlinearOperator.Abs) && min >= 0) continue;
    5353                        var nonsimpleBase = (SimpleBasisFunction)simpleBase.DeepCopy();
    5454                        nonsimpleBase.Operator = op;
    55                         if (!Ok(nonsimpleBase.Simulate(problemData))) continue;
     55                        if (!Ok(nonsimpleBase.Evaluate(problemData))) continue;
    5656                        simpleBasisFunctions.Add(nonsimpleBase);
    5757                    }
     
    7777                    if (b_j.Operator != NonlinearOperator.None) continue; // disallow op() * op(); deemed to complex
    7878                    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;
    8080                    multivariateBases.Add(b_inter);
    8181                    if (multivariateBases.Count() >= maxSize)
     
    9898        }
    9999
    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) {
    101102            var hingeBases = new List<ISimpleBasisFunction>();
    102103
     
    107108        }
    108109
    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) {
    110112            if (relative_start_thr >= relative_end_thr) throw new ArgumentException($"{nameof(relative_start_thr)} must be smaller than {nameof(relative_end_thr)}.");
    111113            var ans = new List<ISimpleBasisFunction>();
    112114
    113             var vals = simple_bf.Simulate(data);
     115            var vals = simple_bf.Evaluate(data);
    114116            var temp = trainingPartition ?? data.TrainingPartition;
    115117            double min = Double.MaxValue;
     
    126128
    127129            foreach (var thr in thresholds) {
    128                 ans.Add(new SimpleBasisFunction(simple_bf.Feature, 1, NonlinearOperator.Gth, true, thr));
    129                 ans.Add(new SimpleBasisFunction(simple_bf.Feature, 1, NonlinearOperator.Lth, 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));
    130132            }
    131133            return ans;
     
    135137            List<IBasisFunction> ans = new List<IBasisFunction>();
    136138            foreach (var bf in basisFunctions) {
    137                 if (!bf.IsNominator) continue;
     139                if (!bf.IsDenominator) continue;
    138140                var denomFunc = bf.DeepCopy();
    139                 denomFunc.IsNominator = false;
     141                denomFunc.IsDenominator = false;
    140142                ans.Add(denomFunc);
    141143            }
     
    151153            int col = 0;
    152154            foreach (var basisFunc in basisFunctions) {
    153                 allowedInputVars.Add(basisFunc.ToString() + (!basisFunc.IsNominator ? " * " + problemData.TargetVariable : ""));
    154                 var vals = basisFunc.Simulate(problemData);
     155                allowedInputVars.Add(basisFunc.ToString() + (!basisFunc.IsDenominator ? " * " + problemData.TargetVariable : ""));
     156                var vals = basisFunc.Evaluate(problemData);
    155157                for (int i = 0; i < numRows; i++) {
    156158                    variableValues[i, col] = vals[i];
Note: See TracChangeset for help on using the changeset viewer.