Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decompose time series in Python with statsmodels

I am trying to decompose a time series. The dataset is a matrix of 2x8638.

The code is the following:

import numpy as np
import pandas as pd
import matplotlib.pylab as plt
%matplotlib inline
from matplotlib.pylab import rcParams
rcParams['figure.figsize'] = 15, 6
from pandas.plotting import register_matplotlib_converters
register_matplotlib_converters()

df1 = pd.read_csv("u_x_ts.csv").set_index("0")
df1.head()

enter image description here

from statsmodels.tsa.seasonal import seasonal_decompose
result = seasonal_decompose(df1, model='multiplicative')
result.plot()
plt.show()

Then python returns the error message:

ValueError: Multiplicative seasonality is not appropriate for zero and negative values

I think the statsmodels doesn't support such small values because the values are too small at the beginning of the series. But if anyone knows a way out of this,s problem, I appreciate it.

like image 981
user13053456 Avatar asked Jun 27 '26 14:06

user13053456


1 Answers

If you want to use seasonal_decompose with negatives values, you must use additif model, like this:

from statsmodels.tsa.seasonal import seasonal_decompose
result = seasonal_decompose(df1, model='additif')
result.plot()
plt.show()
like image 91
StAnto Avatar answered Jun 29 '26 02:06

StAnto



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!