python价格趋势软件,如何在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).

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.

解决方案

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

AR9yD.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值