exact_row_indices#

pymc_marketing.mmm.lift_test.exact_row_indices(df, model)[source]#

Get indices in the model for each row in the DataFrame.

Assumes any column in the DataFrame is a coordinate in the model with the same name.

If the DataFrame has columns that are not in the model, it will raise an error.

Parameters:
  • df (DataFrame) – DataFrame with coordinates combinations.

  • model (Model) – PyMC model with all the coordinates in the DataFrame.

Return type:

dict[str, np.ndarray]

Returns:

Dictionary of indices for the lift test results in the model.

Raises:
  • UnalignedValuesError – If some values are not aligned. This means that some values in the DataFrame are not in the model.

  • KeyError – If some coordinates in the DataFrame are not in the model.

Examples

Get the indices from a DataFrame and model:

import pymc as pm
import pandas as pd

from pymc_marketing.mmm.lift_test import exact_row_indices

df_lift_test = pd.DataFrame(
    {
        "channel": [0, 1, 0],
        "geo": ["A", "B", "B"],
    }
)

coords = {"channel": [0, 1, 2], "geo": ["A", "B", "C"]}
model = pm.Model(coords=coords)

indices = exact_row_indices(df_lift_test, model)
# {'channel': array([0, 1, 0]), 'geo': array([0, 1, 1])}