我有一个小的时间序列,每月间隔。我想绘制它,然后分解为季节性,趋势,残差。首先,我将csv导入到大熊猫中,然后绘制出可以正常工作的时间序列。我遵循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()


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

extendedData.plot()


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

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


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

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



您可以尝试采取第一个区别:生成一个新的时间序列,将前一个值减去每个数据值。在您的情况下,它看起来像这样:

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

接下来可以应用平稳性测试,如here所述

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

10-12 14:02