Ruby曲线拟合(对数回归)包

8

我正在寻找一个Ruby gem或库,用于进行对数回归(将曲线拟合到对数方程)。我已经尝试了statsample(http://ruby-statsample.rubyforge.org/),但它似乎没有我需要的功能。有人有什么建议吗?


2
我不确定您是否熟悉ruby-toolbox.com,但它帮助我为大多数项目找到了合适的库/插件。 - Maran
3个回答

12

尝试使用 "statsample" 宝石。您可以使用类似的方法执行指数、对数、幂、正弦或任何其他转换。希望这可以帮助您。

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')

# Prints a statistical summary of the regression
print mlr.summary

# Lists 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).

# Bonus: The command below lists the methods contained in the instance variable, so that 
# you can get the R^2, SSE, coefficients, and t-values. I'll leave it commented out for now. 
# p mlr.methods

我在想,如果这段代码是“对数变换”的情况,它会是什么样子。我正在尝试计算一个对数趋势线,但似乎statsample的文档已经过时了。有什么想法吗? - Igor Escobar

4

0

试试Rumale和Numo::NArray https://github.com/yoshoku/rumale

enter image description here

Rumale(Ruby机器学习)是一个Ruby语言的机器学习库。Rumale提供了类似于Python中Scikit-Learn的机器学习算法接口。Rumale支持线性/核支持向量机、逻辑回归、线性回归、岭回归、Lasso回归、分解机、朴素贝叶斯、决策树、AdaBoost、梯度树提升、随机森林、Extra-Trees、K最近邻分类器、K-Means、K-Medoids、高斯混合模型、DBSCAN、幂迭代聚类、多维缩放、t-SNE、主成分分析和非负矩阵分解等机器学习算法。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接