Free cookie consent management tool by TermsFeed Policy Generator

Changeset 15046


Ignore:
Timestamp:
06/23/17 13:30:46 (7 years ago)
Author:
mkommend
Message:

#745: Extracted solution creation into a separate static method and renamed CreateElasticNetLinearRegressionSolution to CalculateModelCoefficients.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.Algorithms.DataAnalysis.Glmnet/3.4/ElasticNetLinearRegression.cs

    r15023 r15046  
    8888    }
    8989
    90     private void CreateSolution(double lambda) {
     90  private void CreateSolution(double lambda) {
    9191      double trainNMSE;
    9292      double testNMSE;
    93       var coeff = CreateElasticNetLinearRegressionSolution(Problem.ProblemData, Penality, lambda, out trainNMSE, out testNMSE);
     93      var coeff = CalculateModelCoefficients(Problem.ProblemData, Penality, lambda, out trainNMSE, out testNMSE);
    9494      Results.Add(new Result("NMSE (train)", new DoubleValue(trainNMSE)));
    9595      Results.Add(new Result("NMSE (test)", new DoubleValue(testNMSE)));
    9696
    97       var ds = Problem.ProblemData.Dataset;
    98       var allVariables = Problem.ProblemData.AllowedInputVariables.ToArray();
     97      var solution = CreateSymbolicSolution(coeff, Problem.ProblemData);
     98      Results.Add(new Result(solution.Name, solution.Description, solution));
     99    }
     100
     101    public static IRegressionSolution CreateSymbolicSolution(double[] coeff, IRegressionProblemData problemData) {
     102      var ds = problemData.Dataset;
     103      var allVariables = problemData.AllowedInputVariables.ToArray();
    99104      var doubleVariables = allVariables.Where(ds.VariableHasType<double>);
    100105      var factorVariableNames = allVariables.Where(ds.VariableHasType<string>);
     
    130135      }
    131136      var tree = LinearModelToTreeConverter.CreateTree(
    132         remainingFactorVariablesAndValues, factorCoeff.ToArray(), 
     137        remainingFactorVariablesAndValues, factorCoeff.ToArray(),
    133138        remainingDoubleVariables.ToArray(), doubleVarCoeff.ToArray(),
    134139        coeff.Last());
     
    136141
    137142      SymbolicRegressionSolution solution = new SymbolicRegressionSolution(
    138         new SymbolicRegressionModel(Problem.ProblemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter()),
    139         (IRegressionProblemData)Problem.ProblemData.Clone());
     143        new SymbolicRegressionModel(problemData.TargetVariable, tree, new SymbolicDataAnalysisExpressionTreeInterpreter()),
     144        (IRegressionProblemData)problemData.Clone());
    140145      solution.Model.Name = "Elastic-net Linear Regression Model";
    141146      solution.Name = "Elastic-net Linear Regression Solution";
    142147
    143       Results.Add(new Result(solution.Name, solution.Description, solution));
     148      return solution;
    144149    }
    145150
     
    242247    }
    243248
    244     public static double[] CreateElasticNetLinearRegressionSolution(IRegressionProblemData problemData, double penalty, double lambda,
     249    public static double[] CalculateModelCoefficients(IRegressionProblemData problemData, double penalty, double lambda,
    245250            out double trainNMSE, out double testNMSE,
    246251            double coeffLowerBound = double.NegativeInfinity, double coeffUpperBound = double.PositiveInfinity) {
     
    248253      double[] testNMSEs;
    249254      // run for exactly one lambda
    250       var coeffs = CreateElasticNetLinearRegressionSolution(problemData, penalty, new double[] { lambda }, out trainNMSEs, out testNMSEs, coeffLowerBound, coeffUpperBound);
     255      var coeffs = CalculateModelCoefficients(problemData, penalty, new double[] { lambda }, out trainNMSEs, out testNMSEs, coeffLowerBound, coeffUpperBound);
    251256      trainNMSE = trainNMSEs[0];
    252257      testNMSE = testNMSEs[0];
    253258      return coeffs[0];
    254259    }
    255     public static double[][] CreateElasticNetLinearRegressionSolution(IRegressionProblemData problemData, double penalty, double[] lambda,
     260    public static double[][] CalculateModelCoefficients(IRegressionProblemData problemData, double penalty, double[] lambda,
    256261            out double[] trainNMSEs, out double[] testNMSEs,
    257262            double coeffLowerBound = double.NegativeInfinity, double coeffUpperBound = double.PositiveInfinity,
Note: See TracChangeset for help on using the changeset viewer.