Kats: An Intro to Kit to Analyze Time Series in Python 🐈
What is Kats? How to use Kats to Analyze Time Series. Examples of features of Kats.
Kats is a Python package developed by Facebook’s Core Data Science team. It stands for “Kats: Kit to Analyze Time Series.” Kats provides various tools and algorithms for time series analysis and forecasting. It is designed to be user-friendly and efficient, making it easier for analysts and data scientists to work with time series data.
History
Initial Release: The Kats package was initially released by Facebook in March 2020. It was introduced as an open-source tool specifically designed for time series analysis and forecasting.
Since its release, the Kats package has seen significant development and expansion. The Facebook team has continuously added new features, algorithms, and functionalities to enhance its capabilities for time series analysis.
Kats is an open-source project, and as such, it has benefited from contributions from the wider community. Developers and data scientists have provided bug fixes, feature enhancements, and additional algorithms, making Kats a collaborative effort.
The Kats package has been actively maintained, with regular updates and improvements. The Facebook team has addressed issues, incorporated user feedback, and introduced new features to make the package more robust and user-friendly.
Over time, Kats has gained popularity among data scientists and analysts working with time series data. Its ease of use, comprehensive functionality, and integration with other Python libraries have contributed to its adoption within the data science community.
Features
Time series forecasting: Kats provides different forecasting models such as ARIMA, Prophet, and various state-space models. These models allow users to make predictions and estimate uncertainties for future time steps.
Anomaly detection: Kats includes algorithms for detecting anomalies in time series data. It provides techniques such as robust statistical measures and Gaussian Mixture Models for identifying unusual patterns or outliers.
Change-point detection: This feature helps identify significant changes or shifts in time series data. Kats offers algorithms like Bayesian Change Point Detection and CUSUM (Cumulative Sum) for detecting these points.
Trend analysis: Kats provides tools for analyzing trends in time series data. Users can perform trend decomposition, including extracting seasonality and long-term trends.
Get Manoj Das’s stories in your inbox
Join Medium for free to get updates from this writer.
Model diagnostics: The package includes utilities for evaluating the performance of time series models. Users can assess model accuracy, visualize residuals, and perform other diagnostic tasks.
Forecasting with Kats
Kats support the following 10 base forecasting models:
- Linear
- Quadratic
- ARIMA
- SARIMA
- Holt-Winters
- Prophet
- AR-Net
- LSTM
- Theta
- VAR
Feature Extraction with Kats
Kats provide the TsFeatures module to calculate a set of meaningful features for a time series, including:
- STL (Seasonal and Trend decomposition using Loess) Features -
- Strength of Seasonality
- Strength of Trend
- Spikiness
- Linearity
- Amount of Level Shift
- Presence of Flat Segments
- ACF and PACF Features
- Hurst Exponent
- ARCH Statistic
Install Kats
To install the Kats package, you can use pip, the package installer for Python. Open your command-line interface and execute the following command:
pip install katsExample
Initiate TimeSeriesData
We will use the air_passenger and multi_ts datasets to demonstrate how to create a TimeSeriesData object for univariate and multivariate time series, respectively.
import numpy as np
import pandas as pd
from datetime import datetime, timedelta
import matplotlib.pyplot as plt
import warnings
warnings.filterwarnings('ignore')
from kats.consts import TimeSeriesData
air_passengers_df = pd.read_csv("air_passengers.csv")
air_passengers_df.columns = ["time", "value"]
air_passengers_df.head()multi_ts_df = pd.read_csv("multi_ts.csv", index_col=0)
multi_ts_df.columns = ["time", "v1", "v2"]
multi_ts_df.head()Here we construct TimeSeriesData objects:
air_passengers_ts = TimeSeriesData(air_passengers_df)
multi_ts = TimeSeriesData(multi_ts_df)
# check that the type of the data is a "TimeSeriesData" object for both cases
print(type(air_passengers_ts))
print(type(multi_ts))# For the air_passengers TimeSeriesData, check that both time and value are pd.Series
print(type(air_passengers_ts.time))
print(type(air_passengers_ts.value))# For the multi_ts TimeSeriesData, time is a pd.Series and value is a pd.DataFrame
print(type(multi_ts.time))
print(type(multi_ts.value))Plotting
air_passengers_ts.plot(cols=['value'])
plt.show()multi_ts.plot(cols=['v1','v2'])
plt.show()Outlier Detection
This usually refers to a abnormal spike in a time series data, which can be detected with OutlierDetector.
# deep copy the air_passenger_df
air_passengers_outlier_df = air_passengers_df.copy(deep=True)
# manually add outlier on the date of '1950-12-01'
air_passengers_outlier_df.loc[air_passengers_outlier_df.time == '1950-12-01','value']*=5
# manually add outlier on the date of '1959-12-01'
air_passengers_outlier_df.loc[air_passengers_outlier_df.time == '1959-12-01', 'value']*=4
# visualize the raw data
air_passengers_outlier_df.plot(x='time', y='value', figsize=(15,8))
plt.show()from kats.detectors.outlier import OutlierDetector
# transform the outlier data into `TimeSeriesData` Object
air_passengers_outlier_ts = TimeSeriesData(air_passengers_outlier_df)
ts_outlierDetection = OutlierDetector(air_passengers_outlier_ts, 'additive') # call OutlierDetector
ts_outlierDetection.detector() # apply OutlierDetector
# look at the outliers that the algorithm found
ts_outlierDetection.outliers[0]- No Interpolation: outlier data points will be replaced with NaN values
- With Interpolation: outlier data points will be replaced with linear interploation values
air_passengers_ts_outliers_removed = ts_outlierDetection.remover(interpolate = False) # No interpolation
air_passengers_ts_outliers_interpolated = ts_outlierDetection.remover(interpolate = True) # With interpolation
fig, ax = plt.subplots(figsize=(20,8), nrows=1, ncols=2)
air_passengers_ts_outliers_removed.to_dataframe().plot(x = 'time',y = 'y_0', ax= ax[0])
ax[0].set_title("Outliers Removed : No interpolation")
air_passengers_ts_outliers_interpolated.to_dataframe().plot(x = 'time',y = 'y_0', ax= ax[1])
ax[1].set_title("Outliers Removed : With interpolation")
plt.show()Feature Extraction
We will see how to use TsFeatures get the features for the air_passenger data set.
# Initiate feature extraction class
from kats.tsfeatures.tsfeatures import TsFeatures
tsFeatures = TsFeatures()
features_air_passengers = TsFeatures().transform(air_passengers_ts)
# features_air_passengers
features_air_passengersProphet model
# import the param and model classes for Prophet model
from kats.models.prophet import ProphetModel, ProphetParams# create a model param instance
params = ProphetParams(seasonality_mode='multiplicative') # additive mode gives worse results# create a prophet model instance
m = ProphetModel(air_passengers_ts, params)# fit model simply by calling m.fit()
m.fit()# make prediction for next 30 month
fcst = m.predict(steps=30, freq="MS")
# visualize the results with uncertainty intervals
m.plot()— — —
Why did the time series analysis expert bring their cat to work?
Because they heard Kats had the purr-fect forecast for time series data, and they wanted to bring a feline touch to their data analysis
🙂🙂🙂






