Skip to content

Some baseline models

Models to be used as baselines for choice modeling. Nothing smart here.

DistribMimickingModel

Bases: ChoiceModel

Dumb class model that mimicks the probabilities.

It stores the encountered in the train datasets and always returns them

Source code in choice_learn/models/baseline_models.py
class DistribMimickingModel(ChoiceModel):
    """Dumb class model that mimicks the probabilities.

    It stores the encountered in the train datasets and always returns them
    """

    def __init__(self, **kwargs):
        """Initialize of the model."""
        super().__init__(**kwargs)
        self._trainable_weights = []
        self.is_fitted = False

    @property
    def trainable_weights(self):
        """Trainable weights of the model."""
        return [self._trainable_weights]

    def fit(self, choice_dataset, *args, **kwargs):
        """Compute the choice frequency of each product and defines it as choice probabilities.

        Parameters
        ----------
        choice_dataset : ChoiceDataset
            Dataset to be used for fitting
        """
        _ = kwargs
        _ = args
        choices = choice_dataset.choices
        for i in range(choice_dataset.get_n_items()):
            self._trainable_weights.append(tf.reduce_sum(tf.cast(choices == i, tf.float32)))
        self._trainable_weights = tf.stack(self._trainable_weights) / len(choices)
        self.is_fitted = True

    def _fit_with_lbfgs(self, choice_dataset, *args, **kwargs):
        """Compute the choice frequency of each product and defines it as choice probabilities.

        Parameters
        ----------
        choice_dataset : ChoiceDataset
            Dataset to be used for fitting
        """
        _ = kwargs
        _ = args
        choices = choice_dataset.choices
        for i in range(choice_dataset.get_n_items()):
            self._trainable_weights.append(tf.reduce_sum(tf.cast(choices == i, tf.float32)))
        self._trainable_weights = tf.stack(self._trainable_weights) / len(choices)
        self.is_fitted = True

    def compute_batch_utility(
        self,
        shared_features_by_choice,
        items_features_by_choice,
        available_items_by_choice,
        choices,
    ):
        """Return utility that is fixed. U = log(P).

        Parameters
        ----------
        shared_features_by_choice : tuple of np.ndarray (choices_features)
            a batch of shared features
            Shape must be (n_choices, n_shared_features)
        items_features_by_choice : tuple of np.ndarray (choices_items_features)
            a batch of items features
            Shape must be (n_choices, n_items_features)
        available_items_by_choice : np.ndarray
            A batch of items availabilities
            Shape must be (n_choices, n_items)
        choices_batch : np.ndarray
            Choices
            Shape must be (n_choices, )

        Returns
        -------
        np.ndarray (n_choices, n_items)
            Utilities

        Raises
        ------
        ValueError
            If the model has not been fitted cannot evaluate the utility
        """
        # In order to avoid unused arguments warnings
        _ = items_features_by_choice, shared_features_by_choice, available_items_by_choice
        if not self.is_fitted:
            raise ValueError("Model not fitted")
        return tf.stack([tf.math.log(self.trainable_weights[0])] * len(choices), axis=0)

trainable_weights property

Trainable weights of the model.

__init__(**kwargs)

Initialize of the model.

Source code in choice_learn/models/baseline_models.py
def __init__(self, **kwargs):
    """Initialize of the model."""
    super().__init__(**kwargs)
    self._trainable_weights = []
    self.is_fitted = False

compute_batch_utility(shared_features_by_choice, items_features_by_choice, available_items_by_choice, choices)

Return utility that is fixed. U = log(P).

Parameters:

Name Type Description Default
shared_features_by_choice tuple of np.ndarray (choices_features)

a batch of shared features Shape must be (n_choices, n_shared_features)

required
items_features_by_choice tuple of np.ndarray (choices_items_features)

a batch of items features Shape must be (n_choices, n_items_features)

required
available_items_by_choice ndarray

A batch of items availabilities Shape must be (n_choices, n_items)

required
choices_batch ndarray

Choices Shape must be (n_choices, )

required

Returns:

Type Description
ndarray(n_choices, n_items)

Utilities

Raises:

Type Description
ValueError

If the model has not been fitted cannot evaluate the utility

Source code in choice_learn/models/baseline_models.py
def compute_batch_utility(
    self,
    shared_features_by_choice,
    items_features_by_choice,
    available_items_by_choice,
    choices,
):
    """Return utility that is fixed. U = log(P).

    Parameters
    ----------
    shared_features_by_choice : tuple of np.ndarray (choices_features)
        a batch of shared features
        Shape must be (n_choices, n_shared_features)
    items_features_by_choice : tuple of np.ndarray (choices_items_features)
        a batch of items features
        Shape must be (n_choices, n_items_features)
    available_items_by_choice : np.ndarray
        A batch of items availabilities
        Shape must be (n_choices, n_items)
    choices_batch : np.ndarray
        Choices
        Shape must be (n_choices, )

    Returns
    -------
    np.ndarray (n_choices, n_items)
        Utilities

    Raises
    ------
    ValueError
        If the model has not been fitted cannot evaluate the utility
    """
    # In order to avoid unused arguments warnings
    _ = items_features_by_choice, shared_features_by_choice, available_items_by_choice
    if not self.is_fitted:
        raise ValueError("Model not fitted")
    return tf.stack([tf.math.log(self.trainable_weights[0])] * len(choices), axis=0)

fit(choice_dataset, *args, **kwargs)

Compute the choice frequency of each product and defines it as choice probabilities.

Parameters:

Name Type Description Default
choice_dataset ChoiceDataset

Dataset to be used for fitting

required
Source code in choice_learn/models/baseline_models.py
def fit(self, choice_dataset, *args, **kwargs):
    """Compute the choice frequency of each product and defines it as choice probabilities.

    Parameters
    ----------
    choice_dataset : ChoiceDataset
        Dataset to be used for fitting
    """
    _ = kwargs
    _ = args
    choices = choice_dataset.choices
    for i in range(choice_dataset.get_n_items()):
        self._trainable_weights.append(tf.reduce_sum(tf.cast(choices == i, tf.float32)))
    self._trainable_weights = tf.stack(self._trainable_weights) / len(choices)
    self.is_fitted = True

RandomChoiceModel

Bases: ChoiceModel

Dumb model that randomly attributes utilities to products.

Source code in choice_learn/models/baseline_models.py
class RandomChoiceModel(ChoiceModel):
    """Dumb model that randomly attributes utilities to products."""

    def __init__(self, **kwargs):
        """Initialize of the model."""
        super().__init__(**kwargs)

    @property
    def trainable_weights(self):
        """Return an empty list - there is no trainable weight."""
        return []

    def compute_batch_utility(
        self,
        shared_features_by_choice,
        items_features_by_choice,
        available_items_by_choice,
        choices,
    ):
        """Compute the random utility for each product of each choice.

        Parameters
        ----------
        shared_features_by_choice : tuple of np.ndarray (choices_features)
            a batch of shared features
            Shape must be (n_choices, n_shared_features)
        items_features_by_choice : tuple of np.ndarray (choices_items_features)
            a batch of items features
            Shape must be (n_choices, n_items_features)
        available_items_by_choice : np.ndarray
            A batch of items availabilities
            Shape must be (n_choices, n_items)
        choices_batch : np.ndarray
            Choices
            Shape must be (n_choices, )

        Returns
        -------
        tf.Tensor
            (n_choices, n_items) matrix of random utilities
        """
        # In order to avoid unused arguments warnings
        _ = shared_features_by_choice, items_features_by_choice, choices
        return np.squeeze(
            np.random.uniform(size=(available_items_by_choice.shape), low=0.0, high=1.0)
        ).astype(np.float32)

    def fit(self, *args, **kwargs):
        """Make sure that nothing happens during .fit."""
        _ = kwargs
        _ = args
        return {}

    def _fit_with_lbfgs(self, *args, **kwargs):
        """Make sure that nothing happens during .fit."""
        _ = kwargs
        _ = args
        return {}

trainable_weights property

Return an empty list - there is no trainable weight.

__init__(**kwargs)

Initialize of the model.

Source code in choice_learn/models/baseline_models.py
def __init__(self, **kwargs):
    """Initialize of the model."""
    super().__init__(**kwargs)

compute_batch_utility(shared_features_by_choice, items_features_by_choice, available_items_by_choice, choices)

Compute the random utility for each product of each choice.

Parameters:

Name Type Description Default
shared_features_by_choice tuple of np.ndarray (choices_features)

a batch of shared features Shape must be (n_choices, n_shared_features)

required
items_features_by_choice tuple of np.ndarray (choices_items_features)

a batch of items features Shape must be (n_choices, n_items_features)

required
available_items_by_choice ndarray

A batch of items availabilities Shape must be (n_choices, n_items)

required
choices_batch ndarray

Choices Shape must be (n_choices, )

required

Returns:

Type Description
Tensor

(n_choices, n_items) matrix of random utilities

Source code in choice_learn/models/baseline_models.py
def compute_batch_utility(
    self,
    shared_features_by_choice,
    items_features_by_choice,
    available_items_by_choice,
    choices,
):
    """Compute the random utility for each product of each choice.

    Parameters
    ----------
    shared_features_by_choice : tuple of np.ndarray (choices_features)
        a batch of shared features
        Shape must be (n_choices, n_shared_features)
    items_features_by_choice : tuple of np.ndarray (choices_items_features)
        a batch of items features
        Shape must be (n_choices, n_items_features)
    available_items_by_choice : np.ndarray
        A batch of items availabilities
        Shape must be (n_choices, n_items)
    choices_batch : np.ndarray
        Choices
        Shape must be (n_choices, )

    Returns
    -------
    tf.Tensor
        (n_choices, n_items) matrix of random utilities
    """
    # In order to avoid unused arguments warnings
    _ = shared_features_by_choice, items_features_by_choice, choices
    return np.squeeze(
        np.random.uniform(size=(available_items_by_choice.shape), low=0.0, high=1.0)
    ).astype(np.float32)

fit(*args, **kwargs)

Make sure that nothing happens during .fit.

Source code in choice_learn/models/baseline_models.py
def fit(self, *args, **kwargs):
    """Make sure that nothing happens during .fit."""
    _ = kwargs
    _ = args
    return {}