A hands-on guide to time series modelling pipelines using EvalML
In recent years,  we can see the emergence of automated machine learning (AutoML), and various other tools and libraries that provide a range of features to perform AutoMl. Utilizing AutoML has its benefits and when it comes to time series modelling we find there are very few libraries that allow us to perform automated time series modelling. EvalML is one of such libraries that provide AutoML features in various sectors of machine learning including time series modelling. In this article, we are going to discuss time series modelling pipeline using EvalML. The major points to be discussed in the article are listed below. Table of contents What is EvalML?Data preparationLoading dataExploring dataData preprocessingTime series modellingBaseline pipelinePipeline evaluationMaking predictions What is EvalML? EvalMl is a python library for automated machine learning that helps us in building, optimizing, and evaluating machine learning pipelines. This library combines Features Tools and Compose libraries and uses domain-specific objective functions to build the optimized and evaluated machine learning pipelines in different domains. In one of our articles, we have discussed how we can automate our machine learning pipelines using the EvalML. We can utilize the EvalML for solving a wide range of machine learning problems such as regression, binary classification, multiclass classification, and time series modelling. In this article, we aim to understand how we can utilize the EvalML for time series modelling. Before starting the procedure we are required to install this library in our environment which can be done using the following codes: pip install evalml After installation, we are ready to use it for time series modelling. Are you looking for for a complete repository of Python libraries used in data science, check out here. Data preparation Loading data Before evaluating the EvalMl for time series modelling we are required to have a time series. So in this article, we are going to utilize the yfinance module for extracting the present data of the state bank of India’s share price. Using the following lines of code we can install this module. !pip install yfinance Using the following lines of codes we can extract the share price data of one year. import datetime as dt from datetime import datetime as dt from dateutil.relativedelta import relativedelta import yfinance as yf end = dt.today() start = dt.today() - relativedelta(years=1) data = yf.download('SBIN.NS', start, end) data.tail() Output: Here we can see that we have the share price data of the state bank of India till the 24th of march. For time series modelling we are going to use the close values of the share price. Exploring data Now let’s plot the data to understand it more import matplotlib.pyplot as plt data.plot(y = 'Close',figsize=(20, 4)) plt.grid() plt.legend(loc='best') plt.title('closing rates') plt.show(block=False) Output: Here we can see that there is an upside trend in the data which means with time the share price of the state bank of India is increasing. Let’s check for the outliers. import seaborn as sns fig = plt.subplots(figsize=(20, 5)) ax = sns.boxplot(x=data['Close'],whis=1.5) Output: Here we can see there is no outlier in the data. Let’s check for the null values in the data data.isna().sum() Output: There are no null values in the data. Data preprocessing To use the data with the EvalML we are required to preprocess the data according to the modules of EvalML. Let’s start with the data preprocessing import pandas as pd data.reset_index(inplace = True) data = data.rename(columns={'Close': 'y'}) X = pd.DataFrame(data["Date"]) y = data['y'] In the above codes, we reset the index of the data, renamed the close variable as y, made a data frame using the date values named X and a series of closing values named y. Let’s check the values inside the series and data frame. X Output: y Output: Splitting the data into test and train import numpy as np y_train, y_test= np.split(y, [int(.67 *len(y))]) X_train, X_test= np.split(X, [int(.67 *len(X))]) Let’s plot the data using Plotly import plotly.graph_objects as go data = [ go.Scatter( x=X_train["Date"], y=y_train, mode="lines+markers", name="closing price", line=dict(color="#1f77b4"), ) ] # Let plotly pick the best date format. layout = go.Layout( title={"text": "share market closing price data SBI(24/03/2021 - 24/03/2022"}, xaxis={"title": "Time"}, yaxis={"title": "closing price"}, ) go.Figure(data=data, layout=layout) Output: The above plot is an interactive graph and can be interacted with in this notebook. After this let’s start the time series modelling. Time series modelling In this section, we will look at how we can utilize the EvalML for time series forecast Let’s start by running the AutoMLsearch. AutoMLsearch: from evalml.automl import AutoMLSearch problem_config = {"gap": 0, "max_delay": 7, "forecast_horizon": 15, "time_index": "Date"} automl = AutoMLSearch(X_train, y_train, problem_type="time series regression", max_batches=1, problem_configuration=problem_config, allowed_model_families=["xgboost", "random_forest", "linear_model", "extra_trees"] ) In the above codes, we can see different parameters defined under the instance of AutoMLSearch. One parameter that is very important to define is ploblem_type where for time series we are required to define it as time series regression. With this parameter, we have defined some values in problem_configuration where the gap will tell prediction from next day is defined zero, max_delay is a kind of moving average and defined as 7. So it will look at the 7 past rows to predict the future of the time series and forecast_horgon is defined as 15 to forecast for the next 15 days. This instance will finally work using the following lines of codes. automl.search() Using the above method this instance will search for the best pipeline. Under the hood, the process is going through rolling origin cross-validation which is a way to split the at while training the model. Baseline Pipeline Using the below codes we can define a baseline pipeline baseline = automl.get_pipeline(0) baseline.fit(X_train, y_train) naive_baseline_preds = baseline.predict_in_sample(X_test, y_test, objective=None, X_train=X_train, y_train=y_train) expected_preds = pd.concat([y_train.iloc[-7:], y_test]).shift(7).iloc[7:] pd.testing.assert_series_equal(expected_preds, naive_baseline_preds) In the above, we have used the share price of the recent 7 days to make the forecast because using this way we get the most relevant values. Pipeline evaluation In the above, we have gone through the steps that can be followed for searching the best time-series pipeline to model our time series. Now we can also extract and evaluate the best pipeline we have got using the EvalML. Let’s extract the best pipeline pipeline = automl.best_pipeline pipeline.fit(X_train, y_train) best_pipeline_score = pipeline.score(X_test, y_test, ['MedianAE'], X_train, y_train)['MedianAE'] Let’s check the score of this pipeline. print("the best pipeline's score is:", best_pipeline_score) Output: Here we can see our results, Let’s check how the pipeline is predicted using the test data. from evalml.model_understanding import graph_prediction_vs_actual_over_time plot = graph_prediction_vs_actual_over_time(pipeline, X_test, y_test, X_train, y_train, dates=X_test['Date']) plot Output: This is also an interactive plot and we can see that the model is almost predicting like the actual values. Making predictions Now we can make our predictions using the predict module from EvalML in the following way. Pred = pipeline.predict(X_test.iloc[:pipeline.forecast_horizon], objective=None, X_train=X_train, y_train=y_train) pred Output: Let’s plot the forecasted values. pred.plot() Output: Here we can see that the pipeline is telling us that in the next 15 days share price of the state bank of India is going to decrease within the range of 530 to 480. Final words In this article, we have discussed the EvalML library that provides us with the facility of building, optimizing, and evaluating machine learning pipelines automatically. Using this library we have made a pipeline to forecast the share price of the state bank of India for the next 15 days. References: EvalML documentationLink for the codes
EvalMl is a python library for automated machine learning that helps us in building, optimizing, and evaluating machine learning pipelines.
EvalMl is a python library for automated machine learning that helps us in building, optimizing, and evaluating machine learning pipelines.
["AI (Artificial Intelligence)", "Data Science", "Deep Learning", "Machine Learning", "Time Series", "Time Series Analysis", "Time Series Forecasting", "time series prediction"]
["Time Series Analysis", "data science", "NumPy", "machine learning", "Plotly", "AI", "ML", "Machine Learning", "Time Series Forecasting", "Aim", "XGBoost", "Time Series", "time series prediction", "Deep Learning", "Data Science", "Matplotlib", "AI (Artificial Intelligence)", "Pandas"]
["AI", "machine learning", "ML", "data science", "Aim", "XGBoost", "Pandas", "NumPy", "Matplotlib", "Plotly"]
Yugesh is a graduate in automobile engineering and worked as a data analyst intern. He completed several Data Science projects. He has a strong interest in Deep Learning and writing blogs on data science and machine learning.