gpt4 book ai didi

python - 在 Python 中使用季节性分解时我做错了什么?

转载 作者:太空宇宙 更新时间:2023-11-04 05:23:22 24 4
gpt4 key购买 nike

我有一个以月为间隔的小时间序列。我想绘制它然后分解为季节性、趋势、残差。我首先将 csv 导入 pandas,然后只绘制工作正常的时间序列。我关注This教程和我的代码是这样的:

%matplotlib inline
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
import pandas as pd

ali3 = pd.read_csv('C:\\Users\\ALI\\Desktop\\CSV\\index\\ZIAM\\ME\\ME_DATA_7_MONTH_AVG_PROFIT\\data.csv',
names=['Date', 'Month','AverageProfit'],
index_col=['Date'],
parse_dates=True)

\* Delete month column which is a string */
del ali3['Month']


ali3
plt.plot(ali3)

Data Frame

在这个阶段,我尝试像这样进行季节性分解:

import statsmodels.api as sm 
res = sm.tsa.seasonal_decompose(ali3.AverageProfit)
fig = res.plot()

这会导致以下错误:

---------------------------------------------------------------------------
ValueError Traceback (most recent call last)
<ipython-input-41-afeab639d13b> in <module>()
1 import statsmodels.api as sm
----> 2 res = sm.tsa.seasonal_decompose(ali3.AverageProfit)
3 fig = res.plot()

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\statsmodels\tsa\seasonal.py in seasonal_decompose(x, model, filt, freq)
86 filt = np.repeat(1./freq, freq)
87
---> 88 trend = convolution_filter(x, filt)
89
90 # nan pad for conformability - convolve doesn't do it

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\statsmodels\tsa\filters\filtertools.py in convolution_filter(x, filt, nsides)
287
288 if filt.ndim == 1 or min(filt.shape) == 1:
--> 289 result = signal.convolve(x, filt, mode='valid')
290 elif filt.ndim == 2:
291 nlags = filt.shape[0]

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\signal\signaltools.py in convolve(in1, in2, mode)
468 return correlate(volume, kernel[slice_obj].conj(), mode)
469 else:
--> 470 return correlate(volume, kernel[slice_obj], mode)
471
472

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\signal\signaltools.py in correlate(in1, in2, mode)
158
159 if mode == 'valid':
--> 160 _check_valid_mode_shapes(in1.shape, in2.shape)
161 # numpy is significantly faster for 1d
162 if in1.ndim == 1 and in2.ndim == 1:

C:\Users\D063375\AppData\Local\Continuum\Anaconda2\lib\site-packages\scipy\signal\signaltools.py in _check_valid_mode_shapes(shape1, shape2)
70 if not d1 >= d2:
71 raise ValueError(
---> 72 "in1 should have at least as many items as in2 in "
73 "every dimension for 'valid' mode.")
74

ValueError: in1 should have at least as many items as in2 in every dimension for 'valid' mode.

任何人都可以阐明我做错了什么,我该如何解决?非常感谢。

编辑这就是数据框的样子

Date            AverageProfit

2015-06-01 29.990231
2015-07-01 26.080038
2015-08-01 25.640862
2015-09-01 25.346447
2015-10-01 27.386001
2015-11-01 26.357709
2015-12-01 25.260644

最佳答案

您有 7 个数据点,这对于执行平稳性分析来说通常是一个非常小的数字。

您没有足够的积分来使用季节性分解。要看到这一点,您可以连接您的数据以创建一个扩展的时间序列(只是重复接下来几个月的数据)。让 extendedData 成为这个扩展数据框,让 data 成为你的原始数据。

data.plot()

enter image description here

extendedData.plot()

enter image description here

res = sm.tsa.seasonal_decompose(extendedData.interpolate())
res.plot()

enter image description here

季节性估计的频率 (freq) 是根据数据自动估计的,可以手动指定。


您可以尝试进行一阶差分:生成一个新的时间序列,从前一个数据值中减去每个数据值。在您的情况下,它看起来像这样:

enter image description here

接下来可以应用平稳性测试,如解释的那样here

关于python - 在 Python 中使用季节性分解时我做错了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39590648/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com