API Reference

EventsGroupBy

class monad.targets.EventsGroupBy

Contains grouped events from a data source.

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
    ...
    
    # first we group events (future purchases) by selected column (brand)
    # then we do other operations; below we filter to 2 brands and check for existence

    prop_target = (
        future["transactions"]
        .groupby("brand")
        .count(normalize=True, groups=["Garmin", "Suunto"])
    )

    ...

Methods


EventsGroupBy.count(self, normalize=False, groups=None)

Counts elements in each group.

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
    ...
    
    # below we first group future purchase events with `groupBy` by brand name
    # we then count events, filter down to 2 brands and normalise so the sum of values is 1

    prop_target = (
        future['transactions']
        .groupBy('brand')
        .count(normalize=True, groups=['Garmin', 'Suunto'])
    )

    ...
Parameters

normalize: bool
Default: False
Scales counts so that they sum to 1.


groups: list[Any]
Default: None
Limits grouping to the list of provided values.

Returns

tuple[numpy.ndarray, list[str]], a tuple with count of elements per each group and group names.



EventsGroupBy.sum(self, target, groups=None)

Sums the values of the column target in each group.

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
    ...
    
    spend_by_cat = (
        future["transactions"]
        .groupBy("category")
        .sum(target="purchase_value")
    )
    
    ...
Parameters

target: str
A column to apply the grouping operation to.


groups: list[Any]
Default: None
Limits grouping to the list of provided values.

Returns

tuple[numpy.ndarray, list[str]], a tuple with sum of elements per each group and group names.



EventsGroupBy.mean(self, target, groups=None)

Computes the mean of the values of the column target in each group.

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
    ...
    
    ave_spend_by_cat = (
        future["transactions"]
        .groupBy("category")
        .mean(target="purchase_value")
    )
    
    ...
Parameters

target: str
A column to apply the grouping operation to.


groups: list[Any]
Default: None
Limits grouping to the list of provided values.

Returns

tuple[numpy.ndarray, list[str]], a tuple with mean of elements per each group and group names.



EventsGroupBy.min(self, target, groups=None)

Computes the minimum of the values of the column target in each group.

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
    ...
    
    min_spend_by_cat = (
        future["transactions"]
        .groupBy("category")
        .min(target="purchase_value")
    )
    
    ...
Parameters

target: str
A column to apply the grouping operation to.


groups: list[Any]
Default: None
Limits grouping to the list of provided values.

Returns

tuple[numpy.ndarray, list[str]], a tuple with min value of elements per each group and group names.



EventsGroupBy.max(self, target, groups=None)

Computes the maximum of the values of the column target in each group.

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
    ...
    
    max_spend_by_cat = (
        future["transactions"]
        .groupBy("category")
        .max(target="purchase_value")
    )
    
    ...
Parameters

target: str
A column to apply the grouping operation to.


groups: list[Any]
Default: None
Limits grouping to the list of provided values.

Returns

tuple[numpy.ndarray, list[str]], a tuple with max value of elements per each group and group names.



EventsGroupBy.exists(self, groups)

Checks if any of the groups is empty, returning 1 if there are events for the group, or 0 if not .

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
    ...
    
    # group future purchase events by brand, filter to Nike & Adidas,
    # and return a binary indicator for their presence
    target_purchases = (
        future["product_buy"]
        .groupBy("brand")
        .exists(groups=["Nike", "Adidas"])
    )
    
    ...
Parameters

groups: list[Any]
Default: None
Limits grouping to the list of provided values.

Returns

tuple[numpy.ndarray, list[str]], a tuple with array indicating existence of the elements per each group and group names.



EventsGroupBy.apply(self, func, default_value, target, groups=None)

Applies a function to each group.

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
    ...
    
    # first, normalize brand names to lowercase with apply + lambda,
    # then group and compute normalized counts
    purchase_target = (
        future["product.buy"]
        .apply(lambda x: x.lower(), target="brand")
        .groupBy("brand")
        .count(normalize=True)
    )
    
    ...
Parameters

func: Callable[[np.ndarray], Any]
Function to apply.


default_value: Any
The default output value for empty groups will only be used if the groups parameter is set. Please note that the result returned by func will be cast to the type of default_value. Therefore, ensure that the type of default_value matches the expected output — for example, avoid setting an integer if your column contains floating-point values.


target: str
Column to apply the grouping operation to.


groups: list[Any]
Default: None
Limits grouping to the list of provided values.

Returns

tuple[Any, fist[str]], a tuple with values returned by function per each group and group names.