code: def get_sparse_matrix(session, key_table, cand_lists, key=None):
      
      result = []
      cand_lists = cand_lists if isinstance(cand_lists, (list, tuple)) else [cand_lists]
  
      for cand_list in cand_lists:
          if len(cand_list) == 0:
              raise ValueError("cand_lists contain empty cand_list.")
          candidate_class = cand_list[0].__tablename__
  
          # Keys are used as a global index
          if key:
              keys_map = {key: 0}
              key_size = len(keys_map)
          else:
              all_keys = get_sparse_matrix_keys(session, key_table)
              key_size = len(all_keys)
              keys_map = {}
              for (i, k) in enumerate(all_keys):
                  if candidate_class in k.candidate_classes:
                      keys_map[k.name] = i
  
          indptr = [0]
          indices = []
          data = []
          for cand in cand_list:
              values = _get_cand_values(cand, key_table)
              if values:
                  for cand_key, cand_value in zip(values[0].keys, values[0].values):
                      if cand_key in keys_map:
                          indices.append(keys_map[cand_key])
                          data.append(cand_value)
  
              indptr.append(len(indices))
  
          result.append(
              csr_matrix((data, indices, indptr), shape=(len(cand_list), key_size))
          )
  
      return result
docs: Load sparse matrix of GoldLabels for each candidate_class.