C# 中的非线性回归
我正在寻找一种基于二维数据集生成非线性(最好是二次)曲线的方法,以用于预测目的。现在,我正在使用自己的普通最小二乘法 (OLS) 实现来生成线性趋势,但我的趋势更适合曲线模型。我正在分析的数据是一段时间内的系统负载。
这是我用来生成线性系数的方程:
我有一个看看 Math.NET Numerics 和其他一些库,但它们要么提供插值而不是回归(这对我来说没有用),要么代码不提供t以某种方式工作。
有人知道任何可以生成此类曲线系数的免费开源库或代码示例吗?
I'm looking for a way to produce a non-linear (preferably quadratic) curve, based on a 2D data set, for predictive purposes. Right now I'm using my own implementation of ordinary least squares (OLS) to produce a linear trend, but my trends are much more suited to a curve model. The data I'm analysing is system load over time.
Here's the equation that I'm using to produce my linear coefficients:
I've had a look at Math.NET Numerics and a few other libs, but they either provide interpolation instead of regression (which is of no use to me), or the code just doesn't work in some way.
Anyone know of any free open source libs or code samples that can produce the coefficients for such a curve?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我不认为你想要非线性回归。即使您使用二次函数,它仍然称为线性回归。你想要的就是所谓的多元回归。如果您想要二次方,只需将 ax 平方项添加到因变量中即可。
I don't think you want non linear regression. Even if you are using a quadratic function, it is still called linear regression. What you want is called multivariate regression. If you want a quadratic you just add a x squared term to your dependent variables.
我会看一下 http://mathforum.org/library/drmath/view/53796.html 尝试了解如何完成它。
然后这个有一个很好的实现我认为这会对你有帮助。
I would take a look at http://mathforum.org/library/drmath/view/53796.html to try get an idea about how it can be done.
Then this has a nice implementation that I think will help you.
我使用了 MathNet.Iridium 版本因为它兼容.NET 3.5和VS2008。该方法基于 Vandermonde矩阵。然后我创建了一个类来保存多项式回归
,然后我像这样使用它:
计算的系数
[1,0.57,-0.15]
与输出:这与 二次 来自 Wolfram Alpha 的结果。
编辑 1
为了达到您想要的拟合效果,请尝试对
x_data
和y_data
进行以下初始化:这会产生以下系数(从最低功率到最高功率)
I used the MathNet.Iridium release because it is compatible with .NET 3.5 and VS2008. The method is based on the Vandermonde matrix. Then I created a class to hold my polynomial regression
which then I use it like this:
Calculated coefficients of
[1,0.57,-0.15]
with the output:Which matches the quadratic results from Wolfram Alpha.
Edit 1
To get to the fit you want try the following initialization for
x_data
andy_data
:which produces the following coefficients (from lowest power to highest)
@ja72 代码非常好。但我将其移植到当前版本的 Math.NET 上(目前不支持 MathNet.Iridium,因为我了解)并优化代码大小和性能(例如,由于性能缓慢,我的解决方案中未使用 Math.Pow 函数)。
它也可以在 github:gist 上找到。
@ja72 code is pretty good. But I ported it on the present version of Math.NET (MathNet.Iridium is not supported for now as I understand) and optimized code size and performance (For instance,
Math.Pow
function is not used in my solution because of slow performance).It's also available on github:gist.