How can I detect if trend is increasing or decreasing in time series?(如何检测时间序列中趋势是增加还是减少?)
问题描述
我有几个星期的销售单位数据
I have few weeks data with units sold given
xs[weeks] = [1,2,3,4]
ys['Units Sold'] = [1043,6582,5452,7571]
从给定的系列中,我们可以看到虽然从 xs[2] 到 xs[3] 有所下降,但总体趋势正在增加.如何检测小时间序列数据集中的趋势.
from the given series, we can see that although there is a drop from xs[2] to xs[3] but overall the trend is increasing. How to detect the trend in small time series dataset.
寻找直线的坡度是最好的方法吗?以及如何在python中计算直线的斜角?
Is finding a slope for the line is the best way? And how to calculate slope angle of a line in python?
推荐答案
我遇到了你今天面临的同样问题.为了检测趋势,我找不到特定的函数来处理这种情况.
I have gone through the same issue that you face today. In order to detect the trend, I couldn't find a specific function to handle the situation.
我发现了一个非常有用的函数,即 numpy.polyfit()
:
I found a really helpful function ie, numpy.polyfit()
:
numpy.polyfit(x, y, deg, rcond=None, full=False, w=None, cov=False)
[查看本官方文档]
你可以这样使用函数
def trenddetector(list_of_index, array_of_data, order=1):
result = np.polyfit(list_of_index, list(array_of_data), order)
slope = result[-2]
return float(slope)
此函数返回一个浮点值,指示数据的趋势,您也可以通过类似的方式对其进行分析.
This function returns a float value that indicates the trend of your data and also you can analyze it by something like this.
例如,
如果斜率是 +ve 值 -->增加趋势
if the slope is a +ve value --> increasing trend
如果斜率是 -ve 值 -->下降趋势
if the slope is a -ve value --> decreasing trend
如果斜率是零值 -->没有趋势
if the slope is a zero value --> No trend
使用此功能,根据您的问题找出正确的阈值并将其作为条件.
Play with this function and find out the correct threshold as per your problem and give it as a condition.
解决方案示例代码
import numpy as np
def trendline(index,data, order=1):
coeffs = np.polyfit(index, list(data), order)
slope = coeffs[-2]
return float(slope)
index=[1,2,3,4]
List=[1043,6582,5452,7571]
resultent=trendline(index,List)
print(resultent)
结果
1845.3999999999999
1845.3999999999999
根据此输出,结果远大于零,因此表明您的数据正在稳步增加.
As per this output, The result is much greater than zero so it shows your data is increasing steadily.
这篇关于如何检测时间序列中趋势是增加还是减少?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何检测时间序列中趋势是增加还是减少?


- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- python-m http.server 443--使用SSL? 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- 沿轴计算直方图 2022-01-01