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 Init

From Leeroopedia


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

Overview

Concrete tool for configuring and initializing a SAR (Smart Adaptive Recommendations) single-node collaborative filtering model provided by the recommenders library.

Description

The SARSingleNode.__init__ method initializes all model parameters and internal state for the SAR algorithm. It validates the chosen similarity type against the set of supported metrics (cooccurrence, cosine, inclusion index, jaccard, lexicographers mutual information, lift, mutual information), converts the time-decay coefficient from days to seconds, and sets up internal data structures (index mappings, affinity matrices, similarity matrices) as None, to be populated during the fit step. It also validates that the threshold is positive and configures whether score normalization is enabled.

Usage

Import and instantiate this class at the model configuration stage of your pipeline, before calling fit to train on data. Choose the similarity type and time-decay settings based on your dataset characteristics and evaluation goals.

Code Reference

Source Location

  • Repository: recommenders
  • File: recommenders/models/sar/sar_singlenode.py
  • Lines: L43-L135

Signature

class SARSingleNode:
    def __init__(
        self,
        col_user=DEFAULT_USER_COL,
        col_item=DEFAULT_ITEM_COL,
        col_rating=DEFAULT_RATING_COL,
        col_timestamp=DEFAULT_TIMESTAMP_COL,
        col_prediction=DEFAULT_PREDICTION_COL,
        similarity_type="jaccard",
        time_decay_coefficient=30,
        time_now=None,
        timedecay_formula=False,
        threshold=1,
        normalize=False,
    )

Import

from recommenders.models.sar.sar_singlenode import SARSingleNode

I/O Contract

Inputs

Name Type Required Description
col_user str No (default: DEFAULT_USER_COL) Column name for user IDs in the input DataFrame.
col_item str No (default: DEFAULT_ITEM_COL) Column name for item IDs in the input DataFrame.
col_rating str No (default: DEFAULT_RATING_COL) Column name for rating values in the input DataFrame.
col_timestamp str No (default: DEFAULT_TIMESTAMP_COL) Column name for timestamps. Required only when timedecay_formula is True.
col_prediction str No (default: DEFAULT_PREDICTION_COL) Column name used for prediction output.
similarity_type str No (default: "jaccard") Item-item similarity metric. One of: "cooccurrence", "cosine", "inclusion index", "jaccard", "lexicographers mutual information", "lift", "mutual information".
time_decay_coefficient float No (default: 30) Half-life in days for time-decay weighting. Internally converted to seconds.
time_now int or None No (default: None) Reference timestamp for time-decay calculation. If None, the maximum timestamp in the data is used during fit.
timedecay_formula bool No (default: False) Whether to apply exponential time-decay weighting to ratings.
threshold int No (default: 1) Minimum item-item co-occurrence count. Pairs below this threshold are set to zero. Must be >= 1.
normalize bool No (default: False) Whether to normalize prediction scores to the scale of the original ratings.

Outputs

Name Type Description
return SARSingleNode Initialized SAR model instance with internal state set to None (user_affinity, item_similarity, item_frequencies, user2index, item2index, etc.), ready for training via fit().

Usage Examples

Basic Usage

from recommenders.models.sar.sar_singlenode import SARSingleNode

# Initialize with default Jaccard similarity
model = SARSingleNode(
    col_user="userID",
    col_item="itemID",
    col_rating="rating",
    col_timestamp="timestamp",
    similarity_type="jaccard",
)

# Initialize with cosine similarity and time decay
model = SARSingleNode(
    similarity_type="cosine",
    timedecay_formula=True,
    time_decay_coefficient=15,
)

# Initialize with lift similarity and score normalization
model = SARSingleNode(
    similarity_type="lift",
    normalize=True,
    threshold=3,
)

Dependencies

  • numpy - Numerical operations and array manipulation
  • pandas - DataFrame handling for input data
  • scipy.sparse - Sparse matrix representations for affinity and similarity matrices

Related Pages

Implements Principle

Requires Environment

Page Connections

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