我刚刚开始学习accord.net,在浏览一些示例时,我注意到SimpleLinearRegression上的回归方法已经过时了。
显然,我应该使用OrdinaryLeastSquares类,但我找不到任何返回剩余平方和的方法,类似于回归方法。
我需要自己创建这个方法吗?
发布于 2017-10-24 04:42:51
下面是一个完整的例子,说明如何学习SimpleLinearRegression,并仍然能够像使用以前版本的框架一样计算残差平方和:
// This is the same data from the example available at
// http://mathbits.com/MathBits/TISection/Statistics2/logarithmic.htm
// Declare your inputs and output data
double[] inputs = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11 };
double[] outputs = { 6, 9.5, 13, 15, 16.5, 17.5, 18.5, 19, 19.5, 19.7, 19.8 };
// Transform inputs to logarithms
double[] logx = Matrix.Log(inputs);
// Use Ordinary Least Squares to learn the regression
OrdinaryLeastSquares ols = new OrdinaryLeastSquares();
// Use OLS to learn the simple linear regression
SimpleLinearRegression lr = ols.Learn(logx, outputs);
// Compute predicted values for inputs
double[] predicted = lr.Transform(logx);
// Get an expression representing the learned regression model
// We just have to remember that 'x' will actually mean 'log(x)'
string result = lr.ToString("N4", CultureInfo.InvariantCulture);
// Result will be "y(x) = 6.1082x + 6.0993"
// The mean squared error between the expected and the predicted is
double error = new SquareLoss(outputs).Loss(predicted); // 0.261454本例中的最后一行应该是您最感兴趣的一行。正如您所看到的,现在可以使用SquareLoss class计算之前由.Regress方法返回的剩余平方和。这种方法的优点是,现在您应该能够计算出对您最重要的最合适的指标,例如ZeroOneLoss、Euclidean loss或Hamming loss。
无论如何,我只想重申,框架中标记为过时的任何方法都不会很快停止工作。它们被标记为过时,这意味着在使用这些方法时将不支持新功能,但如果您在其中使用了这些方法中的任何一个,您的应用程序将不会停止工作。
https://stackoverflow.com/questions/46566371
复制相似问题