Changeset 17779 for branches/3022-FastFunctionExtraction/FFX
- Timestamp:
- 10/29/20 15:24:40 (4 years ago)
- Location:
- branches/3022-FastFunctionExtraction/FFX
- Files:
-
- 10 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/3022-FastFunctionExtraction/FFX/Approach.cs
r17737 r17779 4 4 namespace HeuristicLab.Algorithms.DataAnalysis.FastFunctionExtraction { 5 5 internal struct Approach { 6 public bool AllowInter { get; set; }7 public bool AllowDenom { get; set; }6 public bool AllowInteractions { get; set; } 7 public bool AllowDenominators { get; set; } 8 8 public bool AllowExp { get; set; } 9 public bool AllowNon linFuncs { get; set; }9 public bool AllowNonLinearFunctions { get; set; } 10 10 public bool AllowHinge { get; set; } 11 11 public HashSet<double> Exponents { get; set; } 12 public HashSet<NonlinearOperator> Non linFuncs { get; set; }13 public double MinHingeThr { get; set; }14 public double MaxHingeThr { get; set; }15 public int NumHingeThr s { get; set; }16 public double El netPenalty { get; set; }12 public HashSet<NonlinearOperator> NonLinearFunctions { get; set; } 13 public double MinHingeThreshold { get; set; } 14 public double MaxHingeThreshold { get; set; } 15 public int NumHingeThresholds { get; set; } 16 public double ElasticNetPenalty { get; set; } 17 17 public int MaxNumBases { get; set; } 18 18 19 private static int Int(bool b) => b ? 1 : 0;20 21 19 public override string ToString() { 22 return $"Inter {Int(AllowInter)} Denom{Int(AllowDenom)} Exp{Int((AllowExp))} Nonlin{Int(AllowNonlinFuncs)} Hinge{Int(AllowHinge)}";20 return $"Interactions{AllowInteractions} Denominator{AllowDenominators} Exponential{AllowExp} NonLinearFunctions{AllowNonLinearFunctions} HingeFunctions{AllowHinge}"; 23 21 } 24 22 25 public Approach(bool inter, bool denom, bool exp, bool nonlin, bool hinge, HashSet<double> exponents, HashSet<NonlinearOperator> nonlinFuncs, int maxNumBases, double penalty, double minHingeThr, double maxHingeThr, int numHingeThrs) {26 this.AllowInter = inter;27 this.AllowDenom = denom;28 this.AllowExp = exp;29 this.AllowNon linFuncs = nonlin;30 this.AllowHinge = hinge;23 public Approach(bool allowInteractions, bool allowDenominators, bool allowExp, bool allowNonLinearFunctions, bool allowHingeFunctions, HashSet<double> exponents, HashSet<NonlinearOperator> nonLinearFunctions, int maxNumBases, double elasticNetPenality, double minHingeThreshold, double maxHingeThreshold, int numHingeThresholds) { 24 this.AllowInteractions = allowInteractions; 25 this.AllowDenominators = allowDenominators; 26 this.AllowExp = allowExp; 27 this.AllowNonLinearFunctions = allowNonLinearFunctions; 28 this.AllowHinge = allowHingeFunctions; 31 29 if (AllowExp && exponents == null) throw new ArgumentNullException(nameof(exponents)); 32 30 Exponents = exponents; 33 if (AllowNon linFuncs && nonlinFuncs == null) throw new ArgumentNullException(nameof(nonlinFuncs));34 Non linFuncs = nonlinFuncs;35 MinHingeThr = minHingeThr;36 MaxHingeThr = maxHingeThr;37 NumHingeThr s = numHingeThrs;38 El netPenalty = penalty;31 if (AllowNonLinearFunctions && nonLinearFunctions == null) throw new ArgumentNullException(nameof(nonLinearFunctions)); 32 NonLinearFunctions = nonLinearFunctions; 33 MinHingeThreshold = minHingeThreshold; 34 MaxHingeThreshold = maxHingeThreshold; 35 NumHingeThresholds = numHingeThresholds; 36 ElasticNetPenalty = elasticNetPenality; 39 37 MaxNumBases = maxNumBases; 40 38 } -
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]; -
branches/3022-FastFunctionExtraction/FFX/FFXModel.cs
r17737 r17779 18 18 public IEnumerable<(double coeff, IBasisFunction function)> BasisFunctions { get; set; } 19 19 public IEnumerable<(double coeff, IBasisFunction function)> NominatorFunctions => 20 BasisFunctions.Where(bf => bf.function.Is Nominator);20 BasisFunctions.Where(bf => bf.function.IsDenominator); 21 21 public IEnumerable<(double coeff, IBasisFunction function)> DenominatorFunctions => 22 BasisFunctions.Where(bf => !bf.function.Is Nominator);22 BasisFunctions.Where(bf => !bf.function.IsDenominator); 23 23 24 24 public int NumNumeratorFunctions => NominatorFunctions != null ? NominatorFunctions.Count() : 0; -
branches/3022-FastFunctionExtraction/FFX/FastFunctionExtraction.cs
r17762 r17779 12 12 using System.Collections.Generic; 13 13 using HeuristicLab.Problems.DataAnalysis.Symbolic.Regression; 14 using HeuristicLab.Problems.DataAnalysis.Symbolic;14 using System.Collections; 15 15 16 16 namespace HeuristicLab.Algorithms.DataAnalysis.FastFunctionExtraction { 17 17 18 [Item(Name = "FastFunctionExtraction", Description = " An FFX algorithm.")]18 [Item(Name = "FastFunctionExtraction", Description = "Implementation of the Fast Function Extraction (FFX) algorithm in C#.")] 19 19 [Creatable(Category = CreatableAttribute.Categories.Algorithms, Priority = 999)] 20 [StorableType("689280F7-E371-44A2-98A5-FCEDF22CA343")] // for persistence (storing your algorithm to a files or transfer to HeuristicLab.Hive20 [StorableType("689280F7-E371-44A2-98A5-FCEDF22CA343")] 21 21 public sealed class FastFunctionExtraction : FixedDataAnalysisAlgorithm<IRegressionProblem> { 22 22 … … 156 156 .SelectMany(approach => CreateFFXModels(data, approach)).ToList(); 157 157 158 // Final pareto filter over all generated models from all different approaches158 // Final Pareto filter over all generated models from all different approaches 159 159 var nondominatedFFXModels = NondominatedModels(data, allFFXModels); 160 160 … … 199 199 var normalizedElnetData = BFUtils.Normalize(elnetData, out var X_avgs, out var X_stds, out var y_avg, out var y_std); 200 200 201 ElasticNetLinearRegression.RunElasticNetLinearRegression(normalizedElnetData, approach.El netPenalty, out var _, out var _, out var _, out var candidateCoeffsNorm, out var interceptNorm, maxVars: approach.MaxNumBases);201 ElasticNetLinearRegression.RunElasticNetLinearRegression(normalizedElnetData, approach.ElasticNetPenalty, out var _, out var _, out var _, out var candidateCoeffsNorm, out var interceptNorm, maxVars: approach.MaxNumBases); 202 202 203 203 var coefs = RebiasCoefs(candidateCoeffsNorm, interceptNorm, X_avgs, X_stds, y_avg, y_std, out var intercept); … … 251 251 252 252 // return true if ALL indices of true values of arr1 also have true values in arr2 253 bool follows( bool[]arr1, bool[] arr2) {253 bool follows(BitArray arr1, bool[] arr2) { 254 254 if (arr1.Length != arr2.Length) throw new ArgumentException("invalid lengths"); 255 255 for (int i = 0; i < arr1.Length; i++) { … … 259 259 } 260 260 261 261 262 for (int i = 0; i < 32; i++) { // Iterate all combinations of 5 bools. 262 // map i to a bool array of length 5 263 var arr = i.ToBoolArray(5); 263 // map i to a bool array of length 5 264 var v = i; 265 int b = 0; 266 var arr = new BitArray(5); 267 var popCount = 0; 268 while (v>0) { if (v % 2 == 1) { arr[b++] = true; popCount++; } ; v /= 2; } 269 264 270 if (!follows(arr, valids)) continue; 265 int sum = arr.Where(b => b).Count(); // how many features are enabled? 266 if (sum >= 4) continue; // not too many features at once 271 if (popCount >= 4) continue; // not too many features at once 267 272 if (arr[0] && arr[2]) continue; // never need both exponent and inter 268 273 approaches.Add(new Approach(arr[0], arr[1], arr[2], arr[3], arr[4], exponents, nonlinFuncs, maxNumBases, penalty, minHingeThr, maxHingeThr, numHingeThrs)); -
branches/3022-FastFunctionExtraction/FFX/IBasisFunction.cs
r17737 r17779 4 4 internal interface IBasisFunction { 5 5 int Complexity { get; } 6 bool Is Nominator { get; set; }7 double[] Simulate(IRegressionProblemData data);6 bool IsDenominator { get; set; } 7 double[] Evaluate(IRegressionProblemData data); 8 8 IBasisFunction DeepCopy(); 9 9 } -
branches/3022-FastFunctionExtraction/FFX/NonlinearOperator.cs
r17737 r17779 2 2 namespace HeuristicLab.Algorithms.DataAnalysis.FastFunctionExtraction { 3 3 enum NonlinearOperator { 4 None, Abs, Log, Sin, Cos, G th, Lth4 None, Abs, Log, Sin, Cos, GT_Hinge, LT_Hinge 5 5 } 6 6 } -
branches/3022-FastFunctionExtraction/FFX/Plugin.cs
r17762 r17779 5 5 [PluginFile("HeuristicLab.Algorithms.DataAnalysis.FastFunctionExtraction.dll", PluginFileType.Assembly)] // each plugin represents a collection of files. The minimum is one file; the assembly. 6 6 7 // Usually your plugin references other HeuristicLab dlls. If you are referencing files (e.g. assemblies)8 // from another plugin the corresponding plugin should be added as a dependency.9 // Usually, if this information is incorrect you will still be able to use you plugin, but HL functionality10 // which uses plugin dependency resolution will not work correctly. For instance if plugin dependencies are11 // not correct then your plugin cannot be used on HeuristicLab.Hive12 //13 7 [PluginDependency("HeuristicLab.Algorithms.DataAnalysis", "3.4")] 14 8 [PluginDependency("HeuristicLab.Algorithms.DataAnalysis.Glmnet", "3.4")] … … 28 22 [PluginDependency("HeuristicLab.Random", "3.3")] 29 23 30 // HL plugin infrastructure discovers plugins on startup by trying to load all .dll and .exe files and looking for31 // classes deriving from PluginBase. The meta-information for the plugin class is specified in the attributes32 // above, and used by plugin infrastructure primarily for plugin dependency resolution.33 34 // Steps:35 // (1) Check out HL source code (e.g. the trunk version)36 // (2) Build external libraries HeuristicLab.ExtLibs.sln using the Build.cmd (in the path of the HL source code)37 // (3) Build HeuristicLab 3.3.sln using the Build.cmd38 // (4) Build this project. The output path for binaries is set to "".39 // this assumes you have the following folder structure40 // <ROOT>41 // |..hl42 // |..branches43 // | |..Templates44 // | |..EmptyPlugin45 // |..trunk46 // |.. bin47 48 // (5) Check that the output file has been added to the HL binaries folder (hl/trunk/bin/EmptyPlugin.dll)49 // (6) Start hl/trunk/bin/HeuristicLab.exe and open the "Plugin Manager".50 // Make sure your EmptyPlugin appears in the list of loaded plugins51 24 public class Plugin : PluginBase { 52 25 } -
branches/3022-FastFunctionExtraction/FFX/ProductBaseFunction.cs
r17737 r17779 8 8 public IBasisFunction B2 { get; set; } 9 9 10 public int Complexity 11 => 1 + B1.Complexity + B2.Complexity; 10 public int Complexity => 1 + B1.Complexity + B2.Complexity; 12 11 13 public bool Is Nominator { get; set; }12 public bool IsDenominator { get; set; } 14 13 15 public ProductBaseFunction(IBasisFunction b1, IBasisFunction b2, bool nominator) {14 public ProductBaseFunction(IBasisFunction b1, IBasisFunction b2, bool isDenominator) { 16 15 B1 = b1 ?? throw new ArgumentNullException(nameof(b1)); 17 16 B2 = b2 ?? throw new ArgumentNullException(nameof(b2)); 18 Is Nominator =nominator;17 IsDenominator = isDenominator; 19 18 } 20 19 21 public double[] Simulate(IRegressionProblemData data) {22 return B1. Simulate(data).Zip(B2.Simulate(data), (a, b) => a * b).ToArray();20 public double[] Evaluate(IRegressionProblemData data) { 21 return B1.Evaluate(data).Zip(B2.Evaluate(data), (a, b) => a * b).ToArray(); 23 22 } 24 23 … … 28 27 29 28 public IBasisFunction DeepCopy() { 30 return new ProductBaseFunction(B1, B2, Is Nominator);29 return new ProductBaseFunction(B1, B2, IsDenominator); 31 30 } 32 31 } -
branches/3022-FastFunctionExtraction/FFX/SimpleBasisFunction.cs
r17737 r17779 11 11 public double Exponent { get; set; } 12 12 public NonlinearOperator Operator { get; set; } 13 public bool Is Nominator { get; set; }13 public bool IsDenominator { get; set; } 14 14 public string Feature { get; set; } 15 15 public double Threshold { get; set; } // only relevant for hinge function 16 public bool HasExponent => ! Exponent.IsAlmost(1.0);16 public bool HasExponent => !(Exponent == 1.0); 17 17 public bool HasOperator => Operator != NonlinearOperator.None; 18 public int Complexity => Operator == NonlinearOperator.G th || Operator == NonlinearOperator.Lth? 3 : 1;18 public int Complexity => Operator == NonlinearOperator.GT_Hinge || Operator == NonlinearOperator.LT_Hinge ? 3 : 1; 19 19 20 public SimpleBasisFunction(string feature, double exponent = 1, NonlinearOperator op = NonlinearOperator.None, bool isNominator = true, double thr = 0) {20 public SimpleBasisFunction(string feature, double exponent = 1, NonlinearOperator op = NonlinearOperator.None, bool isNominator = true, double threshold = 0) { 21 21 Feature = feature ?? throw new ArgumentNullException(nameof(feature)); 22 22 Exponent = exponent; 23 23 Operator = op; 24 Is Nominator = isNominator;25 Threshold = thr ;24 IsDenominator = isNominator; 25 Threshold = threshold; 26 26 } 27 27 28 28 public IBasisFunction DeepCopy() { 29 return new SimpleBasisFunction(Feature, Exponent, Operator, Is Nominator, Threshold);29 return new SimpleBasisFunction(Feature, Exponent, Operator, IsDenominator, Threshold); 30 30 } 31 31 … … 46 46 47 47 var thr = Threshold.ToString(culture); 48 if (Operator == NonlinearOperator.L th) {48 if (Operator == NonlinearOperator.LT_Hinge) { 49 49 var expr = $"{str} {((Threshold >= 0) ? " - " + Math.Abs(Threshold).ToString(culture) : " + " + Math.Abs(Threshold).ToString(culture))}"; 50 50 str = $"IF(GT(0, {expr}), 0, {expr})"; 51 } else if (Operator == NonlinearOperator.G th) {51 } else if (Operator == NonlinearOperator.GT_Hinge) { 52 52 var expr = $"{thr} - {str}"; 53 53 str = $"IF(GT(0, {expr}), 0, {expr})"; … … 58 58 } 59 59 60 public double[] Simulate(IRegressionProblemData data) {60 public double[] Evaluate(IRegressionProblemData data) { 61 61 var exp = Exponent; 62 62 // exponentVals : e.g. "x3^2" … … 67 67 var vals = Eval(exponentVals); 68 68 69 if (!Is Nominator) {69 if (!IsDenominator) { 70 70 var y = data.TargetVariableValues; 71 71 vals = vals.Zip(y, (a, b) => -a * b).ToArray(); … … 87 87 case NonlinearOperator.Cos: 88 88 return x.Select(val => Math.Cos(val)).ToArray(); 89 case NonlinearOperator.G th:89 case NonlinearOperator.GT_Hinge: 90 90 return x.Select(val => Math.Max(0, thr - val)).ToArray(); 91 case NonlinearOperator.L th:91 case NonlinearOperator.LT_Hinge: 92 92 return x.Select(val => Math.Max(0, val - thr)).ToArray(); 93 93 default: -
branches/3022-FastFunctionExtraction/FFX/Utils.cs
r17737 r17779 25 25 } 26 26 27 // find the array containing n (linearly) evenly spaced double values ranging from start to end (including both)28 27 public static IEnumerable<double> Linspace(double start, double end, int n) { 29 28 if (end < start) throw new ArgumentException("end must be higher that start"); … … 35 34 36 35 public static double[] Normalize(double[] vals) 37 => Bias(vals, 0,1);36 => Transform(vals, mean: 0, stdDev: 1); 38 37 39 public static double[] Bias(double[] vals, double targetAvg, double targetStd) {38 public static double[] Transform(double[] vals, double mean, double stdDev) { 40 39 var result = new double[vals.Length]; 41 40 42 // bias standard deviation 43 var stdRatio = vals.StandardDeviationPop() / targetStd; 41 var scale = vals.StandardDeviationPop() / stdDev; 44 42 for (int i = 0; i < vals.Length; i++) { 45 result[i] = vals[i] / s tdRatio;43 result[i] = vals[i] / scale; 46 44 } 47 45 48 // bias average 49 var avgDiff = result.Average() - targetAvg; 46 var offset = result.Average() - mean; 50 47 for (int i = 0; i < vals.Length; i++) { 51 result[i] = result[i] - avgDiff;48 result[i] = result[i] - offset; 52 49 } 53 50 54 51 return result; 55 52 } 56 57 public static bool[] ToBoolArray(this int val, int min_length) {58 var temp = Convert.ToString(val, 2).Select(s => s.Equals('1')).ToArray();59 if (temp.Length >= min_length) return temp;60 61 var ans = new bool[min_length];62 for(int i = 0; i < min_length - temp.Length; i++) {63 ans[i] = false;64 }65 for(int i = 0; i < temp.Length; i++) {66 ans[i + min_length - temp.Length] = temp[i];67 }68 return ans;69 }70 53 } 71 54 }
Note: See TracChangeset
for help on using the changeset viewer.