Skip to content

n_last

logger = logging.getLogger(__name__) module-attribute

NLastInteractionSplitter

Bases: Splitter

Splits the n most recent interactions of a user into the second return value, and earlier interactions into the first.

Parameters:

Name Type Description Default
n int

Number of most recent actions to assign to the second return value.

required
n_seq_data int

Number of last interactions to provide as unlabeled data for model to make prediction. Defaults to 1.

1

Raises:

Type Description
ValueError

If n is less than 1, as this would cause the ground truth data to be empty.

Source code in src/streamsight/settings/splitters/n_last.py
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
class NLastInteractionSplitter(Splitter):
    """Splits the n most recent interactions of a user into the second return value,
    and earlier interactions into the first.

    Args:
        n (int): Number of most recent actions to assign to the second return value.
        n_seq_data (int, optional): Number of last interactions to provide as unlabeled data
            for model to make prediction. Defaults to 1.

    Raises:
        ValueError: If n is less than 1, as this would cause the ground truth data to be empty.
    """

    def __init__(self, n: int, n_seq_data: int = 1) -> None:
        super().__init__()
        if n < 1:
            raise ValueError(
                f"n must be greater than 0, got {n}. "
                f"Values for n < 1 will cause the ground truth data to be empty."
            )
        self.n = n
        self.n_seq_data = n_seq_data

    def split(self, data: InteractionMatrix) -> tuple[InteractionMatrix, InteractionMatrix]:
        future_interaction = data.get_users_n_last_interaction(self.n)
        past_interaction = data - future_interaction
        past_interaction = past_interaction.get_users_n_last_interaction(self.n_seq_data)
        logger.debug(f"{self.identifier} has complete split")

        return past_interaction, future_interaction

n = n instance-attribute

n_seq_data = n_seq_data instance-attribute

name property

Return the class name of the splitter.

Returns:

Type Description
str

The splitter class name.

identifier property

Return a string identifier including the splitter's parameters.

The identifier includes the class name and a comma-separated list of attribute name/value pairs from self.__dict__.

Returns:

Type Description
str

Identifier string like Name(k1=v1,k2=v2).

split(data)

Source code in src/streamsight/settings/splitters/n_last.py
33
34
35
36
37
38
39
def split(self, data: InteractionMatrix) -> tuple[InteractionMatrix, InteractionMatrix]:
    future_interaction = data.get_users_n_last_interaction(self.n)
    past_interaction = data - future_interaction
    past_interaction = past_interaction.get_users_n_last_interaction(self.n_seq_data)
    logger.debug(f"{self.identifier} has complete split")

    return past_interaction, future_interaction