Get Started

Seasonal decompose

You can think of a time series as being composed of trend, seasonal and residual components. This can be a good way to think about the data when you go about modeling it. If you know the period of the time series you can decompose it into these components.

In this exercise you will decompose a time series showing the monthly milk production per cow in the USA. This will give you a clearer picture of the trend and the seasonal cycle. Since the data is monthly you will guess that the seasonality might be 12 time periods, however this won't always be the case.

The milk production time series has been loaded in to the DataFrame milk_production and is available in your environment.

This is a part of the course

“ARIMA Models in Python”

View Course

Exercise instructions

  • Import the seasonal_decompose() function from statsmodels.tsa.seasonal.
  • Decompose the 'pounds_per_cow' column of milk_production using an additive model and period of 12 months.
  • Plot the decomposition.

Hands-on interactive exercise

Have a go at this exercise by completing this sample code.

# Import seasonal decompose
from ____ import ____

# Perform additive decomposition
decomp = seasonal_decompose(___, 
                            period=____)

# Plot decomposition
____
plt.show()

This exercise is part of the course

ARIMA Models in Python

AdvancedSkill Level
4.8+
22 reviews

Learn about ARIMA models in Python and become an expert in time series analysis.

In this final chapter, you'll learn how to use seasonal ARIMA models to fit more complex data. You'll learn how to decompose this data into seasonal and non-seasonal parts and then you'll get the chance to utilize all your ARIMA tools on one last global forecast challenge.

Exercise 1: Seasonal time seriesExercise 2: Seasonal decompose
Exercise 3: Seasonal ACF and PACFExercise 4: SARIMA modelsExercise 5: Fitting SARIMA modelsExercise 6: Choosing SARIMA orderExercise 7: SARIMA vs ARIMA forecastsExercise 8: Automation and savingExercise 9: Automated model selectionExercise 10: Saving and updating modelsExercise 11: SARIMA and Box-JenkinsExercise 12: Multiplicative vs additive seasonalityExercise 13: SARIMA model diagnosticsExercise 14: SARIMA forecastExercise 15: Congratulations!

What is DataCamp?

Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.

Start Learning for Free