Ignore:
Timestamp:
07/04/19 16:11:29 (22 months ago)
Author:
abeham
Message:

#2892: merged to stable

Location:
stable
Files:
3 edited
1 copied

Legend:

Unmodified
Added
Removed
  • stable

  • stable/HeuristicLab.Algorithms.DataAnalysis

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4

  • stable/HeuristicLab.Algorithms.DataAnalysis/3.4/Linear/LinearRegressionModel.cs

    r16389 r17074  
    2222using System;
    2323using System.Collections.Generic;
     24using System.Drawing;
    2425using System.Linq;
    2526using HeuristicLab.Common;
     
    3536  [Item("Linear Regression Model", "Represents a linear regression model.")]
    3637  public sealed class LinearRegressionModel : RegressionModel, IConfidenceRegressionModel {
     38    public static new Image StaticItemImage {
     39      get { return HeuristicLab.Common.Resources.VSImageLibrary.Function; }
     40    }
    3741
    3842    [Storable]
     
    4953      get; private set;
    5054    }
    51    
     55
    5256    public override IEnumerable<string> VariablesUsedForPrediction {
    53       get { return allowedInputVariables; }
     57      get { return doubleVariables.Union(factorVariables.Select(f => f.Key)); }
    5458    }
    5559
    5660    [Storable]
    57     private string[] allowedInputVariables;
     61    private string[] doubleVariables;
    5862    [Storable]
    5963    private List<KeyValuePair<string, IEnumerable<string>>> factorVariables;
     64
     65    /// <summary>
     66    /// Enumerable of variable names used by the model including one-hot-encoded of factor variables.
     67    /// </summary>
     68    public IEnumerable<string> ParameterNames {
     69      get {
     70        return factorVariables.SelectMany(kvp => kvp.Value.Select(factorVal => $"{kvp.Key}={factorVal}"))
     71          .Concat(doubleVariables)
     72          .Concat(new[] { "<const>" });
     73      }
     74    }
    6075
    6176    [StorableConstructor]
     
    6984      this.NoiseSigma = original.NoiseSigma;
    7085
    71       allowedInputVariables = (string[])original.allowedInputVariables.Clone();
     86      doubleVariables = (string[])original.doubleVariables.Clone();
    7287      this.factorVariables = original.factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
    7388    }
     
    7893      this.W = new double[w.Length];
    7994      Array.Copy(w, W, w.Length);
    80       this.C = new double[covariance.GetLength(0),covariance.GetLength(1)];
     95      this.C = new double[covariance.GetLength(0), covariance.GetLength(1)];
    8196      Array.Copy(covariance, C, covariance.Length);
    8297      this.NoiseSigma = noiseSigma;
    83       this.allowedInputVariables = doubleInputVariables.ToArray();
     98      this.doubleVariables = doubleInputVariables.ToArray();
     99      // clone
    84100      this.factorVariables = factorVariables.Select(kvp => new KeyValuePair<string, IEnumerable<string>>(kvp.Key, new List<string>(kvp.Value))).ToList();
    85101    }
     
    94110
    95111    public override IEnumerable<double> GetEstimatedValues(IDataset dataset, IEnumerable<int> rows) {
    96       double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
     112      double[,] inputData = dataset.ToArray(doubleVariables, rows);
    97113      double[,] factorData = dataset.ToArray(factorVariables, rows);
    98114
     
    113129
    114130    public IEnumerable<double> GetEstimatedVariances(IDataset dataset, IEnumerable<int> rows) {
    115       double[,] inputData = dataset.ToArray(allowedInputVariables, rows);
     131      double[,] inputData = dataset.ToArray(doubleVariables, rows);
    116132      double[,] factorData = dataset.ToArray(factorVariables, rows);
    117133
     
    122138
    123139      double[] d = new double[C.GetLength(0)];
    124      
     140
    125141      for (int row = 0; row < n; row++) {
    126142        for (int column = 0; column < columns; column++) {
    127           d[column] = inputData[row,column];
     143          d[column] = inputData[row, column];
    128144        }
    129145        d[columns] = 1;
    130146
    131147        double var = 0.0;
    132         for(int i=0;i<d.Length;i++) {
    133           for(int j = 0;j<d.Length;j++) {
     148        for (int i = 0; i < d.Length; i++) {
     149          for (int j = 0; j < d.Length; j++) {
    134150            var += d[i] * C[i, j] * d[j];
    135151          }
    136152        }
    137         yield return var + NoiseSigma*NoiseSigma;
     153        yield return var + NoiseSigma * NoiseSigma;
    138154      }
    139155    }
    140 
    141156
    142157    public override IRegressionSolution CreateRegressionSolution(IRegressionProblemData problemData) {
Note: See TracChangeset for help on using the changeset viewer.