Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
11/17/16 15:41:33 (8 years ago)
Author:
gkronber
Message:

#2697: reverse merge of r14378, r14390, r14391, r14393, r14394, r14396

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegression.cs

    r14393 r14400  
    7373      IEnumerable<string> allowedInputVariables = problemData.AllowedInputVariables;
    7474      IEnumerable<int> rows = problemData.TrainingIndices;
    75       double[,] inputMatrix = dataset.ToArray(allowedInputVariables.Concat(new string[] { targetVariable }), rows);
     75      double[,] inputMatrix = AlglibUtil.PrepareInputMatrix(dataset, allowedInputVariables.Concat(new string[] { targetVariable }), rows);
    7676      if (inputMatrix.Cast<double>().Any(x => double.IsNaN(x) || double.IsInfinity(x)))
    7777        throw new NotSupportedException("Linear regression does not support NaN or infinity values in the input dataset.");
     
    8181      int nRows = inputMatrix.GetLength(0);
    8282      int nFeatures = inputMatrix.GetLength(1) - 1;
    83       double[] coefficients;
     83      double[] coefficients = new double[nFeatures + 1]; // last coefficient is for the constant
    8484
    8585      int retVal = 1;
     
    9191      alglib.lrunpack(lm, out coefficients, out nFeatures);
    9292
    93       var tree = LinearModelToTreeConverter.CreateTree(allowedInputVariables.ToArray(),
    94         coefficients.Take(nFeatures).ToArray(), @const: coefficients[nFeatures]);
     93      ISymbolicExpressionTree tree = new SymbolicExpressionTree(new ProgramRootSymbol().CreateTreeNode());
     94      ISymbolicExpressionTreeNode startNode = new StartSymbol().CreateTreeNode();
     95      tree.Root.AddSubtree(startNode);
     96      ISymbolicExpressionTreeNode addition = new Addition().CreateTreeNode();
     97      startNode.AddSubtree(addition);
     98
     99      int col = 0;
     100      foreach (string column in allowedInputVariables) {
     101        VariableTreeNode vNode = (VariableTreeNode)new HeuristicLab.Problems.DataAnalysis.Symbolic.Variable().CreateTreeNode();
     102        vNode.VariableName = column;
     103        vNode.Weight = coefficients[col];
     104        addition.AddSubtree(vNode);
     105        col++;
     106      }
     107
     108      ConstantTreeNode cNode = (ConstantTreeNode)new Constant().CreateTreeNode();
     109      cNode.Value = coefficients[coefficients.Length - 1];
     110      addition.AddSubtree(cNode);
    95111
    96112      SymbolicRegressionSolution solution = new SymbolicRegressionSolution(new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter()), (IRegressionProblemData)problemData.Clone());
Note: See TracChangeset for help on using the changeset viewer.