Остаточный график в функции seasonal_decompose на Python не отображается должным образом

#python #dataframe #matplotlib #stl-decomposition

#питон #фрейм данных #matplotlib #stl-декомпозиция

Вопрос:

Остаточный график не отображается должным образом на моем графике. Я не могу понять, в чем может быть проблема. Пожалуйста, нужна помощь с вводом описания изображения здесь. Есть какая-то проблема с осью. Я извлекаю данные о COVID 19 и строю данные первого порядка (сделанный стационарный набор). Я удалил все значения nan.

формат данных date value_diff 268 2020-10-16 745,0 269 2020-10-17 428,0 270 2020-10-18 465,0

 ecomposition = seasonal_decompose(data_set_3, model='additive', period=7)

    trend = decomposition.trend
    seasonal = decomposition.seasonal
    residual = decomposition.resid

    plt.subplot(411)
    plt.plot(data_set_3, label='Original')
    plt.legend(loc='best')

    plt.subplot(412)
    plt.plot(trend, label='Trend')
    plt.legend(loc='best')

    plt.subplot(413)
    plt.plot(seasonal, label='Seasonality')
    plt.legend(loc='best')

    plt.subplot(414)
    plt.plot(residual, label='Residuals')
    plt.legend(loc='best')

    plt.tight_layout()

 

Ответ №1:

добавьте частоту и периоды в вашу seasonal_decomposition для сглаживания. это будет работать.

 from pandas_datareader import data as pdr
from statsmodels.graphics import tsaplots
import statsmodels.api as sm

current_date=datetime.datetime.now()
start_date=datetime.datetime(current_date.year,1,1)
df = pdr.get_data_yahoo("MSFT",start_date,current_date).reset_index()

decomposition=sm.tsa.seasonal_decompose(x=df['High'],model='additive',         extrapolate_trend='freq', period=30)
decomposition.plot()
plt.show()

decomposition_trend=decomposition.trend
ax= decomposition_trend.plot(figsize=(14,2))
ax.set_xlabel('Date')
ax.set_ylabel('Trend of time series')
ax.set_title('Trend values of the time series')
plt.show()

decomposition_residual=decomposition.resid
ax= decomposition_residual.plot(figsize=(14,2))
ax.set_xlabel('Date')
ax.set_ylabel('Residual of time series')
ax.set_title('Residual values of the time series')
plt.show()

decomposition_trend=decomposition.trend
ax= decomposition_trend.plot(figsize=(14,2))
ax.set_xlabel('Date')
ax.set_ylabel('Trend of time series')
ax.set_title('Trend values of the time series')
plt.show()