Professional Documents
Culture Documents
7 Libraries That Help in Time-Series problems-AI Data Science
7 Libraries That Help in Time-Series problems-AI Data Science
You have 2 free member-only stories left this month. Sign up for Medium and get an extra one
Time series problems are one of the toughest problems to solve in data science.
Traditional methods that are time-aware like ARIMA, SARIMA are great but lately they
have largely been accompanied by the non-time aware and robust machine learning
algorithms like XGBoost, LigthGBM, and so forth because of need and proven successful
track records. However, using these methods require extensive data preparation like
removing periodicity, removing trend from the target and engineering features like
rolling window features, lag features, etc. to prepare the final dataset.
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 1/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
To gain better accuracy we need to develop complex models and the work can be quite
extensive. Therefore, it is better to leverage some of the automation that is already
developed/creating by the Machine Learning community. Below are some of the
packages which are really helpful in solving time series problems.
1. tsfresh:
tsfresh is a fantastic python package that can automatically calculate a large number
of time series features.
Let's understand how tsfresh can be implemented by taking a standard dataset of Airline
passengers:
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 2/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
Get started
mporting librariesOpen in app
ort pandas as pd
m tsfresh import extract_features, extract_relevant_features,
ect_features
m tsfresh.utilities.dataframe_functions import impute,
e_forecasting_frame
m tsfresh.feature_extraction import ComprehensiveFCParameters,
tings
Output of dataframe
Output of features
From the output above we see that almost ~800 features are created. tsfresh also
helps in feature selection based on p-value. Check out the documentation for more
details.
There is an exhaustive list of all the features that are calculated using tsfresh which
can found here.
Github: https://github.com/blue-yonder/tsfresh
Documentation: https://tsfresh.readthedocs.io/en/latest/index.html
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 4/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
2. autots:
Get started Open in app
AutoTS is an automated time series forecasting library that can train multiple time series
models using straightforward code. AutoTS means Automatic Time Series.
Library Logo
It trains diverse models like naive, statistical, machine learning as well as deep
learning models
It also has the ability to handle messy data by learning optimal NaN imputation and
outlier removal
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 5/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
model = AutoTS(forecast_length=40,
frequency='infer',
ensemble='simple',
drop_data_older_than_periods=100)
model = model.fit(df, date_col='Date',
value_col=' Close/Last', id_col=None)
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 6/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
This will run hundreds of models. You will see in the output pane the variety of models
that run. Lets see how the model predicts :
prediction = model.predict()
forecast = prediction.forecast
print("Stock Price Prediction of
Apple")
print(forecast)
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 7/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
temp_df[' Close/Last'].plot(figsize=
(15,8), title= 'AAPL Stock Price',
fontsize=18, label='Train')
forecast[' Close/Last'].plot(figsize=
(15,8), title= 'AAPL Stock Price',
fontsize=18, label='Test')
plt.legend()
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 8/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
Github: https://github.com/winedarksea/AutoTS
Documentation:
https://winedarksea.github.io/AutoTS/build/html/source/tutorial.html
3. Prophet:
Library logo
daily seasonality,
holiday effects
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 9/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
input regressors
Get started Open in app
s plt
het
e repo:
ith b t t /f b k/ h t/ t / l / l
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 10/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
And finally, we can also see the predictions along with all the confidence intervals
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 11/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
Github: https://github.com/facebook/prophet
Documentation: https://facebook.github.io/prophet/
4. darts:
Darts is another Python package that helps in the manipulation and forecasting of time
series. The syntax is “sklearn-friendly” using fit and predict functions to achieve your
goals. In addition, it contains a variety of models from ARIMA to Neural Networks.
The best part of the package is that it supports not only univariate but also supports
multivariate time series and models. The library also makes it easy to backtest models
and combine the predictions of several models and external regressors. Lets take a
simple example and understand its working:
# S litti th i i t i d lid
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd ti t 13/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
# Splitting the series in train and validation set
train, val = series.split_before(pd.Timestamp('19580101'))
Get started Open in app
# Applying a simple Exponential Smoothing model
model = ExponentialSmoothing()
model.fit(train)
Github: https://github.com/unit8co/darts
Documentation: https://unit8co.github.io/darts/README.html
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 14/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
5. AtsPy:
AtsPy stands for Automated Time Series Models in Python. The goal of the library is to
forecast univariate time series. You can load the data and specify which models you
would like to run as shown in the example below:
# Importing packages
import pandas as pd
from atspy import AutomatedModel
# Reading the data:
data = pd.read_csv('../input/air-passengers/AirPassengers.csv')
# Preprocessing data
data.columns = ['month','Passengers']
data['month'] =
pd.to_datetime(data['month'],infer_datetime_format=True,format='%y%m')
data.index = data.month
df_air = data.drop(['month'], axis = 1)
The package provides a diverse set of models totally automated. Below is the screenshot
of the models available:
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 15/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
Github: https://github.com/firmai/atspy
6. kats:
Kats is another recent library developed by the research team at Facebook dedicated
especially to handle time-series data. The goal of the framework is to provide a complete
solution for solving time series problems. Using this library we can do the following:
time-series analysis
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 16/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
building forecasting models on time series data including Prophet, ARIMA, Holt-
Get started Open in app
Winters, etc.
The library seems to be promising and it has just released its first version. Some tutorials
can be found here.
Github: https://github.com/facebookresearch/Kats
7. sktime:
Sktime library as the name suggests is a unified python library that works for time series
data and is scikit-learn compatible. It has models for time series forecasting, regression,
and classification. The main goal to develop was to interoperate with scikit-learn.
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 17/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
Model tuning
Ensembling of models
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 18/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
The roadmap of sktime looks very promising and has a lot of developments coming
Get started Open in app
down the pipeline:
1. Multivariate/panel forecasting,
If there is a specific library/package you would like me to make a detailed tutorial please do
comment and let me know. Also, if there are any other wonderful time series packages that
can be added to this list, please do not hesitate to comment. Thank you for your time for
reading!
1. https://towardsdatascience.com/7-statistical-tests-to-validate-and-help-to-fit-
arima-model-33c5853e2e93
2. https://towardsdatascience.com/20-simple-yet-powerful-features-for-time-series-
using-date-and-time-af9da649e5dc
Every Thursday, the Variable delivers the very best of Towards Data Science: from hands-on tutorials
and cutting-edge research to original features you don't want to miss. Take a look.
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 19/20
6/29/2021 7 libraries that help in time-series problems | by Pratik Gandhi | Jun, 2021 | Towards Data Science
AboutWriteHelpLegal
https://towardsdatascience.com/7-libraries-that-help-in-time-series-problems-d59473e48ddd 20/20