Ignore:
Timestamp:
10/22/09 10:31:19 (13 years ago)
Author:
gkronber
Message:

Fixed #787 (LinearRegressionOperator uses leastsquares function of ALGLIB instead of linearregression function)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/sources/HeuristicLab.LinearRegression/3.2/LinearRegressionOperator.cs

    r2440 r2445  
    101101
    102102    private double[] CalculateCoefficients(double[,] inputMatrix, double[] targetVector) {
    103       double[] weights = new double[targetVector.Length];
    104       double[] coefficients = new double[inputMatrix.GetLength(1)];
    105       for (int i = 0; i < weights.Length; i++) weights[i] = 1.0;
    106       // call external ALGLIB solver
    107       alglib.leastsquares.buildgeneralleastsquares(ref targetVector, ref weights, ref inputMatrix, inputMatrix.GetLength(0), inputMatrix.GetLength(1), ref coefficients);
     103      int retVal = 0;
     104      alglib.linreg.linearmodel lm = new alglib.linreg.linearmodel();
     105      alglib.linreg.lrreport ar = new alglib.linreg.lrreport();
     106      int n = targetVector.Length;
     107      int p = inputMatrix.GetLength(1);
     108      double[,] dataset = new double[n, p];
     109      for (int row = 0; row < n; row++) {
     110        for (int column = 0; column < p-1; column++) {
     111          dataset[row, column] = inputMatrix[row, column];
     112        }
     113        dataset[row, p-1] = targetVector[row];
     114      }
     115      alglib.linreg.lrbuild(ref dataset, n, p-1, ref retVal, ref lm, ref ar);
     116      if (retVal != 1) throw new ArgumentException("Error in calculation of linear regression model");
     117      Console.Out.WriteLine("ALGLIB Linear Regression: Estimated generalization RMS = {0}", ar.cvrmserror);
    108118
     119      double[] coefficients = new double[p];
     120      for (int i = 0; i < p; i++) {
     121        coefficients[i] = lm.w[i+4];
     122      }
    109123      return coefficients;
    110124    }
Note: See TracChangeset for help on using the changeset viewer.