Skip to main content

Time Series

In this example, we will show you how you can train a Time series forecasting model using the Abacus client. We will be using the Demand Forecasting use case type.

Step 1: Create Dummy data​

By default, Timeseries require:

  • Timeseries Feature Group: Mandatory
  • Attributes Feature Group: Optional

If you already have data in the platform, then you can skip the creation of the dummy data. Please use the existing code as a guideline and alter it as required for your use case.

import pandas as pd
import numpy as np

# Create sample weekly data for 3 products across different stores
dates = pd.date_range(start='2023-01-01', end='2023-12-31', freq='W-MON')
products = ['PROD_A', 'PROD_B', 'PROD_C']
stores = ['STORE_1', 'STORE_2']

data = []
for store in stores:
for product in products:
base_demand = np.random.choice([100, 150, 200])
for date in dates:
# Create some seasonal pattern with noise
seasonal = 20 * np.sin(2 * np.pi * date.dayofyear / 365)
noise = np.random.normal(0, 10)
demand = max(0, base_demand + seasonal + noise)

# Create item_id as combination of store and product
item_id = f"{store}_{product}"

# Add some holiday information
is_holiday = 1 if date.dayofyear in [1, 180, 359] else 0 # New Year, Mid-year, Christmas
holiday_name = 'New Year' if date.dayofyear == 1 else 'Mid-Year' if date.dayofyear == 180 else 'Christmas' if date.dayofyear == 359 else None

data.append({
'ITEM_ID': item_id,
'DATE': date.strftime('%Y-%m-%d'),
'SALES': round(demand, 2),
'IS_HOLIDAY': is_holiday,
'HOLIDAY_NAME': holiday_name
})

timeseries_df = pd.DataFrame(data)

# Create attributes data
attributes_data = []
for store in stores:
store_size = np.random.choice(['SMALL', 'MEDIUM', 'LARGE'])
store_region = np.random.choice(['NORTH', 'SOUTH', 'EAST', 'WEST'])

for product in products:
item_id = f"{store}_{product}"
product_category = 'Electronics' if 'A' in product else 'Clothing' if 'B' in product else 'Food'
price_tier = 'Premium' if 'A' in product else 'Standard' if 'B' in product else 'Economy'

attributes_data.append({
'ITEM_ID': item_id,
'STORE_SIZE': store_size,
'STORE_REGION': store_region,
'PRODUCT_CATEGORY': product_category,
'PRICE_TIER': price_tier
})

attributes_df = pd.DataFrame(attributes_data)

Step 2: Upload Data as a Feature Group​

We will now register the data in the platform

client = ApiClient('YOUR_API_KEY')

# Create time series feature group
timeseries_fg = client.create_feature_group_from_pandas_df(
table_name="tutorial_timeseries_data",
df=timeseries_df
)

# Create attributes feature group
attributes_fg = client.create_feature_group_from_pandas_df(
table_name="tutorial_attributes_data",
df=attributes_df
)

print(f"Created time series feature group with ID: {timeseries_fg.id}")
print(f"Created attributes feature group with ID: {attributes_fg.id}")

# Add feature groups to the project
client.add_feature_group_to_project(
feature_group_id=timeseries_fg.id,
project_id="your_project_id",
feature_group_type="TIMESERIES"
)

client.add_feature_group_to_project(
feature_group_id=attributes_fg.id,
project_id="your_project_id",
feature_group_type="ITEM_ATTRIBUTES"
)

Step 3: Set Feature types​

Setting feature types is a mandatory steps for all use cases, unless the mapping has already been done automatically. It allows Abacus to know how to use the different features in the feature group.

# Wait for feature groups to be processed
time.sleep(30) # Wait for 30 seconds

# Set mappings for time series feature group
mappings_ts = [
("ITEM_ID", "ITEM_ID"),
("DATE", "DATE"),
("SALES", "DEMAND"),
("IS_HOLIDAY", "FUTURE"),
("HOLIDAY_NAME", "FUTURE")
]

for col_name, mapping in mappings_ts:
try:
client.set_feature_mapping(
project_id="your_project_id",
feature_group_id=timeseries_fg.id,
feature_name=col_name,
feature_mapping=mapping
)
print(f"Successfully mapped {col_name} to {mapping}")
except Exception as e:
print(f"Error mapping {col_name}: {str(e)}")

# Set mapping for attributes feature group
try:
client.set_feature_mapping(
project_id="your_project_id",
feature_group_id=attributes_fg.id,
feature_name="ITEM_ID",
feature_mapping="ITEM_ID"
)
print("Successfully mapped ITEM_ID in attributes feature group")
except Exception as e:
print(f"Error mapping ITEM_ID in attributes: {str(e)}")

Step 4: Train the Model​

Now we can train the model!

# Create training config
training_config = ForecastingTrainingConfig(
prediction_length=4, # Forecast 4 weeks ahead
forecast_frequency=ForecastingFrequency.WEEKLY_MONDAY_START, # Weekly forecasts starting Monday
enable_feature_selection=True,
probability_quantiles=[0.1, 0.5, 0.9] # Get P10, P50, and P90 predictions
)

# Train the model
model = client.train_model(
project_id="your_project_id",
name="Tutorial_Forecasting_Model_v2",
feature_group_ids=[timeseries_fg.id, attributes_fg.id], # Both time series and attributes feature groups
training_config=training_config
)

print(f"Started training model with ID: {model.id}")

Step 5: Deploy the Model​

Now we can deploy the model!

# Wait for training to complete
print("Waiting for model training to complete...")
model = model.wait_for_full_automl()
print("Model training completed!")

# Create a deployment
deployment = client.create_deployment(
name="Tutorial_Model_Deployment",
model_id=model.id,
project_id="your_project_id",
description="Deployment for our tutorial forecasting model"
)

print(f"Created deployment with ID: {deployment.id}")