Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/23/11 13:52:29 (13 years ago)
Author:
gkronber
Message:

#1418 Fixed a problem with scaling of regression and classification solutions (moved scale method out of solution into the model because of leaky abstraction).

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Problems.DataAnalysis.Symbolic.Classification/3.4/SymbolicDiscriminantFunctionClassificationModel.cs

    r5809 r5818  
    115115    }
    116116    #endregion
     117
     118    public static void Scale(SymbolicDiscriminantFunctionClassificationModel model, IClassificationProblemData problemData) {
     119      var dataset = problemData.Dataset;
     120      var targetVariable = problemData.TargetVariable;
     121      var rows = problemData.TrainingIndizes;
     122      var estimatedValues = model.Interpreter.GetSymbolicExpressionTreeValues(model.SymbolicExpressionTree, dataset, rows);
     123      var targetValues = dataset.GetEnumeratedVariableValues(targetVariable, rows);
     124      double alpha;
     125      double beta;
     126      OnlineLinearScalingParameterCalculator.Calculate(estimatedValues, targetValues, out alpha, out beta);
     127
     128      ConstantTreeNode alphaTreeNode = null;
     129      ConstantTreeNode betaTreeNode = null;
     130      // check if model has been scaled previously by analyzing the structure of the tree
     131      var startNode = model.SymbolicExpressionTree.Root.GetSubtree(0);
     132      if (startNode.GetSubtree(0).Symbol is Addition) {
     133        var addNode = startNode.GetSubtree(0);
     134        if (addNode.SubtreesCount == 2 && addNode.GetSubtree(0).Symbol is Multiplication && addNode.GetSubtree(1).Symbol is Constant) {
     135          alphaTreeNode = addNode.GetSubtree(1) as ConstantTreeNode;
     136          var mulNode = addNode.GetSubtree(0);
     137          if (mulNode.SubtreesCount == 2 && mulNode.GetSubtree(1).Symbol is Constant) {
     138            betaTreeNode = mulNode.GetSubtree(1) as ConstantTreeNode;
     139          }
     140        }
     141      }
     142      // if tree structure matches the structure necessary for linear scaling then reuse the existing tree nodes
     143      if (alphaTreeNode != null && betaTreeNode != null) {
     144        betaTreeNode.Value *= beta;
     145        alphaTreeNode.Value *= beta;
     146        alphaTreeNode.Value += alpha;
     147      } else {
     148        var mainBranch = startNode.GetSubtree(0);
     149        startNode.RemoveSubtree(0);
     150        var scaledMainBranch = MakeSum(MakeProduct(beta, mainBranch), alpha);
     151        startNode.AddSubtree(scaledMainBranch);
     152      }
     153    }
     154
     155    private static ISymbolicExpressionTreeNode MakeSum(ISymbolicExpressionTreeNode treeNode, double alpha) {
     156      if (alpha.IsAlmost(0.0)) {
     157        return treeNode;
     158      } else {
     159        var node = (new Addition()).CreateTreeNode();
     160        var alphaConst = MakeConstant(alpha);
     161        node.AddSubtree(treeNode);
     162        node.AddSubtree(alphaConst);
     163        return node;
     164      }
     165    }
     166
     167    private static ISymbolicExpressionTreeNode MakeProduct(double beta, ISymbolicExpressionTreeNode treeNode) {
     168      if (beta.IsAlmost(1.0)) {
     169        return treeNode;
     170      } else {
     171        var node = (new Multiplication()).CreateTreeNode();
     172        var betaConst = MakeConstant(beta);
     173        node.AddSubtree(treeNode);
     174        node.AddSubtree(betaConst);
     175        return node;
     176      }
     177    }
     178
     179    private static ISymbolicExpressionTreeNode MakeConstant(double c) {
     180      var node = (ConstantTreeNode)(new Constant()).CreateTreeNode();
     181      node.Value = c;
     182      return node;
     183    }
    117184  }
    118185}
Note: See TracChangeset for help on using the changeset viewer.