0
$\begingroup$
import pandas as pd
import numpy as np
import holidays
import matplotlib.pyplot as plt
import statsmodels.api as sm
from datetime import datetime, timedelta
from sklearn.model_selection import train_test_split
from sklearn.linear_model import LinearRegression
from sklearn.ensemble import RandomForestRegressor, GradientBoostingRegressor
from sklearn.metrics import mean_absolute_error

np.random.seed(0) 

start_date = datetime(2022, 1, 1)
end_date = start_date + timedelta(days=730)
dates = pd.date_range(start_date, end_date, freq='MS')

sales = np.random.randint(50, 200, size=len(dates))

sales_data = pd.DataFrame({'Date': dates, 'Sales': sales})
sales_data.index = sales_data['Date']
sales_data = sales_data.drop(['Date'], axis=1)
sales_data['Month'] = sales_data.index.month
sales_data['Quarter'] = sales_data.index.quarter
sales_data['Lag1'] = sales_data['Sales'].shift(1)
sales_data['Lag3'] = sales_data['Sales'].shift(3)
sales_data['Lag6'] = sales_data['Sales'].shift(6)
sales_data['Rolling_Mean_2'] = sales_data['Sales'].rolling(window=2).mean()
sales_data['Rolling_Mean_3'] = sales_data['Sales'].rolling(window=3).mean()
sales_data['Rolling_Mean_6'] = sales_data['Sales'].rolling(window=6).mean()
sales_data = sales_data.dropna()

I am trying to learn time series modeling. This is my code with dummy data. Now I want to train this data using traditional models like linear regression, random forest, and gradient boost. So, now how can we train the model that will predict 90 days from the date the model was run?

For example, right now minimum date in this dataset is 2022-07-01 and the maximum date is 2024-01-01. So, how can we prepare a dataset that will train the machine learning model for 90 days/3 months of forecasting? Even when the model is run on 2024-04-01, we should know or be confident that the prediction that it made was for 2024-07-01. And I am trying to learn single forecasting that will be made for 2024-07-01 instead of multiple stepwise forecasting.

$\endgroup$

1 Answer 1

2
$\begingroup$

One approach is to create target data with shift the sale data by -3 which will create something like this (i remove some features for simplicity):

| Date       | Sales | Target |
|------------|-------|--------|
| 2022-07-01 | 86    | 138.0  |
| 2022-08-01 | 137   | 190.0  |
| 2022-09-01 | 120   | 108.0  |
| 2022-10-01 | 138   | 89.0   |
| 2022-11-01 | 190   | 137.0  |
| 2022-12-01 | 108   | 138.0  |
| 2023-01-01 | 89    | 131.0  |
| 2023-02-01 | 137   | 75.0   |
| 2023-03-01 | 138   | 127.0  |
| 2023-04-01 | 131   | 122.0  |
| 2023-05-01 | 75    | 59.0   |
| 2023-06-01 | 127   | 198.0  |
| 2023-07-01 | 122   | 165.0  |
| 2023-08-01 | 59    | 129.0  |
| 2023-09-01 | 198   | 132.0  |
| 2023-10-01 | 165   | 149.0  |
| 2023-11-01 | 129   | NaN    |
| 2023-12-01 | 132   | NaN    |
| 2024-01-01 | 149   | NaN    |

Now we can see target value corresponded to each Date is sale value of next three month, this is how we can create target for our model to support purpose of our problem

$\endgroup$
6
  • $\begingroup$ Thank you for the information and that is very helpful. So, what is the best way to split the dataset for the time series model? Do we use a regular 80-20 split or do we need to split based on date? $\endgroup$
    – Bad Coder
    Commented Jun 14 at 17:52
  • $\begingroup$ percentage is as usual but important part is you do not have the permission to split data randomly otherwise you will have data leakage. $\endgroup$
    – Moein
    Commented Jun 15 at 8:33
  • $\begingroup$ Sorry I did not get this part do not have the permission to split data randomly otherwise. Would you mind explaining about this? $\endgroup$
    – Bad Coder
    Commented Jun 15 at 16:10
  • $\begingroup$ if you split data to train and test then you train should be past data and test should be future data and if you split your data randomly, there is some chance that future data will be in your train set and therefore your model will have information about future data $\endgroup$
    – Moein
    Commented Jun 15 at 19:00
  • $\begingroup$ Make sense. Thank you so much for your help and information. Do you have any reference to book or article or example that I learn about timeseries. For example, most of the tutorial belongs to single variate. But I wanted to learn multi-variate time series problem where we de-trend the trend and remove seasonality and build the model. While building the model, most of the datasets have single values. For example, single sales of a product. But in real life, there could be multiple products in a company where we have to forecast sales or something. $\endgroup$
    – Bad Coder
    Commented Jun 16 at 4:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.