How to calculate the steepness of a trend in python

佐手、 提交于 2019-12-24 06:49:57

问题


I am using the regression slope as follows to calculate the steepness (slope) of the trend.

Scenario 1: For example, consider I am using sales figures (x-axis: 1, 4, 6, 8, 10, 15) for 6 days (y-axis).

from sklearn.linear_model import LinearRegression
regressor = LinearRegression()
X = [[1], [4], [6], [8], [10], [15]]
y = [1, 2, 3, 4, 5, 6]
regressor.fit(X, y)
print(regressor.coef_)

This gives me 0.37709497

Scenario 2: When I run the same program for a different sale figure (e.g., 1, 2, 3, 4, 5, 6) I get the results as 1.

However, you can see that sales is much productive in scenario 1, but not in scenario 2. However, the slope I get for scenario 2 is higher than scenario 1.

Therefore, I am not sure if the regression slope captures what I require. Is there any other approach I can use instead to calculate the sleepness of the trend slope.

I am happy to provide more details if needed.


回答1:


I believe the problem is your variables are switched. If you want to track sales performance over time, you should perform the regression the other way around. You can invert the slopes you've calculated to get the correct values, which will show higher sales performance in case 1.

1 / 0.377 = 2.65

Here is a visualization of your data:

import matplotlib.pyplot as plt

days = [1,2,3,4,5,6]
sales1 = [1,4,6,8,10,15]
sales2 = [1,2,3,4,5,6]

df = pd.DataFrame({'days': days, 'sales1': sales1, 'sales2': sales2})
df = df.set_index('days')
df.plot(marker='o', linestyle='--')



来源:https://stackoverflow.com/questions/55274024/how-to-calculate-the-steepness-of-a-trend-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!