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 affinity matrix.
  
  As a first step, two new columns are added to the input DF, containing the index maps
  generated by the gen_index() method. The new indices, together with the ratings, are
  then used to generate the user/item affinity matrix using scipy's sparse matrix method
  coo_matrix; for reference see:
  https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html.
  The input format is: `coo_matrix((data, (rows, columns)), shape=(rows, columns))`
  
  Returns:
      scipy.sparse.coo_matrix: User-affinity matrix of dimensions (Nusers, Nitems) in numpy format.
      Unrated movies are assigned a value of 0.
docstr_tok: ["Generate","the","user\/item","affinity","matrix",".","As","a","first","step",",","two","new","columns","are","added","to","the","input","DF",",","containing","the","index","maps","generated","by","the","gen_index","(",")","method",".","The","new","indices",",","together","with","the","ratings",",","are","then","used","to","generate","the","user\/item","affinity","matrix","using","scipy","'s","sparse","matrix","method","coo_matrix",";","for","reference","see",":","https",":","\/\/docs.scipy.org\/doc\/scipy\/reference\/generated\/scipy.sparse.coo_matrix.html",".","The","input","format","is",":","`","coo_matrix","(","(","data",",","(","rows",",","columns",")",")",",","shape=","(","rows",",","columns",")",")","`","Returns",":","scipy.sparse.coo_matrix",":","User-affinity","matrix","of","dimensions","(","Nusers",",","Nitems",")","in","numpy","format",".","Unrated","movies","are","assigned","a","value","of","0","."]
code: def gen_affinity_matrix(self):
      """Generate the user/item affinity matrix.
  
      As a first step, two new columns are added to the input DF, containing the index maps
      generated by the gen_index() method. The new indices, together with the ratings, are
      then used to generate the user/item affinity matrix using scipy's sparse matrix method
      coo_matrix; for reference see:
      https://docs.scipy.org/doc/scipy/reference/generated/scipy.sparse.coo_matrix.html.
      The input format is: `coo_matrix((data, (rows, columns)), shape=(rows, columns))`
  
      Returns:
          scipy.sparse.coo_matrix: User-affinity matrix of dimensions (Nusers, Nitems) in numpy format.
          Unrated movies are assigned a value of 0.
      """
  
      log.info("Generating the user/item affinity matrix...")
  
      self._gen_index()
  
      ratings = self.df_[self.col_rating]  # ratings
      itm_id = self.df_["hashedItems"]  # itm_id serving as columns
      usr_id = self.df_["hashedUsers"]  # usr_id serving as rows
  
      # generate a sparse matrix representation using scipy's coo_matrix and convert to array format
      self.AM = coo_matrix(
          (ratings, (usr_id, itm_id)), shape=(self.Nusers, self.Nitems)
      ).toarray()
  
      zero = (self.AM == 0).sum()  # number of unrated items
      total = (
          self.AM.shape[0] * self.AM.shape[1]
      )  # number of elements in the matrix
      sparsness = (
          zero / total * 100
      )  # Percentage of zeros in the matrix
  
      log.info(
          "Matrix generated, sparseness percentage: %d" % sparsness
      )
  
      return self.AM, self.map_users, self.map_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 3 for calling the AffinityMatrix obj's underlying member method code with example usage: obj.gen_affinity_matrix() and returns: self,  self,  self