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: Map back the user/affinity matrix to a pd dataframe
  
  Args:
      X (numpy.ndarray, int32): user/item affinity matrix
      kind (string): specify if the output values are ratings or predictions
  Returns:
      pandas.DataFrame: the generated pandas dataframe
docstr_tok: ["Map","back","the","user\/affinity","matrix","to","a","pd","dataframe","Args",":","X","(","numpy.ndarray",",","int32",")",":","user\/item","affinity","matrix","kind","(","string",")",":","specify","if","the","output","values","are","ratings","or","predictions","Returns",":","pandas.DataFrame",":","the","generated","pandas","dataframe"]
code: def map_back_sparse(self, X, kind):
      """Map back the user/affinity matrix to a pd dataframe
  
      Args:
          X (numpy.ndarray, int32): user/item affinity matrix
          kind (string): specify if the output values are ratings or predictions
      Returns:
          pandas.DataFrame: the generated pandas dataframe
      """
      m, n = X.shape
  
      # 1) Create a DF from a sparse matrix
      # obtain the non zero items
      items = [
          np.asanyarray(np.where(X[i, :] != 0)).flatten()
          for i in range(m)
      ]
      ratings = [
          X[i, items[i]] for i in range(m)
      ]  # obtain the non-zero ratings
  
      # Creates user ids following the DF format
      userids = []
      for i in range(0, m):
          userids.extend([i] * len(items[i]))
  
      # Flatten the lists to follow the DF input format
      items = list(itertools.chain.from_iterable(items))
      ratings = list(itertools.chain.from_iterable(ratings))
  
      if kind == "ratings":
          col_out = self.col_rating
      else:
          col_out = self.col_pred
  
      # create a df
      out_df = pd.DataFrame.from_dict(
          {
              self.col_user: userids,
              self.col_item: items,
              col_out: ratings,
          }
      )
  
      # 2) map back user/item ids to their original value
  
      out_df[self.col_user] = out_df[self.col_user].map(
          self.map_back_users
      )
      out_df[self.col_item] = out_df[self.col_item].map(
          self.map_back_items
      )
  
      return out_df
  
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 4 for calling the AffinityMatrix obj's underlying member method code with example usage: obj.map_back_sparse(X, kind) and returns: out_df