用Ruby查找数据趋势线?

用Ruby查找数据趋势线?,ruby,statistics,analytics,trendline,Ruby,Statistics,Analytics,Trendline,我的网站上有一个包含用户会话号的数据集,看起来像: page_1 = [4,2,4,1,2,6,3,2,1,6,2,7,0,0,0] page_2 = [6,3,2,3,5,7,9,3,1,6,1,6,2,7,8] ... 等等 我想知道页面的增长趋势是积极的还是消极的,但是我也希望看到页面的增长/下降超过某个阈值 Python有大量的解决方案和libs用于此类任务,而Ruby只有一个gem,其中没有代码。在我开始学习如何使用数学之前,也许有人有一个可行的解决方案?找到趋势线的数学公式,您可以

我的网站上有一个包含用户会话号的数据集,看起来像:

page_1 = [4,2,4,1,2,6,3,2,1,6,2,7,0,0,0]
page_2 = [6,3,2,3,5,7,9,3,1,6,1,6,2,7,8]
...
等等

我想知道页面的增长趋势是积极的还是消极的,但是我也希望看到页面的增长/下降超过某个阈值


Python有大量的解决方案和libs用于此类任务,而Ruby只有一个gem,其中没有代码。在我开始学习如何使用数学之前,也许有人有一个可行的解决方案?

找到趋势线的数学公式,您可以非常轻松地定义自定义方法。 例如,在此之后,我修补了Array类

class Array

  def trend_line
    points = map.with_index { |y, x| [x+1, y] }
    n = points.size
    summation_xy = points.map{ |e| e[0]*e[1] }.inject(&:+)
    summation_x = points.map{ |e| e[0] }.inject(&:+)
    summation_y = points.map{ |e| e[1] }.inject(&:+)
    summation_x2 = points.map{ |e| e[0]**2 }.inject(&:+)
    slope = ( n * summation_xy - summation_x * summation_y ) / ( n * summation_x2 - summation_x**2 ).to_f
    offset = ( summation_y - slope * summation_x ) / n.to_f
    {slope: slope, offset: offset}
  end

end

p page_1.trend_line #=> {:slope=>-0.1357142857142857, :offset=>3.7523809523809524}
p page_2.trend_line #=> {:slope=>0.1, :offset=>3.8}

坡度为您提供了增长:符号表示方向+正在增长,-正在减小,值表示速度有多快。

找到趋势线的数学公式,您可以非常轻松地定义自定义方法。 例如,在此之后,我修补了Array类

class Array

  def trend_line
    points = map.with_index { |y, x| [x+1, y] }
    n = points.size
    summation_xy = points.map{ |e| e[0]*e[1] }.inject(&:+)
    summation_x = points.map{ |e| e[0] }.inject(&:+)
    summation_y = points.map{ |e| e[1] }.inject(&:+)
    summation_x2 = points.map{ |e| e[0]**2 }.inject(&:+)
    slope = ( n * summation_xy - summation_x * summation_y ) / ( n * summation_x2 - summation_x**2 ).to_f
    offset = ( summation_y - slope * summation_x ) / n.to_f
    {slope: slope, offset: offset}
  end

end

p page_1.trend_line #=> {:slope=>-0.1357142857142857, :offset=>3.7523809523809524}
p page_2.trend_line #=> {:slope=>0.1, :offset=>3.8}

斜率表示增长:符号表示方向+正在增长,-正在减少,值表示速度有多快。

看这里:什么意思,它没有代码?是的,自述很好,但是看看回购协议;这可能会对你有所帮助,或者说很少只有一个gem,但它们肯定都专注于严肃的统计数据,而且比趋势线看起来要复杂得多。这里的意思是:什么意思,它没有代码?是的,自述很好,但是看看回购协议;这可能会对你有所帮助,或者说很少只有一个创业板,但它们肯定都专注于严肃的统计数据,而且比趋势线看起来要复杂得多