Kats: a Generalizable Framework to Analyze Time Series Data in Python
By: Khuyen Tran
Tags: Time Series Analysis, Data Analysis, Python, Data Science, Time Series Forecasting

Now let’s analyze the StackOverflow question count related to Python. The data is split into a train and a test set to evaluate the forecast.

Turn Data into Time Series

Start with constructing a time series object. We use time_col_name='month' to specify the time column.

To plot the data, call the plot method:

Image by Author

Cool! It looks like the number of questions about Python goes up over years. Can we predict its trend over the next 30 days? Yes, we can do that with Kats.

Forecasting

Kats currently supports the following 10 forecasting models:

Linear

Quadratic

ARIMA

SARIMA

Holt-Winters

Prophet

AR-Net

LSTM

Theta

VAR

Let’s try two of these models.

Facebook Prophet

Start with forecasting using the Prophet model:

Visualize the forecast:

Image by Author

Cool! Let’s evaluate the forecast by comparing it with the test data.

Image by Author

The forecast seems to follow the observations well!

Holt-Winters

The next model we will try is Holt-Winters. The Holt-Winters method is a method to capture seasonality. Below is how to use the Holt-Winters method with Kats.

Image by Author

Find the API of all Kats’ models here.

Detect Change Point

Have you ever wished to pinpoint the time when a statistically significant shift of the means in your time series occurred?

Image by Author

Kats allows you to detect change points with the CUSUM algorithm. Cusum is a method to detect an up/down shift of means in a time series.

Let’s see how to detect change points in Kats.

The change point is on 2013-02-01 00:00:00

Image by Author

Cool! Let’s try to detect the change points of other categories of StackOverflow’s question count.

Start with creating a function to detect the change point provided a topic.

Machine learning:

The change point is on 2015-12-01 00:00:00

Image by Author

Neural network:

The change point is on 2015-11-01 00:00:00

Image by Author

Deep learning:

The change point is on 2016-09-01 00:00:00

Image by Author

Jupyter:

The change point is on 2015-07-01 00:00:00

Image by Author

Plotly:

The change point is on 2016-02-01 00:00:00

Image by Author

Do you see the general patterns? The shift of means of question count for the categories related to data science, machine learning, and deep learning on StackOverflow seems to be around 2015–2016!

What happens around this period of time that attracts developers’ attention? Post in the comment if you have the answer for this.

Outlier Detection

What do you see when looking at the time series of NLP?

Image by Author

Yes, you’re right. There is a drop in the question count of NLP from 2018 to 2019.

This drop in the question count is an outlier. It is important to detect outliers since they can cause problems in downstream processing.

However, it is not always efficient and easy to find outliers by looking at the data. Luckily, Kats also allows you to detect outliers in time series!

Detecting outliers with Kats only takes 2 lines of code.

The outliers range from 2018-01-01 00:00:00 to 2019-03-01 00:00:00

Cool! The result confirms what we saw from the plot above.

Time Series Features

There are other features in a time series that you might be interested in finding besides its statistics such as linearity, trend strength, seasonality strength, seasonality parameters, etc.

Kats allows you to find out important information about time series features with TsFeatures :

Awesome!

Conclusion

Congratulations! You have just learned how to use Kats to forecast, detect change points, detect outliers, and extract time series features. I hope this article will give you the motivation to solve interesting problems and extract valuable information from your time series.

Feel free to play with the source code here: