如何在python中计算趋势的陡度 [英] How to calculate the steepness of a trend in python

查看:506
本文介绍了如何在python中计算趋势的陡度的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用如下的回归斜率来计算趋势的陡度(斜率).

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

方案1: 例如,假设我使用了6天(y轴)的销售数据(x轴:1, 4, 6, 8, 10, 15).

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

这给了我0.37709497

方案2: 当我针对不同的销售数字(例如1, 2, 3, 4, 5, 6)运行相同的程序时,得到的结果为1.

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.

但是,您可以看到salesscenario 1中非常有生产力,而在scenario 2中却没有.但是,我为scenario 2获得的斜率高于scenario 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='--')

这篇关于如何在python中计算趋势的陡度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆