class_docstr: Generate the user/item affinity matrix from a pandas dataframe and vice versa
class_docstr_tok: ["Generate","the","user\/item","affinity","matrix","from","a","pandas","dataframe","and","vice","versa"]
docstr: Generate the user/item index:
  map_users, map_items: dictionaries mapping the original user/item index to matrix indices
  map_back_users, map_back_items: dictionaries to map back the matrix elements to the original
  dataframe indices
  
  Basic mechanics:
  As a first step we retieve the unique elements in the dataset. In this way we can take care
  of either completely missing rows (a user with no ratings) or completely missing columns
  (an item that has not being reviewed by anyone). The original indices in the dataframe are
  then mapped to an ordered, contiguous integer series to generate a compact matrix representation.
  Functions to map back to the original indices are also provided and can be saved in order to use
  a pretrained model.
docstr_tok: ["Generate","the","user\/item","index",":","map_users",",","map_items",":","dictionaries","mapping","the","original","user\/item","index","to","matrix","indices","map_back_users",",","map_back_items",":","dictionaries","to","map","back","the","matrix","elements","to","the","original","dataframe","indices","Basic","mechanics",":","As","a","first","step","we","retieve","the","unique","elements","in","the","dataset",".","In","this","way","we","can","take","care","of","either","completely","missing","rows","(","a","user","with","no","ratings",")","or","completely","missing","columns","(","an","item","that","has","not","being","reviewed","by","anyone",")",".","The","original","indices","in","the","dataframe","are","then","mapped","to","an","ordered",",","contiguous","integer","series","to","generate","a","compact","matrix","representation",".","Functions","to","map","back","to","the","original","indices","are","also","provided","and","can","be","saved","in","order","to","use","a","pretrained","model","."]
code: def _gen_index(self):
      """
      Generate the user/item index:
      map_users, map_items: dictionaries mapping the original user/item index to matrix indices
      map_back_users, map_back_items: dictionaries to map back the matrix elements to the original
      dataframe indices
  
      Basic mechanics:
      As a first step we retieve the unique elements in the dataset. In this way we can take care
      of either completely missing rows (a user with no ratings) or completely missing columns
      (an item that has not being reviewed by anyone). The original indices in the dataframe are
      then mapped to an ordered, contiguous integer series to generate a compact matrix representation.
      Functions to map back to the original indices are also provided and can be saved in order to use
      a pretrained model.
      """
      # sort entries by user index
      self.df_ = self.df.sort_values(by=[self.col_user])
  
      # find unique user and item index
      unique_users = self.df_[self.col_user].unique()
  
      if self.items_list is not None:
          unique_items = self.items_list  # use this list if provided
      else:
          unique_items = self.df_[
              self.col_item
          ].unique()  # otherwise use unique items from DF
  
      self.Nusers = len(unique_users)
      self.Nitems = len(unique_items)
  
      # create a dictionary to map unique users/items to hashed values to generate the matrix
      self.map_users = {x: i for i, x in enumerate(unique_users)}
      self.map_items = {x: i for i, x in enumerate(unique_items)}
  
      # map back functions used to get back the original dataframe
      self.map_back_users = {i: x for i, x in enumerate(unique_users)}
      self.map_back_items = {i: x for i, x in enumerate(unique_items)}
  
      self.df_.loc[:, "hashedItems"] = self.df_[self.col_item].map(
          self.map_items
      )
      self.df_.loc[:, "hashedUsers"] = self.df_[self.col_user].map(
          self.map_users
      )
  
      # optionally save the inverse dictionary to work with trained models
      if self.save_path is not None:
          np.save(self.save_path + "/user_dict", self.map_users)
          np.save(self.save_path + "/item_dict", self.map_items)
  
          np.save(
              self.save_path + "/user_back_dict", self.map_back_users
          )
          np.save(
              self.save_path + "/item_back_dict", self.map_back_items
          )
  
file_path: recommenders/recommenders/datasets/sparse.py
imports: import pandas
  import numpy
  import itertools
  from scipy.sparse import coo_matrix
  import logging
  from recommenders.utils.constants import DEFAULT_USER_COL, DEFAULT_ITEM_COL, DEFAULT_RATING_COL, DEFAULT_PREDICTION_COL
label_desc: Use image node_id 2 for calling the AffinityMatrix obj's underlying member method code with example usage: obj._gen_index() without return types