Miscellaneous

Facebook’s Kats toolkit – A framework to perform time series analysis

Kats Python tool For time series analysis.

What is Kats Toolkit

Kats toolkit is a lightweight, user-friendly, and generalizable framework for performing time series analysis. It is a toolkit for analysing time series data. When it comes to understanding the most important statistics and characteristics, spotting regressions and anomalies, and predicting future trends, time series analysis is a crucial part of Data Science and Engineering work in industry.

time series analysis tool kats
Source : Github

With Kats, time series analysis can be done all in one place, including detection, forecasting, feature extraction and embedding, multivariate analysis, etc. Facebook’s Infrastructure Data Science team has released Kats. It can be downloaded from PyPI.

Features

Below you can see the 4 best features the kats framework toolkit have to offer.

Forecasting

Kats offers a comprehensive set of tools for forecasting, including backtesting, hyperparameter tuning, ensembling, a selfsupervised learning (meta-learning) model, and empirical prediction intervals.

Example
import pandas as pd

from kats.consts import TimeSeriesData
from kats.models.prophet import ProphetModel, ProphetParams

# take `air_passengers` data as an example
air_passengers_df = pd.read_csv(
    "../kats/data/air_passengers.csv",
    header=0,
    names=["time", "passengers"],
)

# convert to TimeSeriesData object
air_passengers_ts = TimeSeriesData(air_passengers_df)

# 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")

Detection

Seasonalities, outliers, change points, and slow trend changes are just a few of the patterns that Kats supports in its functionalities for pattern detection on time series data.

Example
# import packages
import numpy as np
import pandas as pd

from kats.consts import TimeSeriesData
from kats.detectors.cusum_detection import CUSUMDetector

# simulate time series with increase
np.random.seed(10)
df_increase = pd.DataFrame(
    {
        'time': pd.date_range('2019-01-01', '2019-03-01'),
        'increase':np.concatenate([np.random.normal(1,0.2,30), np.random.normal(2,0.2,30)]),
    }
)

# convert to TimeSeriesData object
timeseries = TimeSeriesData(df_increase)

# run detector and find change points
change_points = CUSUMDetector(timeseries).detector()

Time series feature

The Kats time series feature (TSFeature) extraction module can generate 65 features with precise statistical definitions that can be used in the majority of machine learning (ML) models, including regression and classification.

Example
# Initiate feature extraction class
import pandas as pd
from kats.consts import TimeSeriesData
from kats.tsfeatures.tsfeatures import TsFeatures

# take `air_passengers` data as an example
air_passengers_df = pd.read_csv(
    "../kats/data/air_passengers.csv",
    header=0,
    names=["time", "passengers"],
)

# convert to TimeSeriesData object
air_passengers_ts = TimeSeriesData(air_passengers_df)

# calculate the TsFeatures
features = TsFeatures().transform(air_passengers_ts)

Utilities

A selection of helpful utilities, including time series simulators, are also offered by Kats.

Important links
Homepage: https://facebookresearch.github.io/Kats/
Kats Python package: https://pypi.org/project/kats/0.1.0/
Facebook Engineering Blog Post: https://engineering.fb.com/2021/06/21/open-source/kats/
Source code repository: https://github.com/facebookresearch/kats
Contributing: https://github.com/facebookresearch/Kats/blob/master/CONTRIBUTING.md
Tutorials: https://github.com/facebookresearch/Kats/tree/master/tutorials

Kats Installation in Python

Kats is on PyPI, so you can use pip to install it.

pip install --upgrade pip
pip install kats

If you need only a small subset of Kats, you can install a minimal version of Kats with

MINIMAL_KATS=1 pip install kats

which omits many dependencies (everything in test_requirements.txt). However, this will disable many functionalities and cause import kats to log warnings. See setup.py for full details and options.

Janvi Rajput

I'm Janvi Rajput, Founder Of Spotinkling.com. I love To Write and Explore.

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button