Skip to content

AleaCarta model Usage

Introduction to modelling with AleaCarta

Open In Colab

We use a synthetic dataset to demonstrate how to use the AleaCarta model [1]

# Install necessary packages that are not already installed in the environment
# !pip install matplotlib
import os
import sys

os.environ["CUDA_VISIBLE_DEVICES"] = ""

sys.path.append("./../../")
print(os.getcwd())

import matplotlib.pyplot as plt
import numpy as np

from synthetic_dataset import get_dataset_ace
from choice_learn.basket_models import AleaCarta
data = get_dataset_ace()

print(data)
print(f"\nThe TripDataset 'data' contains {data.n_items} distinct items that appear in {data.n_samples} transactions carried out at {data.n_stores} point(s) of sale with {data.n_assortments} different assortments.")
latent_sizes = {"preferences": 6, "price": 3, "season": 3}
n_negative_samples = 2
optimizer = "adam"
lr = 1e-2
epochs = 200
# epochs = 1000
batch_size = 32
model = AleaCarta(
    # item_intercept=True,
    item_intercept=False,
    price_effects=False,
    seasonal_effects=False,
    latent_sizes=latent_sizes,
    n_negative_samples=n_negative_samples,
    optimizer=optimizer,
    lr=lr,
    epochs=epochs,
    batch_size=batch_size,
)

model.instantiate(n_items=7, n_stores=2)
history = model.fit(trip_dataset=data)
plt.plot(history["train_loss"])
plt.xlabel("Epoch")
plt.ylabel("Training Loss")
plt.legend()
plt.title("Training")
plt.show()
model.compute_batch_utility(item_batch=np.array(list(range(2))),
        basket_batch=np.array([[1, 2], [3, 4]]),
        store_batch=np.array([0, 0]),
        week_batch=np.array([0, 0]),
        price_batch=np.array([[0, 0], [0, 0]]),
        available_item_batch=np.ones((2, data.n_items))
        )

References

[1] Better Capturing Interactions between Products in Retail: Revisited Negative Sampling for Basket Choice Modeling, Désir, J.; Auriau, V.; Možina, M.; Malherbe, E. (2025), ECML-PKDDD