是否有一个 Ruby 库允许我对一组数据进行线性或非线性最小二乘逼近。
我想做的是以下几点:
- 给定一系列 [x,y] 数据点
- 针对该数据生成线性或非线性最小二乘近似
- 该库不必弄清楚它是否需要进行线性或非线性近似。库的调用者应该知道他们需要什么类型的回归
我不想尝试移植一些 C/C++/Java 库来获得这个功能,所以我希望有一些现有的 Ruby 库可以使用。
是否有一个 Ruby 库允许我对一组数据进行线性或非线性最小二乘逼近。
我想做的是以下几点:
我不想尝试移植一些 C/C++/Java 库来获得这个功能,所以我希望有一些现有的 Ruby 库可以使用。
尝试使用“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).
我使用这个片段来计算一些回归。第一个参数是包含 x 坐标的数组,第二个参数是包含 y 坐标的数组,最后一个参数是您要查找的多项式的次数。不确定这是否是您正在寻找的,但希望它有所帮助。
我正在维护一个用于非线性最小二乘最小化的 C 库http://apps.jcns.fz-juelich.de/lmfit,它带有 Ruby 的 swig 文件。