Implementation:Recommenders team Recommenders SARSingleNode Recommend K Items
| Knowledge Sources | |
|---|---|
| Domains | Recommender Systems, Collaborative Filtering, Top-K Recommendation |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for generating top-K item recommendations for users from a trained SAR model provided by the recommenders library.
Description
The SARSingleNode.recommend_k_items method generates personalized top-K item recommendations for all users present in a test DataFrame. It first calls the internal score method to compute the full score matrix (user_affinity x item_similarity), optionally removing already-seen items. It then uses get_top_k_scored_items to efficiently select the K highest-scoring items per user. The results are returned as a flat DataFrame with user, item, and prediction score columns. Invalid scores (negative infinity from removed items) are dropped from the output.
Usage
Call this method after training a SAR model with fit to produce recommendation lists. Pass the test DataFrame containing the users for whom you want recommendations. The output can be directly passed to evaluation functions for computing ranking metrics.
Code Reference
Source Location
- Repository: recommenders
- File:
recommenders/models/sar/sar_singlenode.py - Lines: L522-L552
Signature
def recommend_k_items(
self,
test: pd.DataFrame,
top_k: int = 10,
sort_top_k: bool = True,
remove_seen: bool = False,
) -> pd.DataFrame
Import
from recommenders.models.sar.sar_singlenode import SARSingleNode
model = SARSingleNode(similarity_type="jaccard")
model.fit(train_df)
top_k_df = model.recommend_k_items(test_df, top_k=10)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| test | pd.DataFrame | Yes | DataFrame containing users for whom to generate recommendations. Must contain a col_user column matching the model's configuration. |
| top_k | int | No (default: 10) | Number of top items to recommend per user. |
| sort_top_k | bool | No (default: True) | Whether to sort the top-K results by predicted score in descending order. |
| remove_seen | bool | No (default: False) | Whether to exclude items that the user has already interacted with in the training data. |
Outputs
| Name | Type | Description |
|---|---|---|
| return | pd.DataFrame | DataFrame with columns [col_user, col_item, col_prediction] containing at most top_k recommendations per user, sorted by prediction score if sort_top_k is True. Rows with invalid scores (NaN, -inf) are dropped. |
Usage Examples
Basic Usage
from recommenders.models.sar.sar_singlenode import SARSingleNode
# Train the model
model = SARSingleNode(
col_user="userID",
col_item="itemID",
col_rating="rating",
similarity_type="jaccard",
)
model.fit(train_df)
# Generate top-10 recommendations for test users
top_k = model.recommend_k_items(test_df, top_k=10, remove_seen=True)
print(top_k.head())
# Output:
# userID itemID prediction
# 0 1 302 4.567891
# 1 1 258 4.234567
# ...
# Generate top-5 unsorted recommendations
top_5 = model.recommend_k_items(test_df, top_k=5, sort_top_k=False)
Dependencies
- numpy - Array operations, argpartition for top-K selection
- pandas - DataFrame construction for output