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
VastColumncorresponding 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
VastColumncorresponding 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
VastColumnincluding the items rating. If theratingtype istuple, 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
VastColumnwhich must includes the same id asitem_id.r_name is an input
VastColumnincluding the items rating.ts (str, optional) – TS (Time Series)
VastColumnused to order the data. TheVastColumntype 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 whentsis 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 whentsis greater than November 1993 the 3rd.
- Returns:
The
VastFrameof the recommendation.- Return type:
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 fromvastorbitare used as intended without interfering with functions from other libraries.Let us create a
VastFramewhich 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], }, )
123transaction_idIntegerAbcitem_idVarchar(1)123ratingInteger1 3 A 9 2 3 B 4 3 2 C 2 4 1 C 1 5 3 C 3 6 2 B 6 7 1 A 8 8 1 B 5 Rows: 1-8 | Columns: 3We 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", )
Abcitem1Varchar(1)Abcitem2Varchar(1)123cntBigint123score1Double123score2Double123rankBigint1 A B 2 8.5 5.0 1 2 A C 2 8.5 2.0 2 3 B A 2 5.0 8.5 1 4 B C 3 5.0 2.0 2 5 C A 2 2.0 8.5 1 6 C B 3 2.0 5.0 2 Rows: 1-6 | Columns: 6Note
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", ], }, )
123transaction_idIntegerAbcitem_idVarchar(1)123ratingIntegerAbcdateVarchar(9)1 3 A 9 2021-1-21 2 1 C 1 2021-1-1 3 1 B 5 2021-1-1 4 3 C 3 2021-1-21 5 2 B 6 2021-1-4 6 2 C 2 2021-1-4 7 1 A 8 2021-1-1 8 3 B 4 2021-1-21 Rows: 1-8 | Columns: 4Then 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", )
Abcitem1Varchar(1)Abcitem2Varchar(1)123cntBigint123score1Double123score2Double123rankBigint1 B A 2 5.0 8.5 1 2 B C 3 5.0 2.0 2 3 A B 2 8.5 5.0 1 4 A C 2 8.5 2.0 2 5 C A 2 2.0 8.5 1 6 C B 3 2.0 5.0 2 Rows: 1-6 | Columns: 6See also
VastFrame.add_duplicates(): Add duplicates of values based on weights.