This book teaches the practical implementation of various concepts for time series analysis and modeling with Python through problem-solution-style recipes, starting with data reading and preprocessing. It begins with the fundamentals of time series forecasting using statistical modeling methods
Time Series Algorithms Recipes. Implement Machine Learning and Deep Learning Techniques with Python
✍ Scribed by Akshay R Kulkarni, Adarsha Shivananda, Anoosh Kulkarni, V Adithya Krishnan
- Publisher
- Apress
- Year
- 2023
- Tongue
- English
- Leaves
- 189
- Category
- Library
No coin nor oath required. For personal study only.
✦ Table of Contents
Table of Contents
About the Authors
About the Technical Reviewer
Preface
Chapter 1: Getting Started with Time Series
Recipe 1-1A. Reading Time Series Objects (Air Passengers)
Problem
Solution
How It Works
Step 1A-1. Import the required libraries.
Step 1A-2. Write a parsing function for the datetime column.
Step 1A-3. Read the data.
Recipe 1-1B. Reading Time Series Objects (India GDP Data)
Problem
Solution
How It Works
Step 1B-1. Import the required libraries.
Step 1B-2. Read India’s GDP time series data.
Step 1B-3. Plot the time series.
Step 1B-4. Store and retrieve as a pickle.
Recipe 1-2. Saving Time Series Objects
Problem
Solution
How It Works
Step 2-1. Save the previously loaded time series object.
Recipe 1-3A. Exploring Types of Time Series Data: Univariate
Problem
Solution
How It Works
Step 3A-1. Import the required libraries.
Step 3A-2. Read the time series data.
Step 3A-3. Plot the time series.
Recipe 1-3B. Exploring Types of Time Series Data: Multivariate
Problem
Solution
How It Works
Step 3B-1. Import the required libraries.
Step 3B-2. Write the parsing function.
Step 3B-3. Load the dataset.
Step 3B-4. Do basic preprocessing.
Step 3B-5. Plot each series.
Recipe 1-4A. Time Series Components: Trends
Problem
Solution
How It Works
Step 4A-1. Import the required libraries.
Step 4A-2. Write the parsing function.
Step 4A-3. Load the dataset.
Step 4A-4. Plot the time series.
Recipe 1-4B. Time Series Components: Seasonality
Problem
Solution
How It Works
Step 4B-1. Import the required libraries.
Step 4B-2. Read the data.
Step 4B-3. Plot the time series.
Step 4B-4. Plot a box plot by month.
Step 4B-5. Plot a box plot by year.
Recipe 1-4C. Time Series Components: Seasonality (cont’d.)
Problem
Solution
How It Works
Step 4C-1. Import the required libraries.
Step 4C-2. Read tractor sales data.
Step 4C-3. Set a datetime series to use as an index.
Step 4C-4. Format the data.
Step 4C-5. Plot the time series.
Step 4C-6. Plot a box plot by month.
Recipe 1-5A. Time Series Decomposition: Additive Model
Problem
Solution
How It Works
Step 5A-1. Load the required libraries.
Step 5A-2. Read and process retail turnover data.
Step 5A-3. Plot the time series.
Step 5A-4. Decompose the time series.
Step 5A-5. Separate the components.
Recipe 1-5B. Time Series Decomposition: Multiplicative Model
Problem
Solution
How It Works
Step 5B-1. Load the required libraries.
Step 5B-2. Load air passenger data.
Step 5B-3. Process the data.
Step 5B-4. Decompose the time series.
Step 5B-5. Get the seasonal component.
Recipe 1-6. Visualization of Seasonality
Problem
Solution
How It Works
Step 6-1. Import the required libraries.
Step 6-2. Load the data.
Step 6-3. Process the data.
Step 6-4. Pivot the table.
Step 6-5. Plot the line charts.
Step 6-6. Plot the box plots.
Chapter 2: Statistical Univariate Modeling
Recipe 2-1. Moving Average (MA) Forecast
Problem
Solution
How It Works
Step 1-1. Import the required libraries.
Step 1-2. Read the data.
Step 1-3. Preprocess the data.
Step 1-4. Plot the time series.
Step 1-5. Use a rolling mean to get the moving average.
Step 1-6. Plot the forecast vs. the actual.
Recipe 2-2. Autoregressive (AR) Model
Problem
Solution
How It Works
Step 2-1. Import the required libraries.
Step 2-2. Load and plot the dataset.
Step 2-3. Check for stationarity of the time series data.
Step 2-4. Find the order of the AR model to be trained.
Step 2-5. Create training and test data.
Step 2-6. Call and fit the AR model.
Step 2-7. Output the model summary.
Step 2-8. Get the predictions from the model.
Step 2-9. Plot the predictions vs. actuals.
Recipe 2-3. Autoregressive Moving Average (ARMA) Model
Problem
Solution
How It Works
Step 3-1. Import the required libraries.
Step 3-2. Load the data.
Step 3-3. Preprocess the data.
Step 3-4. Plot the time series.
Step 3-5. Do a train-test split.
Step 3-6. Plot time the series after the train-test split.
Step 3-7. Define the actuals from training.
Step 3-8. Initialize and fit the ARMA model.
Step 3-9. Get the test predictions.
Step 3-10. Plot the train, test, and predictions as a line plot.
Step 3-11. Calculate the RMSE score for the model.
Recipe 2-4. Autoregressive Integrated Moving Average (ARIMA) Model
Problem
Solution
How It Works
Step 4-1. Make the data stationary by differencing.
Step 4-2. Check the ADF (Augmented Dickey-Fuller) test for stationarity.
Step 4-3. Get the Auto Correlation Function and Partial Auto Correlation Function values.
Step 4-4. Plot the ACF and PACF to get p- and q-values.
Step 4-5. Initialize and fit the ARIMA model.
Step 4-6. Get the test predictions.
Step 4-7. Plot the train, test, and predictions as a line plot.
Step 4-8. Calculate the RMSE score for the model.
Recipe 2-5. Grid Search Hyperparameter Tuning for ARIMA Model
Problem
Solution
How It Works
Step 5-1. Write a function to evaluate the ARIMA model.
Step 5-2. Write a function to evaluate multiple models through grid search hyperparameter tuning.
Step 5-3. Perform the grid search hyperparameter tuning by calling the defined functions.
Step 5-4. Initialize and fit the ARIMA model with the best configuration.
Step 5-5. Get the test predictions.
Step 5-6. Plot the train, test, and predictions as a line plot.
Step 5-7. Calculate the RMSE score for the model.
Recipe 2-6. Seasonal Autoregressive Integrated Moving Average (SARIMA) Model
Problem
Solution
How It Works
Step 6-1. Initialize and fit the SARIMA model.
Step 6-2. Get the test predictions.
Step 6-3. Plot the train, test, and predictions as a line plot.
Step 6-4. Calculate the RMSE score for the model.
Recipe 2-7. Simple Exponential Smoothing (SES) Model
Problem
Solution
How It Works
Step 7-1. Initialize and fit the SES model.
Step 7-2. Get the test predictions.
Step 7-3. Plot the train, test, and predictions as a line plot.
Step 7-4. Calculate the RMSE score for the model.
Recipe 2-8. Holt-Winters (HW) Model
Problem
Solution
How It Works
Step 8-1. Initialize and fit the HW model.
Step 8-2. Get the test predictions.
Step 8-3. Plot the train, test, and predictions as a line plot.
Step 8-4. Calculate the RMSE score for the model.
Chapter 3: Advanced Univariate and Statistical Multivariate Modeling
Recipe 3-1. FBProphet Univariate Time Series Modeling
Problem
Solution
How It Works
Step 1-1. Import the required libraries.
Step 1-2. Read the data.
Step 1-3. Create the training dataset.
Step 1-4. Initialize a basic Facebook Prophet model.
Step 1-5. Create the future dataframe for forecasting.
Step 1-6. Getting the predictions.
Step 1-7. Plot the forecast.
Step 1-8. Plot the forecast components.
Recipe 3-2. FBProphet Modeling by Controlling the Change Points
Problem
Solution
How It Works
Step 2-1. Plot the change points.
Step 2-2. Print the change points.
Step 2-3. Check the magnitude of each changepoint.
Step 2-4. Tweak the n_changepoints hyperparameter and forecasting.
Step 2-5. Tweak the changepoint_range hyperparameter and forecasting.
Recipe 3-3. FBProphet Modeling by Adjusting Trends
Problem
Solution
How It Works
Step 3-1. Increase the changepoint_prior_scale hyperparameter.
Step 3-2. Forecast and plot the output.
Step 3-3. Decrease the changepoint_prior_scale hyperparameter.
Step 3-4. Forecast and plot the output.
Recipe 3-4. FBProphet Modeling with Holidays
Problem
Solution
How It Works
Step 4-1. Create a custom holiday dataframe.
Step 4-2. Initialize and fit the Prophet model with the holidays dataframe.
Step 4-3. Create a future dataframe for the forecast.
Step 4-4. Get the forecast.
Recipe 3-5. FBProphet Modeling with Added Regressors
Problem
Solution
How It Works
Step 5-1. Label and encode the type column.
Step 5-2. Get the data in the required format.
Step 5-3. Do a train-test split.
Step 5-4. Initialize the Prophet model and add a regressor.
Step 5-5. Fit the data.
Step 5-6. Forecast the data in the test.
Recipe 3-6. Time Series Forecasting Using Auto-ARIMA
Problem
Solution
How It Works
Step 6-1. Import the required libraries.
Step 6-2. Read the data.
Step 6-3. Preprocess the data.
Step 6-4. Analyze the data pattern.
Step 6-5. Test for stationarity.
Step 6-6. Split the dataset to train and test.
Step 6-7. Build the Auto-ARIMA model.
Step 6-8. Forecast using the test data.
Step 6-9. Evaluate the model.
Recipe 3-7. Multivariate Time Series Forecasting Using the VAR Model
Problem
Solution
How It Works
Step 7-1. Import the required libraries.
Step 7-2. Read the data.
Step 7-3. Preprocess the data.
Step 7-4. Check the stationarity.
Step 7-5. Split the dataset into train-test.
Step 7-6. Build the VAR model and forecast on the test set.
Step 7-7. Evaluate the model.
Chapter 4: Machine Learning Regression–based Forecasting
Recipe 4-1. Formulating Regression Modeling for Time Series Forecasting
Problem
Solution
How It Works
Step 1-1. Install and import the required libraries.
Step 1-2. Collect the data.
Step 1-3. Preprocess the data and create features (feature engineering).
Step 1-4. Select the features.
Step 1-5. Work on the train-test and validation split.
Recipe 4-2. Implementing the XGBoost Model
Problem
Solution
How It Works
Step 2-1. Build the XGBoost model.
Step 2-2. Evaluate the XGBoost model in the test set.
Step 2-3. Evaluate the XGBoost model in the validation set.
Recipe 4-3. Implementing the LightGBM Model
Problem
Solution
How It Works
Step 3-1. Build the LightGBM model.
Step 3-2. Evaluate the LightGBM model in the test set.
Step 3-3. Evaluate the LightGBM model in the validation set.
Recipe 4-4. Implementing the Random Forest Model
Problem
Solution
How It Works
Step 4-1. Build a random forest model.
Step 4-2. Evaluate the LightGBM model in the test set.
Step 4-3. Evaluate the LightGBM model in the validation set.
Recipe 4-5. Selecting the Best Model
Problem
Solution
How It Works
Step 5-1. Evaluate the method.
Step 5-2. Compare performance in the test set.
Step 5-3. Plot the LightGBM model prediction against the actuals in the test set.
Step 5-4. Compare performance in the validation set.
Step 5-5. Plot the LightGBM model prediction against actuals in the validation set.
Chapter 5: Deep Learning–based Time Series Forecasting
Recipe 5-1. Time Series Forecasting Using LSTM
Problem
Solution
How It Works
Step 1-1. Import the required libraries.
Step 1-2. Use DOM_hourly.csv data for analysis.
Step 1-3. Read the data.
Step 1-4. Check for missing data.
Step 1-5. Plot the time series data.
Step 1-6. Write a function to normalize the data.
Step 1-7. Call the normalize_fn function.
Step 1-7. Plot the data after normalization.
Step 1-8. Create a function to perform data preparation and train-test split.
Step 1-9. Call the data_prep function.
Step 1-10. Initialize the LSTM model.
Step 1-11. Create the model summary.
Step 1-12. Fit the model.
Step 1-13. Make the model predictions and print the score.
Step 1-14. Write a function to plot the predictions.
Step 1-15. Call the plotting_actual_vs_pred function.
Recipe 5-2. Multivariate Time Series Forecasting Using the GRU Model
Problem
Solution
How It Works
Step 2-1. Import the required libraries.
Step 2-2. Read the data.
Step 2-3. Analyze the data.
Step 2-4. Preprocess the data.
Step 2-5. Do a train-test split.
Step 2-6. Build the model.
Step 2-7. Evaluate and predict the model.
Recipe 5-3. Time Series Forecasting Using NeuralProphet
Problem
Solution
How It Works
Step 3-1. Import the required libraries.
Step 3-2. Read the data.
Step 3-3. Preprocess the data.
Step 3-4. Build the model and make predictions.
Recipe 5-4. Time Series Forecasting Using RNN
Problem
Solution
How It Works
Step 4-1. Initialize the RNN model.
Step 4-2. Create the model summary.
Step 4-3. Fit the model.
Step 4-4. Make the model predictions and print the score.
Step 4-5 is the same as step 1-14 from Recipe 5-1.
Step 4-6. Call the plotting_actual_vs_pred function.
Index
📜 SIMILAR VOLUMES
Learn to harness the power of AI for natural language processing, performing tasks such as spell check, text summarization, document classification, and natural language generation. Along the way, you will learn the skills to implement these methods in larger infrastructures to replace existing code
<span> Get Involved with Machine learning </span><span><br><br> </span><span>Key Features</span><ul><li><span><span> Machine learning in MATLAB using basic concepts and algorithms.</span></span></li><li><span><span> Algorithms of machine learning in a simple language using MATLAB code.</span></span>
Learn how to use TensorFlow 2.0 to build machine learning and deep learning models with complete examples. The book begins with introducing TensorFlow 2.0 framework and the major changes from its last release. Next, it focuses on building Supervised Machine Learning models using TensorFlow 2.0. It