Loading...

vastorbit.VastFrame.recommend

VastFrame.recommend(unique_id: str, item_id: str, method: Literal['count', 'avg', 'median'] = 'count', rating: str | tuple = '', ts: str | None = None, start_date: Annotated[bool | float | str | timedelta | datetime, 'Python Scalar'] = '', end_date: Annotated[bool | float | str | timedelta | datetime, 'Python Scalar'] = '') VastFrame

Recommend items based on the Collaborative Filtering (CF) technique. The implementation is the same as APRIORI algorithm, but is limited to pairs of items.

Parameters:
  • unique_id (str) – Input VastColumn corresponding to a unique ID. It serves as a primary key in another dataset. In our context, it represents an operation, such as a basket ID, which includes multiple sub-transactions.

  • item_id (str) – Input VastColumn corresponding to an item ID. It is a secondary key used to compute the different pairs.

  • method (str, optional) –

    Method used to recommend.

    • count:

      Each item will be recommended based on frequencies of the different pairs of items.

    • avg:

      Each item will be recommended based on the average rating of the different item pairs with a differing second element.

    • median:

      Each item will be recommended based on the median rating of the different item pairs with a differing second element.

  • rating (str | tuple, optional) –

    Input VastColumn including the items rating. If the rating type is tuple, it must be composed of 3 elements:

    (r_vdf, r_item_id, r_name) where:

    r_vdf is an input VastFrame.

    r_item_id is an input VastColumn which must includes the same id as item_id.

    r_name is an input VastColumn including the items rating.

  • ts (str, optional) – TS (Time Series) VastColumn used to order the data. The VastColumn type must be date (date, datetime, timestamp…) or numerical.

  • start_date (str | PythonNumber | date, optional) – Input Start Date. For example, time = '03-11-1993' will filter the data when ts is less than November 1993 the 3rd.

  • end_date (str | PythonNumber | date, optional) – Input End Date. For example, time = '03-11-1993' will filter the data when ts is greater than November 1993 the 3rd.

Returns:

The VastFrame of the recommendation.

Return type:

VastFrame

Examples

Let’s begin by importing vastorbit.

import vastorbit as vo

Hint

By assigning an alias to vastorbit, we mitigate the risk of code collisions with other libraries. This precaution is necessary because vastorbit uses commonly known function names like “average” and “median”, which can potentially lead to naming conflicts. The use of an alias ensures that the functions from vastorbit are used as intended without interfering with functions from other libraries.

Let us create a VastFrame which has some purchase transaction data:

  • transaction_id:

    Unique ID for a transaction.

  • item_id:

    The unique ID for different items that were purchased.

  • rating:

    Rating provided by the user for the item purchased.

vdf = vo.VastFrame(
    {
        "transaction_id": [1, 1, 1, 2, 2, 3, 3, 3],
        "item_id": ["A", "B", "C", "B", "C", "A", "B", "C"],
        "rating": [8, 5, 1, 6, 2, 9, 4, 3],
    },
)
123
transaction_id
Integer
Abc
item_id
Varchar(1)
123
rating
Integer
13A9
23B4
32C2
41C1
53C3
62B6
71A8
81B5
Rows: 1-8 | Columns: 3

We can easily create the recommend table from the above data:

recommendations = vdf.recommend(
    unique_id = "transaction_id",
    item_id = "item_id",
    method = "avg",
    rating = "rating",
)
Abc
item1
Varchar(1)
Abc
item2
Varchar(1)
123
cnt
Bigint
123
score1
Double
123
score2
Double
123
rank
Bigint
1AB28.55.01
2AC28.52.02
3BA25.08.51
4BC35.02.02
5CA22.08.51
6CB32.05.02
Rows: 1-6 | Columns: 6

Note

This function is highly useful for basket analysis and can be employed to derive valuable recommendations.

Let’s look at another example involving timestamp values:

# Create a VastFrame with the transaction data
vdf = vo.VastFrame(
    {
        "transaction_id": [1, 1, 1, 2, 2, 3, 3, 3],
        "item_id": ["A", "B", "C", "B", "C", "A", "B", "C"],
        "rating": [8, 5, 1, 6, 2, 9, 4, 3],
        "date": [
            "2021-1-1",
            "2021-1-1",
            "2021-1-1",
            "2021-1-4",
            "2021-1-4",
            "2021-1-21",
            "2021-1-21",
            "2021-1-21",
        ],
    },
)
123
transaction_id
Integer
Abc
item_id
Varchar(1)
123
rating
Integer
Abc
date
Varchar(9)
13A92021-1-21
21C12021-1-1
31B52021-1-1
43C32021-1-21
52B62021-1-4
62C22021-1-4
71A82021-1-1
83B42021-1-21
Rows: 1-8 | Columns: 4

Then we can use the timestamp column to filter the recommendation results:

recommendations = vdf.recommend(
    unique_id = "transaction_id",
    item_id = "item_id",
    method = "avg",
    rating = "rating",
    ts = "date",
    start_date = "2021-1-1",
    end_date = "2021-1-5",
)
Abc
item1
Varchar(1)
Abc
item2
Varchar(1)
123
cnt
Bigint
123
score1
Double
123
score2
Double
123
rank
Bigint
1BA25.08.51
2BC35.02.02
3AB28.55.01
4AC28.52.02
5CA22.08.51
6CB32.05.02
Rows: 1-6 | Columns: 6

See also

VastFrame.add_duplicates() : Add duplicates of values based on weights.