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: Main fit method for SAR.
  
  .. note::
  
  Please make sure that `df` has no duplicates.
  
  Args:
      df (pandas.DataFrame): User item rating dataframe (without duplicates).
docstr_tok: ["Main","fit","method","for","SAR",".","..","note",":",":","Please","make","sure","that","`","df","`","has","no","duplicates",".","Args",":","df","(","pandas.DataFrame",")",":","User","item","rating","dataframe","(","without","duplicates",")","."]
code: def fit(self, df):
      """Main fit method for SAR.
  
      .. note::
  
      Please make sure that `df` has no duplicates.
  
      Args:
          df (pandas.DataFrame): User item rating dataframe (without duplicates).
      """
      select_columns = [self.col_user, self.col_item, self.col_rating]
      if self.time_decay_flag:
          select_columns += [self.col_timestamp]
  
      if df[select_columns].duplicated().any():
          raise ValueError(
              "There should not be duplicates in the dataframe"
          )
  
      # generate continuous indices if this hasn't been done
      if self.index2item is None:
          self.set_index(df)
  
      logger.info("Collecting user affinity matrix")
      if not np.issubdtype(df[self.col_rating].dtype, np.number):
          raise TypeError("Rating column data type must be numeric")
  
      # copy the DataFrame to avoid modification of the input
      temp_df = df[select_columns].copy()
  
      if self.time_decay_flag:
          logger.info("Calculating time-decayed affinities")
          temp_df = self.compute_time_decay(
              df=temp_df, decay_column=self.col_rating
          )
  
      logger.info("Creating index columns")
      # add mapping of user and item ids to indices
      temp_df.loc[:, self.col_item_id] = temp_df[self.col_item].apply(
          lambda item: self.item2index.get(item, np.NaN)
      )
      temp_df.loc[:, self.col_user_id] = temp_df[self.col_user].apply(
          lambda user: self.user2index.get(user, np.NaN)
      )
  
      if self.normalize:
          self.rating_min = temp_df[self.col_rating].min()
          self.rating_max = temp_df[self.col_rating].max()
          logger.info("Calculating normalization factors")
          temp_df[self.col_unity_rating] = 1.0
          if self.time_decay_flag:
              temp_df = self.compute_time_decay(
                  df=temp_df, decay_column=self.col_unity_rating
              )
          self.unity_user_affinity = self.compute_affinity_matrix(
              df=temp_df, rating_col=self.col_unity_rating
          )
  
      # affinity matrix
      logger.info("Building user affinity sparse matrix")
      self.user_affinity = self.compute_affinity_matrix(
          df=temp_df, rating_col=self.col_rating
      )
  
      # calculate item co-occurrence
      logger.info("Calculating item co-occurrence")
      item_cooccurrence = self.compute_cooccurrence_matrix(df=temp_df)
  
      # free up some space
      del temp_df
  
      # creates an array with the frequency of every unique item
      self.item_frequencies = item_cooccurrence.diagonal()
  
      logger.info("Calculating item similarity")
      if self.similarity_type == SIM_COOCCUR:
          logger.info("Using co-occurrence based similarity")
          self.item_similarity = item_cooccurrence
      elif self.similarity_type == SIM_COSINE:
          logger.info("Using cosine similarity")
          self.item_similarity = cosine_similarity(item_cooccurrence)
      elif self.similarity_type == SIM_INCLUSION_INDEX:
          logger.info("Using inclusion index")
          self.item_similarity = inclusion_index(item_cooccurrence)
      elif self.similarity_type == SIM_JACCARD:
          logger.info("Using jaccard based similarity")
          self.item_similarity = jaccard(item_cooccurrence)
      elif (
          self.similarity_type == SIM_LEXICOGRAPHERS_MUTUAL_INFORMATION
      ):
          logger.info(
              "Using lexicographers mutual information similarity"
          )
          self.item_similarity = lexicographers_mutual_information(
              item_cooccurrence
          )
      elif self.similarity_type == SIM_LIFT:
          logger.info("Using lift based similarity")
          self.item_similarity = lift(item_cooccurrence)
      elif self.similarity_type == SIM_MUTUAL_INFORMATION:
          logger.info("Using mutual information similarity")
          self.item_similarity = mutual_information(item_cooccurrence)
      else:
          raise ValueError(
              "Unknown similarity type: {}".format(self.similarity_type)
          )
  
      # free up some space
      del item_cooccurrence
  
      logger.info("Done training")
  
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 6 for calling the SARSingleNode obj's underlying member method code with example usage: obj.fit(df) without return types