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:
  ...
    
	# here we get price feature from the datasource transactions
  # Note, it is ModalityEvents object, not the value of the feature
	future['product_buy'].groupBy('brand').exists(groups=['Nike', 'Adidas'])
  ...

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:
  ...
	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:
  ...
	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:
  ...
	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:
  ...
	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:
  ...
	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.

def target_fn(history: Events, future: Events, attributes: Attributes, ctx: Dict) -> np.ndarray:
  ...
	future['transactions'].groupBy('brand').exists(groups=TARGET_BRANDS)
  ...
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:
  ...
	future['product_buy'].apply(lambda x: x.lower()), target='brand')
  ...
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.