Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
08/01/10 18:08:04 (14 years ago)
Author:
gkronber
Message:

Made variable frequency analyzer more efficient and removed subtraction of base line (variable impacts are now in the range 0..1 instead of -1 .. 1) #1011

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis/3.3/Symbolic/VariableFrequencyAnalyser.cs

    r4068 r4125  
    8484
    8585    public static IEnumerable<KeyValuePair<string, double>> CalculateVariableFrequencies(IEnumerable<SymbolicExpressionTree> trees, IEnumerable<string> inputVariables) {
    86       int totalVariableReferences = 0;
    8786      Dictionary<string, double> variableReferencesSum = new Dictionary<string, double>();
    8887      Dictionary<string, double> variableFrequencies = new Dictionary<string, double>();
     
    9493          variableReferencesSum[pair.Key] += pair.Value;
    9594        }
    96         totalVariableReferences += GetTotalVariableReferencesCount(tree);
    9795      }
     96      double totalVariableReferences = variableReferencesSum.Values.Sum();
    9897      foreach (string inputVariable in inputVariables) {
    99         double relFreq = variableReferencesSum[inputVariable] / (double)totalVariableReferences;
     98        double relFreq = variableReferencesSum[inputVariable] / totalVariableReferences;
    10099        variableFrequencies.Add(inputVariable, relFreq);
    101100      }
     
    103102    }
    104103
    105     private static int GetTotalVariableReferencesCount(SymbolicExpressionTree tree) {
    106       return tree.IterateNodesPrefix().OfType<VariableTreeNode>().Count();
    107     }
    108 
    109104    private static IEnumerable<KeyValuePair<string, int>> GetVariableReferenceCount(SymbolicExpressionTree tree, IEnumerable<string> inputVariables) {
    110105      Dictionary<string, int> references = new Dictionary<string, int>();
    111       var groupedFuns = (from node in tree.IterateNodesPrefix().OfType<VariableTreeNode>()
    112                          select node.VariableName)
    113                          .GroupBy(x => x)
    114                          .Select(g => new { Key = g.Key, Count = g.Count() })
    115                          .ToArray();
     106      var variableNames = from node in tree.IterateNodesPrefix().OfType<VariableTreeNode>()
     107                          select node.VariableName;
    116108
    117       foreach (var inputVariable in inputVariables) {
    118         var matchingFuns = from g in groupedFuns
    119                            where g.Key == inputVariable
    120                            select g.Count;
    121         if (matchingFuns.Count() == 0)
    122           references.Add(inputVariable, 0);
    123         else {
    124           references.Add(inputVariable, matchingFuns.Single());
     109      foreach (var variableName in variableNames) {
     110        if (!references.ContainsKey(variableName)) {
     111          references[variableName] = 1;
     112        } else {
     113          references[variableName] += 1;
    125114        }
    126115      }
Note: See TracChangeset for help on using the changeset viewer.