问题
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?
回答1:
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.
来源:https://stackoverflow.com/questions/51440999/finding-data-trendlines-with-ruby