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: Get top K similar items to provided seed items based on similarity metric defined.
  This method will take a set of items and use them to recommend the most similar items to that set
  based on the similarity matrix fit during training.
  This allows recommendations for cold-users (unseen during training), note - the model is not updated.
  
  The following options are possible based on information provided in the items input:
  1. Single user or seed of items: only item column (ratings are assumed to be 1)
  2. Single user or seed of items w/ ratings: item column and rating column
  3. Separate users or seeds of items: item and user column (user ids are only used to separate item sets)
  4. Separate users or seeds of items with ratings: item, user and rating columns provided
  
  Args:
      items (pandas.DataFrame): DataFrame with item, user (optional), and rating (optional) columns
      top_k (int): number of top items to recommend
      sort_top_k (bool): flag to sort top k results
  
  Returns:
      pandas.DataFrame: sorted top k recommendation items
docstr_tok: ["Get","top","K","similar","items","to","provided","seed","items","based","on","similarity","metric","defined",".","This","method","will","take","a","set","of","items","and","use","them","to","recommend","the","most","similar","items","to","that","set","based","on","the","similarity","matrix","fit","during","training",".","This","allows","recommendations","for","cold-users","(","unseen","during","training",")",",","note","-","the","model","is","not","updated",".","The","following","options","are","possible","based","on","information","provided","in","the","items","input",":","1",".","Single","user","or","seed","of","items",":","only","item","column","(","ratings","are","assumed","to","be","1",")","2",".","Single","user","or","seed","of","items","w\/","ratings",":","item","column","and","rating","column","3",".","Separate","users","or","seeds","of","items",":","item","and","user","column","(","user","ids","are","only","used","to","separate","item","sets",")","4",".","Separate","users","or","seeds","of","items","with","ratings",":","item",",","user","and","rating","columns","provided","Args",":","items","(","pandas.DataFrame",")",":","DataFrame","with","item",",","user","(","optional",")",",","and","rating","(","optional",")","columns","top_k","(","int",")",":","number","of","top","items","to","recommend","sort_top_k","(","bool",")",":","flag","to","sort","top","k","results","Returns",":","pandas.DataFrame",":","sorted","top","k","recommendation","items"]
code: def get_item_based_topk(self, items, top_k=10, sort_top_k=True):
      """Get top K similar items to provided seed items based on similarity metric defined.
      This method will take a set of items and use them to recommend the most similar items to that set
      based on the similarity matrix fit during training.
      This allows recommendations for cold-users (unseen during training), note - the model is not updated.
  
      The following options are possible based on information provided in the items input:
      1. Single user or seed of items: only item column (ratings are assumed to be 1)
      2. Single user or seed of items w/ ratings: item column and rating column
      3. Separate users or seeds of items: item and user column (user ids are only used to separate item sets)
      4. Separate users or seeds of items with ratings: item, user and rating columns provided
  
      Args:
          items (pandas.DataFrame): DataFrame with item, user (optional), and rating (optional) columns
          top_k (int): number of top items to recommend
          sort_top_k (bool): flag to sort top k results
  
      Returns:
          pandas.DataFrame: sorted top k recommendation items
      """
  
      # convert item ids to indices
      item_ids = np.asarray(
          list(
              map(
                  lambda item: self.item2index.get(item, np.NaN),
                  items[self.col_item].values,
              )
          )
      )
  
      # if no ratings were provided assume they are all 1
      if self.col_rating in items.columns:
          ratings = items[self.col_rating]
      else:
          ratings = pd.Series(np.ones_like(item_ids))
  
      # create local map of user ids
      if self.col_user in items.columns:
          test_users = items[self.col_user]
          user2index = {
              x[1]: x[0]
              for x in enumerate(items[self.col_user].unique())
          }
          user_ids = test_users.map(user2index)
      else:
          # if no user column exists assume all entries are for a single user
          test_users = pd.Series(np.zeros_like(item_ids))
          user_ids = test_users
      n_users = user_ids.drop_duplicates().shape[0]
  
      # generate pseudo user affinity using seed items
      pseudo_affinity = sparse.coo_matrix(
          (ratings, (user_ids, item_ids)), shape=(n_users, self.n_items)
      ).tocsr()
  
      # calculate raw scores with a matrix multiplication
      test_scores = pseudo_affinity.dot(self.item_similarity)
  
      # remove items in the seed set so recommended items are novel
      test_scores[user_ids, item_ids] = -np.inf
  
      top_items, top_scores = get_top_k_scored_items(
          scores=test_scores, top_k=top_k, sort_top_k=sort_top_k
      )
  
      df = pd.DataFrame(
          {
              self.col_user: np.repeat(
                  test_users.drop_duplicates().values,
                  top_items.shape[1],
              ),
              self.col_item: [
                  self.index2item[item] for item in top_items.flatten()
              ],
              self.col_prediction: top_scores.flatten(),
          }
      )
  
      # drop invalid items
      return df.replace(-np.inf, np.nan).dropna()
  
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 9 for calling the SARSingleNode obj's underlying member method code with example usage: obj.get_item_based_topk(items, top_k, sort_top_k) and returns: df