Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Recommenders team Recommenders SARSingleNode Fit

From Leeroopedia


Knowledge Sources
Domains Recommender Systems, Collaborative Filtering, Model Training
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for training a SAR collaborative filtering model on user-item interaction data provided by the recommenders library.

Description

The SARSingleNode.fit method is the main training entry point for the SAR model. Given a pandas DataFrame of user-item-rating interactions (with no duplicates), it performs the following steps:

  1. Validates that the input contains no duplicate (user, item) pairs.
  2. Generates continuous index mappings for users and items if not already set.
  3. Validates that the rating column is numeric.
  4. Optionally applies time-decay weighting to ratings based on timestamps.
  5. Builds the user-affinity sparse matrix via compute_affinity_matrix.
  6. Computes the item co-occurrence matrix via compute_cooccurrence_matrix.
  7. Extracts item frequencies from the co-occurrence diagonal.
  8. Derives the item-similarity matrix using the configured similarity metric (jaccard, cosine, lift, cooccurrence, inclusion index, mutual information, or lexicographers mutual information).
  9. If normalization is enabled, computes an additional unity-rating affinity matrix for score scaling.

After fit completes, the model's user_affinity, item_similarity, and item_frequencies attributes are populated.

Usage

Call this method after instantiating a SARSingleNode model and before calling recommend_k_items or predict. The input DataFrame must contain user, item, and rating columns, and optionally a timestamp column if time-decay is enabled.

Code Reference

Source Location

  • Repository: recommenders
  • File: recommenders/models/sar/sar_singlenode.py
  • Lines: L226-L323

Signature

def fit(self, df: pd.DataFrame) -> None

Import

from recommenders.models.sar.sar_singlenode import SARSingleNode

model = SARSingleNode(similarity_type="jaccard")
model.fit(train_df)

I/O Contract

Inputs

Name Type Required Description
df pd.DataFrame Yes User-item-rating interaction data. Must contain columns matching col_user, col_item, and col_rating (as configured in __init__). Must not contain duplicate (user, item) pairs. If timedecay_formula is True, must also contain a col_timestamp column.

Outputs

Name Type Description
return None The method modifies the model instance in-place. After fitting, the following attributes are populated:
self.user_affinity scipy.sparse matrix Sparse user-affinity matrix of shape (n_users, n_items).
self.item_similarity scipy.sparse matrix Sparse item-similarity matrix of shape (n_items, n_items).
self.item_frequencies numpy.ndarray Array of item frequency counts (diagonal of the co-occurrence matrix).

Usage Examples

Basic Usage

from recommenders.models.sar.sar_singlenode import SARSingleNode

# Initialize and train the model
model = SARSingleNode(
    col_user="userID",
    col_item="itemID",
    col_rating="rating",
    similarity_type="jaccard",
)
model.fit(train_df)

# After fit, model attributes are populated
print(model.user_affinity.shape)    # (n_users, n_items)
print(model.item_similarity.shape)  # (n_items, n_items)
print(model.item_frequencies.shape) # (n_items,)

# Training with time decay
model_td = SARSingleNode(
    col_user="userID",
    col_item="itemID",
    col_rating="rating",
    col_timestamp="timestamp",
    similarity_type="cosine",
    timedecay_formula=True,
    time_decay_coefficient=15,
)
model_td.fit(train_df)

Dependencies

  • numpy - Numerical operations, dtype validation
  • pandas - DataFrame input handling and manipulation
  • scipy.sparse - Sparse matrix construction and multiplication

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment