简体   繁体   English

如何计算python中趋势的陡度

[英]How to calculate the steepness of a trend in python

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). 场景1:例如,考虑我使用销售数字(x轴: 1, 4, 6, 8, 10, 15 )6天(y轴)。

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 这给了我0.37709497

Scenario 2: When I run the same program for a different sale figure (eg, 1, 2, 3, 4, 5, 6 ) I get the results as 1 . 场景2:当我为不同的销售数字(例如, 1, 2, 3, 4, 5, 6运行相同的程序时,我得到的结果为1

However, you can see that sales is much productive in scenario 1 , but not in scenario 2 . 但是,您可以看到scenario 1中的sales效率很高,但scenario 2没有。 However, the slope I get for scenario 2 is higher than scenario 1 . 但是, scenario 2得到的斜率高于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. 如果需要,我很乐意提供更多细节。

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中显示更高的销售业绩。

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='--')

数据可视化

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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