简体   繁体   中英

Finding data trendlines with Ruby?

I have a dataset with user session numbers from my site which looks like:

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]
...

And so on.

I would like to find out whether the page has a positive or negative trendline in terms of growth, however I would also like to get the pages that are growing/falling beyond a certain threshold.

Python has a ton of solutions and libs for this kind of task, yet Ruby has only one gem ( trendline ) which has no code in it. Before I start learning how to do this using math maybe somebody has a working solution?

Finding the math formula for trend lines, you can define your custom method pretty easily. For example, following this https://math.stackexchange.com/questions/204020/what-is-the-equation-used-to-calculate-a-linear-trendline , I monkey patched the Array class.

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}

The slope gives you the growth: the sign indicates the direction ( + is growing, - is decreasing), the value indicates how fast.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM