seasonal_decompose: operands could not be broadcast together with shapes on a series

99封情书 提交于 2019-12-07 03:27:07

问题


I know there are many questions on this topic, but none of them helped me to solve this problem. I'm really stuck on this.

With a simple series:

0
2016-01-31  266
2016-02-29  235
2016-03-31  347
2016-04-30  514
2016-05-31  374
2016-06-30  250
2016-07-31  441
2016-08-31  422
2016-09-30  323
2016-10-31  168
2016-11-30  496
2016-12-31  303

import statsmodels.api as sm
logdf = np.log(df[0])
decompose = sm.tsa.seasonal_decompose(logdf,freq=12, model='additive')
decomplot = decompose.plot()

i keep getting: ValueError: operands could not be broadcast together with shapes (12,) (14,)

I've tried pretty much everything, passing only logdf.values, passing a non-log series. It doesn't work. Numpy and statsmodel versions:

print(statsmodels.__version__)
print(pd.__version__)
print(np.__version__)
0.6.1
0.18.1
1.11.3

回答1:


As @yoonforh pointed, in my case this was fixed by setting the freq parameter to less than the time series length. E.g. if your time series ts looks like this:

2014-01-01    0.0
2014-02-01    0.0
2014-03-01    1.0
2014-04-01    1.0
2014-05-01    0.0
2014-06-01    1.0
2014-07-01    1.0
2014-08-01    0.0
2014-09-01    0.0
2014-10-01    1.0
2014-11-01    0.0
2014-12-01    0.0

the shape is

(12,)

so this will give the error as per above:

seasonal_decompose(ts, freq=12, model='additive')

but if I try freq=11 or any other int less than 12, e.g.

seasonal_decompose(ts, freq=11, model='additive')

this works




回答2:


i noticed that with newer pandas and statsmodel versions it seems to work.

Given a series:

2016-01-03    8.326275
2016-01-10    8.898229
2016-01-17    8.754792
2016-01-24    8.658172
2016-01-31    8.731659
2016-02-07    9.047233
2016-02-14    8.799662
2016-02-21    8.783549
2016-02-28    8.782783
2016-03-06    9.081825
2016-03-13    8.737934
2016-03-20    8.658693
2016-03-27    8.666475
2016-04-03    9.029178
2016-04-10    8.781555
2016-04-17    8.720787
2016-04-24    8.633909
2016-05-01    8.937744
2016-05-08    8.804925
2016-05-15    8.766862
2016-05-22    8.651899
2016-05-29    8.653645
...

And pd/sm version:

statsmodels.__version__ 0.8.0
pandas.__version__ 0.20.1

This is the result:

import statsmodels.api as sm
logdf = np.log(df_series)
decompose = sm.tsa.seasonal_decompose(logdf, model='additive', filt=None, freq=1, two_sided=True)
decompose.plot()

I hope this could solve your problem too.



来源:https://stackoverflow.com/questions/42364901/seasonal-decompose-operands-could-not-be-broadcast-together-with-shapes-on-a-se

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!