Time series data is a type of data that changes over a time period. The sales data of a company does not remain the same for every year, sometimes it’s higher than the previous year, and sometimes it’s lower. Similarly, we see that stock prices are always changing.
Time series prediction is predicting future data with respect to the historic data. Python has different models and libraries which can be used for prediction but we should know which library or model works best for which type of data. Because there are a large number of models but all work on different types of data.
AutoTS allows you to run your data through different models for time series prediction which are already present in it and give out the result for the best model that works for your data. It has around 20 built-in models which make it powerful enough to work on any type of time-series data.
Some of the features of AutoTS are:
- Around 20 pre-defined models like ARIMA, ETS, VECM etc. are available to use with thousands of possible hyperparameters.
- It works on finding the best model according to the seasonality and trend of the data by genetic programming
- AutoTS can handle both Univariate and Multi-Variate Datasets.
- AutoTS itself clears the data from any NaN columns or outliers which are present.
In this article, we will explore how to use AutoTS to predict time-series data and forecast future data accordingly.
Implementation:
Like any other python library, we will install AutoTS using pip install autots.
- Importing Required Libraries
We will use pandas for loading the dataset we will use and Autots for predicting the best model for forecasting of data.
import pandas as pd
from autots import AutoTS
- Loading the dataset
I have downloaded the stock data of a stock name Biocon from Yahoo Finance in the csv format which I will use here. The dataset contains different attributes of stock data but we are mainly concerned with Date and Closing price.
df = pd.read_csv('biocon.csv')
df
- Creating the Model
For calling the model with our dataset we first need to define the model and pass the parameters according to our requirements. While creating the model we will give below parameters:
- forecast_length
- frequency
- ensemble
- Drop data
#Creating the Model
mod = AutoTS(forecast_length=3, frequency='infer', ensemble='simple', drop_data_older_than_periods=200 )
- Fitting The Model
After creating our model the step is to fit the model according to our dataset. We will also print the name of the model which best works for our data. This step will take some time as it will run our data through different models and check the best model for our data. I won’t be able to show the output of the whole process so I will only print the name of the best Model.
mod = mod.fit(df, date_col='Date', value_col='Close', id_col=None)
# Name of the best model
print(mod)
Here we can see that the best model for our dataset is VECM(Vector Error Correction Model), now we use this model for prediction and forecasting.
- Prediction and Forecasting
We can use AutoTS forecast and predict function for this step. As we have selected forecast length as 3 so it will display the forecast of the next 3 days.
prediction = mod.predict()
forecast = prediction.forecast
model_results = model.results()
validation = model.results("validation")
Let’s print the forecast of the dataset for the future stock price. Also, we will see the validation of the model results.
#Forecast
print(forecast)
#Validation Results
print(validation)
The validation result also contains different attributes i.e errors, weighted errors, etc. The score of the respective models is also present in the validation results which is used to select the best model.
This is how we can create a model using AutoTS for time series analysis, prediction and forecasting. We can select the model to use by passing a model list and naming the models we want in that list, which will run your data through those models only.
Conclusion:
In this article, we saw how easily we can use AutoTS to analyze predict and forecast time series data. AutoTs is fast and time-saving because you don’t have to define models explicitly as more than 20 models are already defined in it and the list is ever increasing.