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

| 有没有一个Ruby库,可以让我对一组数据进行线性或非线性最小二乘近似。 我想做的是以下几点: 给定一系列[x,y]数据点 针对该数据生成线性或非线性最小二乘近似 该库无需弄清楚是否需要进行线性或非线性近似。库的调用者应该知道他们需要哪种类型的回归 我希望不必尝试移植一些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文件。     

要回复问题请先登录注册