【问题标题】:Ruby Library for doing Linear or NonLinear Least Squares Approximation?用于进行线性或非线性最小二乘逼近的 Ruby 库?
【发布时间】:2019-08-10 09:37:10
【问题描述】:

是否有一个 Ruby 库可以让我对一组数据进行线性或非线性最小二乘逼近。

我想做的是:

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

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

【问题讨论】:

标签: ruby linear-regression least-squares


【解决方案1】:

尝试使用“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).

【讨论】:

    【解决方案2】:

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

    【讨论】:

      【解决方案3】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-04-02
        • 2018-12-27
        相关资源
        最近更新 更多