class_docstr: Simple Algorithm for Recommendations (SAR) implementation
  
  SAR is a fast scalable adaptive algorithm for personalized recommendations based on user transaction history
  and items description. The core idea behind SAR is to recommend items like those that a user already has
  demonstrated an affinity to. It does this by 1) estimating the affinity of users for items, 2) estimating
  similarity across items, and then 3) combining the estimates to generate a set of recommendations for a given user.
class_docstr_tok: ["Simple","Algorithm","for","Recommendations","(","SAR",")","implementation","SAR","is","a","fast","scalable","adaptive","algorithm","for","personalized","recommendations","based","on","user","transaction","history","and","items","description",".","The","core","idea","behind","SAR","is","to","recommend","items","like","those","that","a","user","already","has","demonstrated","an","affinity","to",".","It","does","this","by","1",")","estimating","the","affinity","of","users","for","items",",","2",")","estimating","similarity","across","items",",","and","then","3",")","combining","the","estimates","to","generate","a","set","of","recommendations","for","a","given","user","."]
docstr: Affinity matrix.
  
  The user-affinity matrix can be constructed by treating the users and items as
  indices in a sparse matrix, and the events as the data. Here, we're treating
  the ratings as the event weights.  We convert between different sparse-matrix
  formats to de-duplicate user-item pairs, otherwise they will get added up.
  
  Args:
      df (pandas.DataFrame): Indexed df of users and items
      rating_col (str): Name of column to use for ratings
  
  Returns:
      sparse.csr: Affinity matrix in Compressed Sparse Row (CSR) format.
docstr_tok: ["Affinity","matrix",".","The","user-affinity","matrix","can","be","constructed","by","treating","the","users","and","items","as","indices","in","a","sparse","matrix",",","and","the","events","as","the","data",".","Here",",","we","'re","treating","the","ratings","as","the","event","weights",".","We","convert","between","different","sparse-matrix","formats","to","de-duplicate","user-item","pairs",",","otherwise","they","will","get","added","up",".","Args",":","df","(","pandas.DataFrame",")",":","Indexed","df","of","users","and","items","rating_col","(","str",")",":","Name","of","column","to","use","for","ratings","Returns",":","sparse.csr",":","Affinity","matrix","in","Compressed","Sparse","Row","(","CSR",")","format","."]
code: def compute_affinity_matrix(self, df, rating_col):
      """Affinity matrix.
  
      The user-affinity matrix can be constructed by treating the users and items as
      indices in a sparse matrix, and the events as the data. Here, we're treating
      the ratings as the event weights.  We convert between different sparse-matrix
      formats to de-duplicate user-item pairs, otherwise they will get added up.
  
      Args:
          df (pandas.DataFrame): Indexed df of users and items
          rating_col (str): Name of column to use for ratings
  
      Returns:
          sparse.csr: Affinity matrix in Compressed Sparse Row (CSR) format.
      """
  
      return sparse.coo_matrix(
          (
              df[rating_col],
              (df[self.col_user_id], df[self.col_item_id]),
          ),
          shape=(self.n_users, self.n_items),
      ).tocsr()
  
file_path: recommenders/recommenders/models/sar/sar_singlenode.py
imports: import numpy
  import pandas
  import logging
  from scipy import sparse
  from recommenders.utils.python_utils import cosine_similarity, inclusion_index, jaccard, lexicographers_mutual_information, lift, mutual_information, exponential_decay, get_top_k_scored_items, rescale
  from recommenders.utils import constants
label_desc: Use image node_id 2 for calling the SARSingleNode obj's underlying member method code with example usage: obj.compute_affinity_matrix(df, rating_col) and returns: sparse