Free cookie consent management tool by TermsFeed Policy Generator

Ignore:
Timestamp:
03/29/21 09:54:58 (3 years ago)
Author:
mkommend
Message:

#3105: Merge trunk changes into branch.

Location:
branches/3105_PythonFormatter
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • branches/3105_PythonFormatter

  • branches/3105_PythonFormatter/HeuristicLab.Algorithms.DataAnalysis

  • branches/3105_PythonFormatter/HeuristicLab.Algorithms.DataAnalysis/3.4

  • branches/3105_PythonFormatter/HeuristicLab.Algorithms.DataAnalysis/3.4/GAM/GeneralizedAdditiveModelAlgorithm.cs

    r17815 r17918  
    3939  [Creatable(CreatableAttribute.Categories.DataAnalysisRegression, Priority = 600)]
    4040  public sealed class GeneralizedAdditiveModelAlgorithm : FixedDataAnalysisAlgorithm<IRegressionProblem> {
     41
    4142    #region ParameterNames
    4243
     
    4647    private const string SetSeedRandomlyParameterName = "SetSeedRandomly";
    4748    private const string CreateSolutionParameterName = "CreateSolution";
     49
    4850    #endregion
    4951
     
    141143      var problemData = Problem.ProblemData;
    142144      var ds = problemData.Dataset;
    143       var trainRows = problemData.TrainingIndices;
    144       var testRows = problemData.TestIndices;
     145      var trainRows = problemData.TrainingIndices.ToArray();
     146      var testRows = problemData.TestIndices.ToArray();
    145147      var avgY = problemData.TargetVariableTrainingValues.Average();
    146148      var inputVars = problemData.AllowedInputVariables.ToArray();
     
    178180      double[] resTest = problemData.TargetVariableTestValues.Select(yi => yi - avgY).ToArray();
    179181
    180       curRMSE.Value = res.StandardDeviation();
    181       curRMSETest.Value = resTest.StandardDeviation();
    182       rmseRow.Values.Add(res.StandardDeviation());
    183       rmseRowTest.Values.Add(resTest.StandardDeviation());
     182      curRMSE.Value = RMSE(res);
     183      curRMSETest.Value = RMSE(resTest);
     184      rmseRow.Values.Add(curRMSE.Value);
     185      rmseRowTest.Values.Add(curRMSETest.Value);
    184186
    185187
     
    197199          AddInPlace(resTest, f[inputIdx].GetEstimatedValues(ds, testRows));
    198200
    199           rssTable[inputIdx, 0] = res.Variance();
     201          rssTable[inputIdx, 0] = MSE(res);
    200202          f[inputIdx] = RegressSpline(problemData, inputVar, res, lambda);
    201203
     
    204206        }
    205207
    206         curRMSE.Value = res.StandardDeviation();
    207         curRMSETest.Value = resTest.StandardDeviation();
     208        curRMSE.Value = RMSE(res);
     209        curRMSETest.Value = RMSE(resTest);
    208210        rmseRow.Values.Add(curRMSE.Value);
    209211        rmseRowTest.Values.Add(curRMSETest.Value);
     
    215217        var model = new RegressionEnsembleModel(f.Concat(new[] { new ConstantModel(avgY, problemData.TargetVariable) }));
    216218        model.AverageModelEstimates = false;
    217         var solution = model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone());
     219        var solution = model.CreateRegressionSolution((IRegressionProblemData)problemData.Clone());       
    218220        Results.Add(new Result("Ensemble solution", solution));
    219221      }
     222    }
     223
     224    public static double MSE(IEnumerable<double> residuals) {
     225      var mse  = residuals.Select(r => r * r).Average();
     226      return mse;
     227    }
     228
     229    public static double RMSE(IEnumerable<double> residuals) {
     230      var mse = MSE(residuals);
     231      var rmse = Math.Sqrt(mse);
     232      return rmse;
    220233    }
    221234
  • branches/3105_PythonFormatter/HeuristicLab.Algorithms.DataAnalysis/3.4/GAM/Spline1dModel.cs

    r17839 r17918  
    2626using HeuristicLab.Core;
    2727using HeuristicLab.Problems.DataAnalysis;
     28using System;
    2829
    2930namespace HeuristicLab.Algorithms.DataAnalysis {
     
    3536    private alglib.spline1d.spline1dinterpolant interpolant;
    3637
    37     [Storable]
    38     private readonly string[] variablesUsedForPrediction;
    39     public override IEnumerable<string> VariablesUsedForPrediction {
    40       get {
    41         return variablesUsedForPrediction;
     38    [Storable(OldName = "variablesUsedForPrediction")]
     39    private string[] StorableVariablesUsedForPrediction {
     40      set {
     41        if (value.Length > 1) throw new ArgumentException("A one-dimensional spline model supports only one input variable.");
     42        inputVariable = value[0];
    4243      }
    4344    }
     45
     46    [Storable]
     47    private string inputVariable;
     48    public override IEnumerable<string> VariablesUsedForPrediction => new[] { inputVariable };
    4449
    4550    [StorableConstructor]
     
    4954
    5055    private Spline1dModel(Spline1dModel orig, Cloner cloner) : base(orig, cloner) {
    51       this.variablesUsedForPrediction = orig.VariablesUsedForPrediction.ToArray();
     56      this.inputVariable = orig.inputVariable;
    5257      this.interpolant = (alglib.spline1d.spline1dinterpolant)orig.interpolant.make_copy();
    5358    }
    5459    public Spline1dModel(alglib.spline1d.spline1dinterpolant interpolant, string targetVar, string inputVar)
    55       : base(targetVar, "Spline model (1d)") {
    56       this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();     
    57       this.variablesUsedForPrediction = new string[] { inputVar };
     60      : base(targetVar, $"Spline model ({inputVar})") {
     61      this.interpolant = (alglib.spline1d.spline1dinterpolant)interpolant.make_copy();
     62      this.inputVariable = inputVar;     
    5863    }
    5964
    6065
    61     public override IDeepCloneable Clone(Cloner cloner) {
    62       return new Spline1dModel(this, cloner);
     66    public override IDeepCloneable Clone(Cloner cloner) => new Spline1dModel(this, cloner);
     67
     68    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
     69      var solution =  new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
     70      solution.Name = $"Regression Spline ({inputVariable})";
     71
     72      return solution;
    6373    }
    6474
    65     public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
    66       return new RegressionSolution(this, (IRegressionProblemData)problemData.Clone());
    67     }
    68 
    69     public double GetEstimatedValue(double x) {
    70       return alglib.spline1d.spline1dcalc(interpolant, x);
    71     }
     75    public double GetEstimatedValue(double x) => alglib.spline1d.spline1dcalc(interpolant, x);
    7276
    7377    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
    74       var x = dataset.GetDoubleValues(VariablesUsedForPrediction.First(), rows).ToArray();
    75       foreach (var xi in x) {
    76         yield return GetEstimatedValue(xi);
    77       }
     78      return dataset.GetDoubleValues(inputVariable, rows).Select(GetEstimatedValue);
    7879    }
    7980
  • branches/3105_PythonFormatter/HeuristicLab.Algorithms.DataAnalysis/3.4/GradientBoostedTrees/GradientBoostedTreesAlgorithm.cs

    r17180 r17918  
    281281            new AccuracyMaximizationThresholdCalculator());
    282282          var classificationProblemData = new ClassificationProblemData(problemData.Dataset,
    283             problemData.AllowedInputVariables, problemData.TargetVariable, problemData.Transformations);
     283            problemData.AllowedInputVariables, problemData.TargetVariable, transformations: problemData.Transformations);
    284284          classificationProblemData.TrainingPartition.Start = Problem.ProblemData.TrainingPartition.Start;
    285285          classificationProblemData.TrainingPartition.End = Problem.ProblemData.TrainingPartition.End;
  • branches/3105_PythonFormatter/HeuristicLab.Algorithms.DataAnalysis/3.4/RandomForest/RandomForestModelSurrogate.cs

    r17278 r17918  
    135135    }
    136136
     137    public override bool IsProblemDataCompatible(IDataAnalysisProblemData problemData, out string errorMessage) {
     138      return ActualModel.IsProblemDataCompatible(problemData, out errorMessage);
     139    }
     140
    137141    //RegressionModel methods
    138142    public bool IsProblemDataCompatible(IRegressionProblemData problemData, out string errorMessage) {
Note: See TracChangeset for help on using the changeset viewer.