用于进行线性或非线性最小二乘近似的 Ruby 库?

发布于 2024-11-09 16:31:51 字数 247 浏览 4 评论 0原文

是否有一个 Ruby 库允许我对一组数据进行线性或非线性最小二乘近似。

我想做的是:

  • 给定一系列 [x,y] 数据点
  • 针对该数据生成线性或非线性最小二乘近似
  • 库不必弄清楚是否需要执行线性或非线性 最小二乘近似非线性近似。库的调用者应该知道他们需要什么类型的回归,

我不想尝试移植一些 C/C++/Java 库来获得此功能,所以我希望有一些现有的 Ruby 库可供我使用。

Is there a Ruby library that allows me to do either linear or non-linear least squares approximation of a set of data.

What I would like to do is the following:

  • Given a series of [x,y] data points
  • Generate a linear or non linear least squares approximation against that data
  • The library doesn't have to figure out if it needs to do a linear or non linear approximation. The caller of the library should know what type of regression they need

I'd prefer not to have to try to port some C/C++/Java library to get this functionality so I'm hoping there is some existing Ruby lib that I can use.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

女中豪杰 2024-11-16 16:31:51

尝试使用“statsample”gem。您可以使用下面提供的示例执行对数、指数、幂或任何其他转换。我希望这有帮助。

require 'statsample'

# Independent Variable
x_data = [Math.exp(1), Math.exp(2), Math.exp(3), Math.exp(4), Math.exp(5)]

# Dependent Variable
y_data = [3, 5, 7, 9, 11]

# Logarithmic Transformation of X data 
# Math.log in Ruby has the base of Euler's number 'e' ~= '2.71828', 
# instead of the base '10'. Just a note.
log_x_data = x_data.map { |x| Math.log(x) }

# Linear Regression using the Logarithmic Transformation
x_vector=log_x_data.to_vector(:scale)
y_vector=y_data.to_vector(:scale)
ds={'x'=>x_vector,'y'=>y_vector}.to_dataset
mlr=Statsample::Regression.multiple(ds,'y')
mlr.summary

# Provides the value of the y-intercept 
#p mlr.constant

# Lists the coefficients of each casual variable. In this case, we have only one--'x'.
#p mlr.coeffs

# The regression output produces the line y = 1 + 2*x, but 
# considering that we transformed x earlier, it really produces
# y = 1 + 2*ln(x).

Try using the 'statsample' gem. You can perform logarithmic, exponential, power, or any other transformation using the example that is provided below. I hope this helps.

require 'statsample'

# Independent Variable
x_data = [Math.exp(1), Math.exp(2), Math.exp(3), Math.exp(4), Math.exp(5)]

# Dependent Variable
y_data = [3, 5, 7, 9, 11]

# Logarithmic Transformation of X data 
# Math.log in Ruby has the base of Euler's number 'e' ~= '2.71828', 
# instead of the base '10'. Just a note.
log_x_data = x_data.map { |x| Math.log(x) }

# Linear Regression using the Logarithmic Transformation
x_vector=log_x_data.to_vector(:scale)
y_vector=y_data.to_vector(:scale)
ds={'x'=>x_vector,'y'=>y_vector}.to_dataset
mlr=Statsample::Regression.multiple(ds,'y')
mlr.summary

# Provides the value of the y-intercept 
#p mlr.constant

# Lists the coefficients of each casual variable. In this case, we have only one--'x'.
#p mlr.coeffs

# The regression output produces the line y = 1 + 2*x, but 
# considering that we transformed x earlier, it really produces
# y = 1 + 2*ln(x).
许一世地老天荒 2024-11-16 16:31:51

我使用这个片段来计算一些回归。第一个参数是包含 x 坐标的数组,第二个参数是包含 y 坐标的数组,最后一个参数是要查找的多项式的次数。不确定这是否是您正在寻找的,但希望它有所帮助。

I used this snippet to work out some regressions. The first parameter is an array containing the x coordinates, the second an array containing the y coordinates and the last is the degree of the polynomial you are looking for. Not sure if it is this what you are looking for, but hopes it helps.

东北女汉子 2024-11-16 16:31:51

我正在维护一个用于非线性最小二乘最小化的 C 库, http://apps.jcns.fz -juelich.de/lmfit,附带 Ruby 的 swig 文件。

I am maintaining a C library for non-linear least squares minimization, http://apps.jcns.fz-juelich.de/lmfit, that comes with swig files for Ruby.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文